public sealed class UpsertGlobalNotificationSettingsCommand : IRequest
{
public Guid EventTypeId { get; set; }
public bool InternalNotificationsEnabled { get; set; }
public bool EmailNotificationsEnabled { get; set; }
public bool SmsNotificationsEnabled { get; set; }
}
public sealed class UpsertGlobalNotificationSettingsCommandHandler : IRequestHandler<UpsertGlobalNotificationSettingsCommand>
{
private readonly AppDbContext _dbContext;
public UpsertGlobalNotificationSettingsCommandHandler(AppDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task Handle(UpsertGlobalNotificationSettingsCommand request, CancellationToken cancellationToken)
{
var existingSettings = await _dbContext.GlobalNotificationSettings.FindAsync(new object[] { request.EventTypeId }, cancellationToken);
if (existingSettings == null)
{
var entity = new GlobalNotificationSettings
{
EventTypeId = request.EventTypeId,
InternalNotificationsEnabled = request.InternalNotificationsEnabled,
EmailNotificationsEnabled = request.EmailNotificationsEnabled,
SmsNotificationsEnabled = request.SmsNotificationsEnabled
};
_dbContext.GlobalNotificationSettings.Add(entity);
}
else
{
existingSettings.InternalNotificationsEnabled = request.InternalNotificationsEnabled;
existingSettings.EmailNotificationsEnabled = request.EmailNotificationsEnabled;
existingSettings.SmsNotificationsEnabled = request.SmsNotificationsEnabled;
}
await _dbContext.SaveChangesAsync(cancellationToken);
}
}