Refactored project to module structure
This commit is contained in:
13
Moonlight.Api/Shared/Frontend/FrontendConfigMapper.cs
Normal file
13
Moonlight.Api/Shared/Frontend/FrontendConfigMapper.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Moonlight.Shared.Shared.Frontend;
|
||||
using Riok.Mapperly.Abstractions;
|
||||
|
||||
namespace Moonlight.Api.Shared.Frontend;
|
||||
|
||||
[Mapper]
|
||||
[SuppressMessage("Mapper", "RMG020:No members are mapped in an object mapping")]
|
||||
[SuppressMessage("Mapper", "RMG012:No members are mapped in an object mapping")]
|
||||
public static partial class FrontendConfigMapper
|
||||
{
|
||||
public static partial FrontendConfigDto ToDto(FrontendConfiguration configuration);
|
||||
}
|
||||
3
Moonlight.Api/Shared/Frontend/FrontendConfiguration.cs
Normal file
3
Moonlight.Api/Shared/Frontend/FrontendConfiguration.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace Moonlight.Api.Shared.Frontend;
|
||||
|
||||
public record FrontendConfiguration(string Name, string? ThemeCss);
|
||||
23
Moonlight.Api/Shared/Frontend/FrontendController.cs
Normal file
23
Moonlight.Api/Shared/Frontend/FrontendController.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moonlight.Shared.Shared.Frontend;
|
||||
|
||||
namespace Moonlight.Api.Shared.Frontend;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/frontend")]
|
||||
public class FrontendController : Controller
|
||||
{
|
||||
private readonly FrontendService FrontendService;
|
||||
|
||||
public FrontendController(FrontendService frontendService)
|
||||
{
|
||||
FrontendService = frontendService;
|
||||
}
|
||||
|
||||
[HttpGet("config")]
|
||||
public async Task<ActionResult<FrontendConfigDto>> GetConfigAsync()
|
||||
{
|
||||
var configuration = await FrontendService.GetConfigurationAsync();
|
||||
return FrontendConfigMapper.ToDto(configuration);
|
||||
}
|
||||
}
|
||||
52
Moonlight.Api/Shared/Frontend/FrontendService.cs
Normal file
52
Moonlight.Api/Shared/Frontend/FrontendService.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moonlight.Api.Admin.Sys.Settings;
|
||||
using Moonlight.Api.Admin.Sys.Versions;
|
||||
using Moonlight.Api.Infrastructure.Database;
|
||||
using Moonlight.Api.Infrastructure.Database.Entities;
|
||||
|
||||
namespace Moonlight.Api.Shared.Frontend;
|
||||
|
||||
public class FrontendService
|
||||
{
|
||||
private const string CacheKey = $"Moonlight.{nameof(FrontendService)}.{nameof(GetConfigurationAsync)}";
|
||||
private readonly IMemoryCache Cache;
|
||||
private readonly IOptions<FrontendOptions> Options;
|
||||
private readonly SettingsService SettingsService;
|
||||
private readonly DatabaseRepository<Theme> ThemeRepository;
|
||||
|
||||
public FrontendService(IMemoryCache cache, DatabaseRepository<Theme> themeRepository,
|
||||
IOptions<FrontendOptions> options, SettingsService settingsService)
|
||||
{
|
||||
Cache = cache;
|
||||
ThemeRepository = themeRepository;
|
||||
Options = options;
|
||||
SettingsService = settingsService;
|
||||
}
|
||||
|
||||
public async Task<FrontendConfiguration> GetConfigurationAsync()
|
||||
{
|
||||
if (Cache.TryGetValue(CacheKey, out FrontendConfiguration? value))
|
||||
if (value != null)
|
||||
return value;
|
||||
|
||||
var theme = await ThemeRepository
|
||||
.Query()
|
||||
.FirstOrDefaultAsync(x => x.IsEnabled);
|
||||
|
||||
var name = await SettingsService.GetValueAsync<string>(FrontendSettingConstants.Name);
|
||||
|
||||
var config = new FrontendConfiguration(name ?? "Moonlight", theme?.CssContent);
|
||||
|
||||
Cache.Set(CacheKey, config, TimeSpan.FromMinutes(Options.Value.CacheMinutes));
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
public Task ResetCacheAsync()
|
||||
{
|
||||
Cache.Remove(CacheKey);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Moonlight.Api.Shared.Frontend;
|
||||
|
||||
public class FrontendSettingConstants
|
||||
{
|
||||
public const string Name = "Moonlight.Frontend.Name";
|
||||
}
|
||||
Reference in New Issue
Block a user