Persists the activated theme without a redundant update

This commit is contained in:
BinaryAlley committed 2026-11-30 21:20:44 +02:00
1 parent 68143dff25
commit 2a90022aa8
4 files changed
+145 -74

No files matched your search

@@ -1,4 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Microsoft.Extensions.Logging;
using Nemera.Application.Common.DataAccess.Entities.Themes;
using Nemera.Application.Common.DataAccess.UoW;
using Nemera.Application.Common.DTO.Filtering;
@@ -7,7 +8,6 @@ using Nemera.Application.Common.Infrastructure.Models.DTO.Themes;
using Nemera.Application.Common.Infrastructure.Themes;
using Nemera.Domain.Common.Primitives;
using Nemera.Domain.SharedKernel.Common.Enums.Themes;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
@@ -32,6 +32,8 @@ internal static class ThemeSynchronizer
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
internal static async Task SynchronizeAsync(IThemeService themeService, IUnitOfWork unitOfWork, ILogger logger, CancellationToken cancellationToken)
{
// The installed themes are read tracked, so the current flag that EnsureCurrentThemeExists sets on an existing theme is
// persisted by the final SaveChangesAsync; a theme inserted during this synchronization is tracked as added by InsertAsync.
Result<PaginatedResultDto<ThemeEntity>> getThemesResult = await unitOfWork.ThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: cancellationToken).ConfigureAwait(false);
if (getThemesResult.IsFailure)
{
@@ -127,7 +129,7 @@ internal static class ThemeSynchronizer
}
await CleanUpMissingThemePacksAsync(themeService, unitOfWork, themes, logger, cancellationToken).ConfigureAwait(false);
await EnsureCurrentThemeExistsAsync(themeService, unitOfWork, themes, logger, cancellationToken).ConfigureAwait(false);
EnsureCurrentThemeExists(themeService, themes);
Result<Success> saveChangesResult = await unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
if (saveChangesResult.IsFailure)
@@ -208,14 +210,11 @@ internal static class ThemeSynchronizer
}
/// <summary>
/// Activates the configured default theme when no theme is currently active, so the application always has an active theme.
/// Marks the configured default theme as current when no theme is currently active, so the application always has an active theme.
/// </summary>
/// <param name="themeService">Injected service for the server-side storage and serving of theme packs.</param>
/// <param name="unitOfWork">The unit of work for interacting with the theme repository.</param>
/// <param name="themes">The installed themes read from the storage medium.</param>
/// <param name="logger">Injected logger used for logging.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
private static async Task EnsureCurrentThemeExistsAsync(IThemeService themeService, IUnitOfWork unitOfWork, IReadOnlyList<ThemeEntity> themes, ILogger logger, CancellationToken cancellationToken)
internal static void EnsureCurrentThemeExists(IThemeService themeService, IReadOnlyList<ThemeEntity> themes)
{
if (themes.Any(theme => !theme.IsDeleted && theme.IsCurrent == true))
return;
@@ -231,10 +230,9 @@ internal static class ThemeSynchronizer
if (defaultTheme is null)
return;
// A theme inserted during this synchronization is not in the database yet, so an UpdateAsync call would fail with ThemeNotFound.
// The entity is already tracked, as added by InsertAsync or as an existing row from the tracked read, so setting the flag here
// is persisted by the SaveChangesAsync that follows the synchronization.
defaultTheme.IsCurrent = true;
Result<Updated> updateResult = await unitOfWork.ThemeRepository.UpdateAsync(defaultTheme, cancellationToken).ConfigureAwait(false);
if (updateResult.IsFailure)
if (logger.IsEnabled(LogLevel.Warning))
logger.LogWarning("Failed to activate theme '{ThemeId}': {Error}", defaultTheme.ThemeId, updateResult.FirstError.Description);
}
}