diff --git a/src/Lumina.Application/Common/Mapping/Themes/RestoreThemeRequestMapping.cs b/src/Lumina.Application/Common/Mapping/Themes/RestoreThemeRequestMapping.cs
new file mode 100644
index 00000000..e4087116
--- /dev/null
+++ b/src/Lumina.Application/Common/Mapping/Themes/RestoreThemeRequestMapping.cs
@@ -0,0 +1,22 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Core.Themes.Management.Commands.RestoreTheme;
+using Lumina.Contracts.Requests.Themes;
+#endregion
+
+namespace Lumina.Application.Common.Mapping.Themes;
+
+///
+/// Extension methods for converting .
+///
+public static class RestoreThemeRequestMapping
+{
+ ///
+ /// Converts to .
+ ///
+ /// The request to be converted.
+ /// The converted command.
+ public static RestoreThemeCommand ToCommand(this RestoreThemeRequest request)
+ {
+ return new RestoreThemeCommand(request.ThemeId);
+ }
+}
diff --git a/src/Lumina.Application/Common/Mapping/Themes/ThemeEntityMapping.cs b/src/Lumina.Application/Common/Mapping/Themes/ThemeEntityMapping.cs
index c6818b85..b3460bc2 100644
--- a/src/Lumina.Application/Common/Mapping/Themes/ThemeEntityMapping.cs
+++ b/src/Lumina.Application/Common/Mapping/Themes/ThemeEntityMapping.cs
@@ -27,6 +27,7 @@ public static class ThemeEntityMapping
theme.PreviewPath,
theme.InstallSource,
theme.IsCurrent,
- theme.InstalledAtUtc);
+ theme.InstalledAtUtc,
+ theme.IsDeleted);
}
}
diff --git a/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommand.cs b/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommand.cs
new file mode 100644
index 00000000..642eb561
--- /dev/null
+++ b/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommand.cs
@@ -0,0 +1,16 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.CQRS;
+using System;
+using System.Diagnostics;
+#endregion
+
+namespace Lumina.Application.Core.Themes.Management.Commands.RestoreTheme;
+
+///
+/// Command for restoring a soft deleted bundled theme.
+///
+/// The manifest id of the theme to restore.
+[DebuggerDisplay("ThemeId: {ThemeId}")]
+public record RestoreThemeCommand(
+ string? ThemeId
+) : ICommand;
diff --git a/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandHandler.cs b/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandHandler.cs
new file mode 100644
index 00000000..c092f1ac
--- /dev/null
+++ b/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandHandler.cs
@@ -0,0 +1,106 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.CQRS;
+using Lumina.Application.Common.DataAccess.Entities.Themes;
+using Lumina.Application.Common.DataAccess.UoW;
+using Lumina.Application.Common.Infrastructure.Authentication;
+using Lumina.Application.Common.Infrastructure.Authorization;
+using Lumina.Application.Common.Infrastructure.Themes;
+using Lumina.Application.Common.Infrastructure.Validation;
+using Lumina.Domain.Common.Primitives;
+using Lumina.Domain.SharedKernel.Common.Enums.Themes;
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
+using DomainErrors = Lumina.Domain.Common.Errors.Errors;
+#endregion
+
+namespace Lumina.Application.Core.Themes.Management.Commands.RestoreTheme;
+
+///
+/// Handler for the command to restore a soft deleted bundled theme.
+///
+public class RestoreThemeCommandHandler : ICommandHandler>
+{
+ private readonly IUnitOfWork _unitOfWork;
+ private readonly IThemeService _themeService;
+ private readonly ICurrentUserService _currentUserService;
+ private readonly IAuthorizationService _authorizationService;
+ private readonly IValidator _validator;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Injected unit of work for interacting with the data access layer repositories.
+ /// Injected service for the server-side storage and serving of theme packs.
+ /// Injected service to retrieve the current user information.
+ /// Injected service for authorization related functionality.
+ /// Injected validator for application validation rules.
+ public RestoreThemeCommandHandler(
+ IUnitOfWork unitOfWork,
+ IThemeService themeService,
+ ICurrentUserService currentUserService,
+ IAuthorizationService authorizationService,
+ IValidator validator)
+ {
+ _unitOfWork = unitOfWork;
+ _themeService = themeService;
+ _currentUserService = currentUserService;
+ _authorizationService = authorizationService;
+ _validator = validator;
+ }
+
+ ///
+ /// Handles the command to restore a soft deleted bundled theme.
+ ///
+ /// The command to be handled.
+ /// Cancellation token that can be used to stop the execution.
+ ///
+ /// An representing either a successful operation, or an error.
+ ///
+ public async Task> HandleAsync(RestoreThemeCommand command, CancellationToken cancellationToken)
+ {
+ List validationResult = _validator.Validate(command);
+ if (validationResult.Count > 0)
+ return validationResult;
+
+ Guid? currentUserId = _currentUserService.UserId;
+ if (currentUserId is null)
+ return ApplicationErrors.Authorization.NotAuthorized;
+ Guid userId = currentUserId.Value;
+
+ // only admins can restore themes
+ if (!await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false))
+ return ApplicationErrors.Authorization.NotAuthorized;
+
+ Result getThemeResult = await _unitOfWork.ThemeRepository.GetByThemeIdAsync(command.ThemeId!, cancellationToken).ConfigureAwait(false);
+ if (getThemeResult.IsFailure)
+ return getThemeResult.Errors;
+
+ ThemeEntity? theme = getThemeResult.Value;
+ if (theme is null)
+ return DomainErrors.Themes.ThemeNotFound;
+
+ // only bundled themes are soft deleted, so only a soft deleted bundled theme can be restored
+ if (!theme.IsDeleted || theme.InstallSource != ThemeInstallSource.Bundled)
+ return DomainErrors.Themes.ThemeCannotBeRestored;
+
+ // restore the pack files from the shipped archive before reactivating the theme
+ Result restoreFilesResult = await _themeService.RestoreBundledThemeAsync(theme.ThemeId, cancellationToken).ConfigureAwait(false);
+ if (restoreFilesResult.IsFailure)
+ return restoreFilesResult.Errors;
+
+ theme.IsDeleted = false;
+ theme.IsCurrent = null;
+ theme.UpdatedOnUtc = DateTime.UtcNow;
+ theme.UpdatedBy = userId;
+ Result updateResult = await _unitOfWork.ThemeRepository.UpdateAsync(theme, cancellationToken).ConfigureAwait(false);
+ if (updateResult.IsFailure)
+ return updateResult.Errors;
+
+ await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
+
+ return Result.Success;
+ }
+}
diff --git a/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandValidator.cs b/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandValidator.cs
new file mode 100644
index 00000000..d2b145a9
--- /dev/null
+++ b/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandValidator.cs
@@ -0,0 +1,24 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Infrastructure.Validation;
+using Lumina.Application.Common.Utilities;
+using Lumina.Application.Core.Themes.Management.Commands.RestoreTheme;
+using DomainErrors = Lumina.Domain.Common.Errors.Errors;
+#endregion
+
+namespace Lumina.Application.Core.Themes.Management.Commands.RestoreTheme;
+
+///
+/// Validates the needed validation rules for .
+///
+public class RestoreThemeCommandValidator : AbstractValidator
+{
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RestoreThemeCommandValidator()
+ {
+ RuleFor(command => command.ThemeId)
+ .NotEmpty()
+ .WithError(DomainErrors.Themes.ThemeIdCannotBeEmpty);
+ }
+}
diff --git a/src/Lumina.Application/Core/Themes/Management/Queries/GetThemes/GetThemesQueryHandler.cs b/src/Lumina.Application/Core/Themes/Management/Queries/GetThemes/GetThemesQueryHandler.cs
index 90202b57..03eb9b5a 100644
--- a/src/Lumina.Application/Core/Themes/Management/Queries/GetThemes/GetThemesQueryHandler.cs
+++ b/src/Lumina.Application/Core/Themes/Management/Queries/GetThemes/GetThemesQueryHandler.cs
@@ -45,10 +45,11 @@ public class GetThemesQueryHandler : IQueryHandler>([.. getThemesResult.Value
- .Where(theme => !theme.IsDeleted)
+ .OrderBy(theme => theme.IsDeleted)
// the shipped bundled themes are listed before the user themes, so the defaults stay the most visible
- .OrderByDescending(theme => theme.InstallSource == ThemeInstallSource.Bundled)
+ .ThenByDescending(theme => theme.InstallSource == ThemeInstallSource.Bundled)
.ThenBy(theme => theme.Name, StringComparer.OrdinalIgnoreCase)
.Select(theme => theme.ToResponse())]);
}
diff --git a/src/Lumina.Contracts/Requests/Themes/RestoreThemeRequest.cs b/src/Lumina.Contracts/Requests/Themes/RestoreThemeRequest.cs
new file mode 100644
index 00000000..b0e07d08
--- /dev/null
+++ b/src/Lumina.Contracts/Requests/Themes/RestoreThemeRequest.cs
@@ -0,0 +1,15 @@
+#region ========================================================================= USING =====================================================================================
+using System;
+using System.Diagnostics;
+#endregion
+
+namespace Lumina.Contracts.Requests.Themes;
+
+///
+/// Represents a request to restore a soft deleted bundled theme.
+///
+/// The manifest id of the theme to restore. Required.
+[DebuggerDisplay("ThemeId: {ThemeId}")]
+public record RestoreThemeRequest(
+ string? ThemeId
+);
diff --git a/src/Lumina.Contracts/Responses/Themes/ThemeResponse.cs b/src/Lumina.Contracts/Responses/Themes/ThemeResponse.cs
index adda0c8c..b8aca76f 100644
--- a/src/Lumina.Contracts/Responses/Themes/ThemeResponse.cs
+++ b/src/Lumina.Contracts/Responses/Themes/ThemeResponse.cs
@@ -19,6 +19,7 @@ namespace Lumina.Contracts.Responses.Themes;
/// The source the theme was installed from.
/// Whether the theme is the currently active one.
/// The UTC timestamp at which the theme was installed.
+/// Whether the theme was deleted by the user, which is only possible for bundled themes.
[DebuggerDisplay("ThemeId: {ThemeId}, Name: {Name}")]
public record ThemeResponse(
Guid Id,
@@ -30,5 +31,6 @@ public record ThemeResponse(
string? PreviewPath,
ThemeInstallSource InstallSource,
bool? IsCurrent,
- DateTime InstalledAtUtc
+ DateTime InstalledAtUtc,
+ bool IsDeleted
);
diff --git a/src/Lumina.Domain/Common/Errors/Errors.Themes.cs b/src/Lumina.Domain/Common/Errors/Errors.Themes.cs
index 81affa2e..3c32d711 100644
--- a/src/Lumina.Domain/Common/Errors/Errors.Themes.cs
+++ b/src/Lumina.Domain/Common/Errors/Errors.Themes.cs
@@ -18,6 +18,7 @@ public static partial class Errors
public static Error ThemeArchiveCannotBeNull => Error.Validation(description: nameof(ThemeArchiveCannotBeNull));
public static Error LastBundledThemeCannotBeDeleted => Error.Forbidden(description: nameof(LastBundledThemeCannotBeDeleted));
public static Error ThemeCannotBeDeleted => Error.Forbidden(description: nameof(ThemeCannotBeDeleted));
+ public static Error ThemeCannotBeRestored => Error.Forbidden(description: nameof(ThemeCannotBeRestored));
public static Error ThemeArchiveNotReadable => Error.Failure(description: nameof(ThemeArchiveNotReadable));
public static Error ThemeFilesUnreadable => Error.Failure(description: nameof(ThemeFilesUnreadable));
public static Error ThemeNotAvailable => Error.Failure(description: nameof(ThemeNotAvailable));
diff --git a/src/Lumina.Presentation.Api/Common/Routes/Themes/ApiRoutes.Themes.cs b/src/Lumina.Presentation.Api/Common/Routes/Themes/ApiRoutes.Themes.cs
index f9e366ce..3c094d40 100644
--- a/src/Lumina.Presentation.Api/Common/Routes/Themes/ApiRoutes.Themes.cs
+++ b/src/Lumina.Presentation.Api/Common/Routes/Themes/ApiRoutes.Themes.cs
@@ -15,6 +15,7 @@ public static partial class ApiRoutes
public const string GET_THEME_TEMPLATE = "/themes/{themeId}/templates/{*pageKey}";
public const string GET_THEME_ASSET = "/themes/{themeId}/assets/{*assetPath}";
public const string GET_THEME_ARCHIVE = "/themes/{themeId}/archive";
+ public const string RESTORE_THEME = "/themes/{themeId}/restore";
public const string DELETE_THEME = "/themes/{themeId}";
}
}
diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/InstallTheme/InstallThemeEndpointSummary.cs b/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/InstallTheme/InstallThemeEndpointSummary.cs
index 9513198f..83bd9815 100644
--- a/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/InstallTheme/InstallThemeEndpointSummary.cs
+++ b/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/InstallTheme/InstallThemeEndpointSummary.cs
@@ -34,7 +34,8 @@ public class InstallThemeEndpointSummary : Summary
+/// API endpoint for the /themes/{themeId}/restore route.
+///
+public class RestoreThemeEndpoint : BaseEndpoint
+{
+ private readonly ICommandHandler> _restoreThemeCommandHandler;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Injected service for handling restore theme commands.
+ public RestoreThemeEndpoint(ICommandHandler> restoreThemeCommandHandler)
+ {
+ _restoreThemeCommandHandler = restoreThemeCommandHandler;
+ }
+
+ ///
+ /// Configures the API endpoint.
+ ///
+ public override void Configure()
+ {
+ Verbs(FastEndpoints.Http.POST);
+ Routes(ApiRoutes.Themes.RESTORE_THEME);
+ Version(1);
+ DontCatchExceptions();
+ }
+
+ ///
+ /// Restores the soft deleted bundled theme identified by .
+ ///
+ /// The request containing the theme to restore.
+ /// Cancellation token that can be used to stop the execution.
+ public override async Task ExecuteAsync(RestoreThemeRequest request, CancellationToken cancellationToken)
+ {
+ Result result = await _restoreThemeCommandHandler.HandleAsync(request.ToCommand(), cancellationToken).ConfigureAwait(false);
+ return result.Match(success => TypedResults.NoContent(), Problem);
+ }
+}
diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/RestoreTheme/RestoreThemeEndpointSummary.cs b/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/RestoreTheme/RestoreThemeEndpointSummary.cs
new file mode 100644
index 00000000..e2592e87
--- /dev/null
+++ b/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/RestoreTheme/RestoreThemeEndpointSummary.cs
@@ -0,0 +1,120 @@
+#region ========================================================================= USING =====================================================================================
+using FastEndpoints;
+using Lumina.Contracts.Requests.Themes;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+#endregion
+
+namespace Lumina.Presentation.Api.Core.Endpoints.Themes.Management.RestoreTheme;
+
+///
+/// Class used for providing a textual description for the API endpoint, for OpenAPI.
+///
+[ExcludeFromCodeCoverage]
+public class RestoreThemeEndpointSummary : Summary
+{
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RestoreThemeEndpointSummary()
+ {
+ Summary = "Restores a deleted bundled theme.";
+ Description = "Restores the files and reactivates a bundled theme that was soft deleted by the user, restoring the pack from the shipped archive.";
+
+ ExampleRequest = new RestoreThemeRequest(
+ ThemeId: "editorial-paper"
+ );
+
+ RequestParam(r => r.ThemeId, "The manifest id of the bundled theme to restore. Required.");
+
+ Response(204, "The theme was successfully restored.");
+
+ Response(401, "Authentication required.", "application/problem+json",
+ example: new[]
+ {
+ new
+ {
+ type = "https://tools.ietf.org/html/rfc7235#section-3.1",
+ status = 401,
+ title = "Unauthorized",
+ detail = "You are not authorized",
+ instance = "/api/v1/themes/{themeId}/restore"
+ },
+ new
+ {
+ type = "https://tools.ietf.org/html/rfc7235#section-3.1",
+ status = 401,
+ title = "Unauthorized",
+ detail = "Invalid token: The token expired at '01/01/2024 01:00:00'",
+ instance = "/api/v1/themes/{themeId}/restore"
+ },
+ new
+ {
+ type = "https://tools.ietf.org/html/rfc7235#section-3.1",
+ status = 401,
+ title = "Unauthorized",
+ detail = "The token is invalid",
+ instance = "/api/v1/themes/{themeId}/restore"
+ }
+ }
+ );
+
+ Response(403, "The request failed because the user is not an Admin or the theme is not a soft deleted bundled theme.", "application/problem+json",
+ example: new[]
+ {
+ new
+ {
+ type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
+ title = "General.Failure",
+ status = 403,
+ detail = "NotAuthorized",
+ instance = "/api/v1/themes/{themeId}/restore",
+ traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
+ },
+ new
+ {
+ type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
+ title = "General.Failure",
+ status = 403,
+ detail = "ThemeCannotBeRestored",
+ instance = "/api/v1/themes/{themeId}/restore",
+ traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
+ }
+ }
+ );
+
+ Response(404, "The request failed because the provided theme does not exist.", "application/problem+json",
+ example: new
+ {
+ type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
+ title = "General.NotFound",
+ status = 404,
+ detail = "ThemeNotFound",
+ instance = "/api/v1/themes/{themeId}/restore",
+ traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
+ }
+ );
+
+ Response(422, "The request did not pass validation checks.", "application/problem+json",
+ example: new
+ {
+ type = "https://tools.ietf.org/html/rfc4918#section-11.2",
+ title = "General.Validation",
+ status = 422,
+ detail = "OneOrMoreValidationErrorsOccurred",
+ instance = "/api/v1/themes/{themeId}/restore",
+ errors = new Dictionary
+ {
+ {
+ "General.Validation", new[]
+ {
+ "ThemeIdCannotBeEmpty"
+ }
+ }
+ },
+ traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
+ }
+ );
+ }
+}
diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/SetCurrentTheme/SetCurrentThemeEndpointSummary.cs b/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/SetCurrentTheme/SetCurrentThemeEndpointSummary.cs
index a26d0331..b6bf6338 100644
--- a/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/SetCurrentTheme/SetCurrentThemeEndpointSummary.cs
+++ b/src/Lumina.Presentation.Api/Core/Endpoints/Themes/Management/SetCurrentTheme/SetCurrentThemeEndpointSummary.cs
@@ -41,7 +41,8 @@ public class SetCurrentThemeEndpointSummary : Summary{{content}}