From 9628861186612d87acf00858d89fade013d5bab4 Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Wed, 4 Dec 2024 15:26:54 +0100 Subject: [PATCH] Add AliasVault.Shared.Server project (#221) --- aliasvault.sln | 7 ++++ .../AliasVault.Shared.Server.csproj | 36 +++++++++++++++++++ .../Models/ServerSettingsModel.cs | 2 +- src/Shared/AliasVault.Shared.Server/README.md | 5 +++ .../Services/ServerSettingsService.cs | 31 +++++++--------- 5 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 src/Shared/AliasVault.Shared.Server/AliasVault.Shared.Server.csproj rename src/{AliasVault.Admin => Shared/AliasVault.Shared.Server}/Models/ServerSettingsModel.cs (97%) create mode 100644 src/Shared/AliasVault.Shared.Server/README.md rename src/{AliasVault.Admin => Shared/AliasVault.Shared.Server}/Services/ServerSettingsService.cs (81%) diff --git a/aliasvault.sln b/aliasvault.sln index 512cdb7b9..d20754add 100644 --- a/aliasvault.sln +++ b/aliasvault.sln @@ -69,6 +69,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasVault.Shared.Core", "s EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasVault.TaskRunner", "src\Services\AliasVault.TaskRunner\AliasVault.TaskRunner.csproj", "{D631A936-DD1C-40CC-B735-BD0A5D4F46A1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasVault.Shared.Server", "src\Shared\AliasVault.Shared.Server\AliasVault.Shared.Server.csproj", "{34FADEB6-4B56-463B-B359-F844B43D76D9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -175,6 +177,10 @@ Global {D631A936-DD1C-40CC-B735-BD0A5D4F46A1}.Debug|Any CPU.Build.0 = Debug|Any CPU {D631A936-DD1C-40CC-B735-BD0A5D4F46A1}.Release|Any CPU.ActiveCfg = Release|Any CPU {D631A936-DD1C-40CC-B735-BD0A5D4F46A1}.Release|Any CPU.Build.0 = Release|Any CPU + {34FADEB6-4B56-463B-B359-F844B43D76D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34FADEB6-4B56-463B-B359-F844B43D76D9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34FADEB6-4B56-463B-B359-F844B43D76D9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34FADEB6-4B56-463B-B359-F844B43D76D9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -204,6 +210,7 @@ Global {15EFE0D0-F41B-47D7-86B7-8F840335CB82} = {DD359F0A-0180-4F8F-9E48-46213386BA4D} {40CA41BF-9E67-4D0A-A3F8-38B94992E4CA} = {DD359F0A-0180-4F8F-9E48-46213386BA4D} {D631A936-DD1C-40CC-B735-BD0A5D4F46A1} = {8A477241-B96C-4174-968D-D40CB77F1ECD} + {34FADEB6-4B56-463B-B359-F844B43D76D9} = {DD359F0A-0180-4F8F-9E48-46213386BA4D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {FEE82475-C009-4762-8113-A6563D9DC49E} diff --git a/src/Shared/AliasVault.Shared.Server/AliasVault.Shared.Server.csproj b/src/Shared/AliasVault.Shared.Server/AliasVault.Shared.Server.csproj new file mode 100644 index 000000000..54233aa2e --- /dev/null +++ b/src/Shared/AliasVault.Shared.Server/AliasVault.Shared.Server.csproj @@ -0,0 +1,36 @@ + + + + net9.0 + enable + enable + + + + bin\Debug\net9.0\AliasVault.Shared.Server.xml + true + + + + bin\Release\net9.0\AliasVault.Shared.Server.xml + true + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + stylecop.json + + + + diff --git a/src/AliasVault.Admin/Models/ServerSettingsModel.cs b/src/Shared/AliasVault.Shared.Server/Models/ServerSettingsModel.cs similarity index 97% rename from src/AliasVault.Admin/Models/ServerSettingsModel.cs rename to src/Shared/AliasVault.Shared.Server/Models/ServerSettingsModel.cs index 4ef9bf826..789fd6ef8 100644 --- a/src/AliasVault.Admin/Models/ServerSettingsModel.cs +++ b/src/Shared/AliasVault.Shared.Server/Models/ServerSettingsModel.cs @@ -5,7 +5,7 @@ // //----------------------------------------------------------------------- -namespace AliasVault.Admin.Models; +namespace AliasVault.Shared.Server.Models; /// /// Server settings model. diff --git a/src/Shared/AliasVault.Shared.Server/README.md b/src/Shared/AliasVault.Shared.Server/README.md new file mode 100644 index 000000000..00eacde73 --- /dev/null +++ b/src/Shared/AliasVault.Shared.Server/README.md @@ -0,0 +1,5 @@ +# AliasVault.Shared.Server + +This project contains shared functionality used only by the server applications and not required by the client applications. + +This is to reduce the number of client dependencies and keep the client applications as lightweight as possible. \ No newline at end of file diff --git a/src/AliasVault.Admin/Services/ServerSettingsService.cs b/src/Shared/AliasVault.Shared.Server/Services/ServerSettingsService.cs similarity index 81% rename from src/AliasVault.Admin/Services/ServerSettingsService.cs rename to src/Shared/AliasVault.Shared.Server/Services/ServerSettingsService.cs index 752fb6434..2f78b96a1 100644 --- a/src/AliasVault.Admin/Services/ServerSettingsService.cs +++ b/src/Shared/AliasVault.Shared.Server/Services/ServerSettingsService.cs @@ -5,33 +5,24 @@ // //----------------------------------------------------------------------- -namespace AliasVault.Admin.Services; +namespace AliasVault.Shared.Server.Services; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AliasServerDb; -using AliasVault.Admin.Models; +using AliasVault.Shared.Server.Models; using Microsoft.EntityFrameworkCore; /// /// Server settings service. /// -public class ServerSettingsService +/// IDbContextFactory instance. +public class ServerSettingsService(IDbContextFactory dbContextFactory) { - private readonly AliasServerDbContext _dbContext; private readonly Dictionary _cache = new(); - /// - /// Initializes a new instance of the class. - /// - /// The database context. - public ServerSettingsService(AliasServerDbContext dbContext) - { - _dbContext = dbContext; - } - /// /// Gets the setting async. /// @@ -44,7 +35,9 @@ public class ServerSettingsService return cachedValue; } - var setting = await _dbContext.ServerSettings.FirstOrDefaultAsync(x => x.Key == key); + await using var dbContext = await dbContextFactory.CreateDbContextAsync(CancellationToken.None); + var setting = await dbContext.ServerSettings.FirstOrDefaultAsync(x => x.Key == key); + _cache[key] = setting?.Value; return setting?.Value; } @@ -63,7 +56,8 @@ public class ServerSettingsService return; } - var setting = await _dbContext.ServerSettings.FirstOrDefaultAsync(x => x.Key == key); + await using var dbContext = await dbContextFactory.CreateDbContextAsync(CancellationToken.None); + var setting = await dbContext.ServerSettings.FirstOrDefaultAsync(x => x.Key == key); var now = DateTime.UtcNow; // If setting exists and value hasn't changed, return early @@ -83,7 +77,7 @@ public class ServerSettingsService CreatedAt = now, UpdatedAt = now, }; - _dbContext.ServerSettings.Add(setting); + dbContext.ServerSettings.Add(setting); } else { @@ -91,7 +85,7 @@ public class ServerSettingsService setting.UpdatedAt = now; } - await _dbContext.SaveChangesAsync(); + await dbContext.SaveChangesAsync(); _cache[key] = value; } @@ -101,7 +95,8 @@ public class ServerSettingsService /// The settings. public async Task GetAllSettingsAsync() { - var settings = await _dbContext.ServerSettings.ToDictionaryAsync(x => x.Key, x => x.Value); + await using var dbContext = await dbContextFactory.CreateDbContextAsync(CancellationToken.None); + var settings = await dbContext.ServerSettings.ToDictionaryAsync(x => x.Key, x => x.Value); return new ServerSettingsModel {