using Microsoft.EntityFrameworkCore;
using ScadaLink.Commons.Entities.Notifications;
using ScadaLink.Commons.Interfaces.Repositories;
namespace ScadaLink.ConfigurationDatabase.Repositories;
public class NotificationRepository : INotificationRepository
{
private readonly ScadaLinkDbContext _context;
/// Initializes a new instance of the NotificationRepository class.
/// The database context.
public NotificationRepository(ScadaLinkDbContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
///
public async Task GetNotificationListByIdAsync(int id, CancellationToken cancellationToken = default)
=> await _context.Set().FindAsync(new object[] { id }, cancellationToken);
///
public async Task> GetAllNotificationListsAsync(CancellationToken cancellationToken = default)
=> await _context.Set().Include(n => n.Recipients).ToListAsync(cancellationToken);
///
public async Task GetListByNameAsync(string name, CancellationToken cancellationToken = default)
=> await _context.Set().FirstOrDefaultAsync(l => l.Name == name, cancellationToken);
///
public async Task AddNotificationListAsync(NotificationList list, CancellationToken cancellationToken = default)
=> await _context.Set().AddAsync(list, cancellationToken);
///
public Task UpdateNotificationListAsync(NotificationList list, CancellationToken cancellationToken = default)
{ _context.Set().Update(list); return Task.CompletedTask; }
///
public async Task DeleteNotificationListAsync(int id, CancellationToken cancellationToken = default)
{
var entity = await GetNotificationListByIdAsync(id, cancellationToken);
if (entity != null) _context.Set().Remove(entity);
}
///
public async Task GetRecipientByIdAsync(int id, CancellationToken cancellationToken = default)
=> await _context.Set().FindAsync(new object[] { id }, cancellationToken);
///
public async Task> GetRecipientsByListIdAsync(int notificationListId, CancellationToken cancellationToken = default)
=> await _context.Set().Where(r => r.NotificationListId == notificationListId).ToListAsync(cancellationToken);
///
public async Task AddRecipientAsync(NotificationRecipient recipient, CancellationToken cancellationToken = default)
=> await _context.Set().AddAsync(recipient, cancellationToken);
///
public Task UpdateRecipientAsync(NotificationRecipient recipient, CancellationToken cancellationToken = default)
{ _context.Set().Update(recipient); return Task.CompletedTask; }
///
public async Task DeleteRecipientAsync(int id, CancellationToken cancellationToken = default)
{
var entity = await GetRecipientByIdAsync(id, cancellationToken);
if (entity != null) _context.Set().Remove(entity);
}
///
public async Task GetSmtpConfigurationByIdAsync(int id, CancellationToken cancellationToken = default)
=> await _context.Set().FindAsync(new object[] { id }, cancellationToken);
///
public async Task> GetAllSmtpConfigurationsAsync(CancellationToken cancellationToken = default)
=> await _context.Set().ToListAsync(cancellationToken);
///
public async Task AddSmtpConfigurationAsync(SmtpConfiguration configuration, CancellationToken cancellationToken = default)
=> await _context.Set().AddAsync(configuration, cancellationToken);
///
public Task UpdateSmtpConfigurationAsync(SmtpConfiguration configuration, CancellationToken cancellationToken = default)
{ _context.Set().Update(configuration); return Task.CompletedTask; }
///
public async Task DeleteSmtpConfigurationAsync(int id, CancellationToken cancellationToken = default)
{
var entity = await GetSmtpConfigurationByIdAsync(id, cancellationToken);
if (entity != null) _context.Set().Remove(entity);
}
///
public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
=> await _context.SaveChangesAsync(cancellationToken);
}