diff --git a/src/ServiceControl.Persistence.EFCore/Implementation/NotificationsDataStore.cs b/src/ServiceControl.Persistence.EFCore/Implementation/NotificationsDataStore.cs index c89c2c5666..a3c4793539 100644 --- a/src/ServiceControl.Persistence.EFCore/Implementation/NotificationsDataStore.cs +++ b/src/ServiceControl.Persistence.EFCore/Implementation/NotificationsDataStore.cs @@ -1,7 +1,14 @@ namespace ServiceControl.Persistence.EFCore.Implementation; -public class NotificationsDataStore : INotificationsDataStore +using DbContexts; +using Microsoft.Extensions.DependencyInjection; + +public class NotificationsDataStore(IServiceProvider serviceProvider) : INotificationsDataStore { - public Task CreateNotificationsManager() => - throw new NotImplementedException(); + public Task CreateNotificationsManager() + { + var scope = serviceProvider.CreateAsyncScope(); + ServiceControlDbContext serviceControlDbContext = scope.ServiceProvider.GetRequiredService(); + return Task.FromResult(new NotificationsManager(scope, serviceControlDbContext)); + } } diff --git a/src/ServiceControl.Persistence.EFCore/Implementation/NotificationsManager.cs b/src/ServiceControl.Persistence.EFCore/Implementation/NotificationsManager.cs index b601155a89..760270e5a5 100644 --- a/src/ServiceControl.Persistence.EFCore/Implementation/NotificationsManager.cs +++ b/src/ServiceControl.Persistence.EFCore/Implementation/NotificationsManager.cs @@ -1,14 +1,35 @@ namespace ServiceControl.Persistence.EFCore.Implementation; -using ServiceControl.Notifications; +using DbContexts; +using Infrastructure; +using Notifications; -public class NotificationsManager : INotificationsManager +public class NotificationsManager(IAsyncDisposable scope, ServiceControlDbContext dbContext) : INotificationsManager { - public Task LoadSettings() => - throw new NotImplementedException(); + NotificationsSettings? _settings; - public Task SaveChanges() => - throw new NotImplementedException(); + public Task SaveChanges() + { + if (_settings == null) + { + return Task.CompletedTask; + } - public ValueTask DisposeAsync() => default; -} + return dbContext.StoreSetting(SettingKeys.NotificationEmails, _settings.Email, CancellationToken.None); + } + + public async Task LoadSettings() + { + var settings = await dbContext.GetSetting(SettingKeys.NotificationEmails, CancellationToken.None); + _settings = new NotificationsSettings() { Email = settings ?? new EmailNotifications() }; + return _settings; + } + + public async ValueTask DisposeAsync() + { + await dbContext.DisposeAsync(); + await scope.DisposeAsync(); + + GC.SuppressFinalize(this); + } +} \ No newline at end of file diff --git a/src/ServiceControl.Persistence.EFCore/Infrastructure/SettingKeys.cs b/src/ServiceControl.Persistence.EFCore/Infrastructure/SettingKeys.cs index bdafd60226..7a8e403170 100644 --- a/src/ServiceControl.Persistence.EFCore/Infrastructure/SettingKeys.cs +++ b/src/ServiceControl.Persistence.EFCore/Infrastructure/SettingKeys.cs @@ -8,4 +8,5 @@ static class SettingKeys public const string AuditServiceMetadata = "AuditServiceMetadata"; public const string ReportMasks = "ReportMasks"; public const string LicensedEndpointDetails = "LicensedEndpointDetails"; + public const string NotificationEmails = "NotificationEmails"; } diff --git a/src/ServiceControl.Persistence.Tests.PostgreSql/ServiceControl.Persistence.Tests.PostgreSql.csproj b/src/ServiceControl.Persistence.Tests.PostgreSql/ServiceControl.Persistence.Tests.PostgreSql.csproj index 46290f8252..e6a7e6b3ba 100644 --- a/src/ServiceControl.Persistence.Tests.PostgreSql/ServiceControl.Persistence.Tests.PostgreSql.csproj +++ b/src/ServiceControl.Persistence.Tests.PostgreSql/ServiceControl.Persistence.Tests.PostgreSql.csproj @@ -36,7 +36,6 @@ - diff --git a/src/ServiceControl.Persistence.Tests.SqlServer/ServiceControl.Persistence.Tests.SqlServer.csproj b/src/ServiceControl.Persistence.Tests.SqlServer/ServiceControl.Persistence.Tests.SqlServer.csproj index e268e9acdc..e2a322f5b9 100644 --- a/src/ServiceControl.Persistence.Tests.SqlServer/ServiceControl.Persistence.Tests.SqlServer.csproj +++ b/src/ServiceControl.Persistence.Tests.SqlServer/ServiceControl.Persistence.Tests.SqlServer.csproj @@ -36,7 +36,6 @@ -