Add AliasVault.Shared.Server project (#221)

This commit is contained in:
Leendert de Borst
2024-12-04 17:57:45 +01:00
parent 2b541dc28d
commit 9628861186
5 changed files with 62 additions and 19 deletions
+7
View File
@@ -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}
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>bin\Debug\net9.0\AliasVault.Shared.Server.xml</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\net9.0\AliasVault.Shared.Server.xml</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Databases\AliasServerDb\AliasServerDb.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\stylecop.json">
<Link>stylecop.json</Link>
</AdditionalFiles>
</ItemGroup>
</Project>
@@ -5,7 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------
namespace AliasVault.Admin.Models;
namespace AliasVault.Shared.Server.Models;
/// <summary>
/// Server settings model.
@@ -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.
@@ -5,33 +5,24 @@
// </copyright>
//-----------------------------------------------------------------------
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;
/// <summary>
/// Server settings service.
/// </summary>
public class ServerSettingsService
/// <param name="dbContextFactory">IDbContextFactory instance.</param>
public class ServerSettingsService(IDbContextFactory<AliasServerDbContext> dbContextFactory)
{
private readonly AliasServerDbContext _dbContext;
private readonly Dictionary<string, string?> _cache = new();
/// <summary>
/// Initializes a new instance of the <see cref="ServerSettingsService"/> class.
/// </summary>
/// <param name="dbContext">The database context.</param>
public ServerSettingsService(AliasServerDbContext dbContext)
{
_dbContext = dbContext;
}
/// <summary>
/// Gets the setting async.
/// </summary>
@@ -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
/// <returns>The settings.</returns>
public async Task<ServerSettingsModel> 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
{