mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 04:11:10 +00:00
Adds a restore action for soft deleted bundled themes
This commit is contained in:
1 parent
15945c2e56
commit
7e54c990ad
49 files changed
+1666
-26
No files matched your search
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting <see cref="RestoreThemeRequest"/>.
|
||||
/// </summary>
|
||||
public static class RestoreThemeRequestMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="request"/> to <see cref="RestoreThemeCommand"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to be converted.</param>
|
||||
/// <returns>The converted command.</returns>
|
||||
public static RestoreThemeCommand ToCommand(this RestoreThemeRequest request)
|
||||
{
|
||||
return new RestoreThemeCommand(request.ThemeId);
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ public static class ThemeEntityMapping
|
||||
theme.PreviewPath,
|
||||
theme.InstallSource,
|
||||
theme.IsCurrent,
|
||||
theme.InstalledAtUtc);
|
||||
theme.InstalledAtUtc,
|
||||
theme.IsDeleted);
|
||||
}
|
||||
}
|
||||
+16
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Command for restoring a soft deleted bundled theme.
|
||||
/// </summary>
|
||||
/// <param name="ThemeId">The manifest id of the theme to restore.</param>
|
||||
[DebuggerDisplay("ThemeId: {ThemeId}")]
|
||||
public record RestoreThemeCommand(
|
||||
string? ThemeId
|
||||
) : ICommand;
|
||||
+106
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for the command to restore a soft deleted bundled theme.
|
||||
/// </summary>
|
||||
public class RestoreThemeCommandHandler : ICommandHandler<RestoreThemeCommand, Result<Success>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IThemeService _themeService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IValidator<RestoreThemeCommand> _validator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RestoreThemeCommandHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
/// <param name="themeService">Injected service for the server-side storage and serving of theme packs.</param>
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public RestoreThemeCommandHandler(
|
||||
IUnitOfWork unitOfWork,
|
||||
IThemeService themeService,
|
||||
ICurrentUserService currentUserService,
|
||||
IAuthorizationService authorizationService,
|
||||
IValidator<RestoreThemeCommand> validator)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_themeService = themeService;
|
||||
_currentUserService = currentUserService;
|
||||
_authorizationService = authorizationService;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the command to restore a soft deleted bundled theme.
|
||||
/// </summary>
|
||||
/// <param name="command">The command to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> representing either a successful operation, or an error.
|
||||
/// </returns>
|
||||
public async Task<Result<Success>> HandleAsync(RestoreThemeCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> 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<ThemeEntity?> 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<Success> 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<Updated> updateResult = await _unitOfWork.ThemeRepository.UpdateAsync(theme, cancellationToken).ConfigureAwait(false);
|
||||
if (updateResult.IsFailure)
|
||||
return updateResult.Errors;
|
||||
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return Result.Success;
|
||||
}
|
||||
}
|
||||
+24
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for <see cref="RestoreThemeCommand"/>.
|
||||
/// </summary>
|
||||
public class RestoreThemeCommandValidator : AbstractValidator<RestoreThemeCommand>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RestoreThemeCommandValidator"/> class.
|
||||
/// </summary>
|
||||
public RestoreThemeCommandValidator()
|
||||
{
|
||||
RuleFor(command => command.ThemeId)
|
||||
.NotEmpty()
|
||||
.WithError(DomainErrors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -45,10 +45,11 @@ public class GetThemesQueryHandler : IQueryHandler<GetThemesQuery, Result<IReadO
|
||||
if (getThemesResult.IsFailure)
|
||||
return getThemesResult.Errors;
|
||||
|
||||
// deleted bundled themes are returned as well, so the administration page can offer to restore them
|
||||
return Result.From<IReadOnlyList<ThemeResponse>>([.. 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())]);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Contracts.Requests.Themes;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to restore a soft deleted bundled theme.
|
||||
/// </summary>
|
||||
/// <param name="ThemeId">The manifest id of the theme to restore. Required.</param>
|
||||
[DebuggerDisplay("ThemeId: {ThemeId}")]
|
||||
public record RestoreThemeRequest(
|
||||
string? ThemeId
|
||||
);
|
||||
@@ -19,6 +19,7 @@ namespace Lumina.Contracts.Responses.Themes;
|
||||
/// <param name="InstallSource">The source the theme was installed from.</param>
|
||||
/// <param name="IsCurrent">Whether the theme is the currently active one.</param>
|
||||
/// <param name="InstalledAtUtc">The UTC timestamp at which the theme was installed.</param>
|
||||
/// <param name="IsDeleted">Whether the theme was deleted by the user, which is only possible for bundled themes.</param>
|
||||
[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
|
||||
);
|
||||
@@ -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));
|
||||
|
||||
@@ -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}";
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -34,7 +34,8 @@ public class InstallThemeEndpointSummary : Summary<InstallThemeEndpoint, EmptyRe
|
||||
PreviewPath: "preview.png",
|
||||
InstallSource: ThemeInstallSource.Uploaded,
|
||||
IsCurrent: null,
|
||||
InstalledAtUtc: DateTime.UtcNow
|
||||
InstalledAtUtc: DateTime.UtcNow,
|
||||
IsDeleted: false
|
||||
));
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.Themes;
|
||||
using Lumina.Application.Core.Themes.Management.Commands.RestoreTheme;
|
||||
using Lumina.Contracts.Requests.Themes;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.Themes;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Themes.Management.RestoreTheme;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/themes/{themeId}/restore</c> route.
|
||||
/// </summary>
|
||||
public class RestoreThemeEndpoint : BaseEndpoint<RestoreThemeRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<RestoreThemeCommand, Result<Success>> _restoreThemeCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RestoreThemeEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="restoreThemeCommandHandler">Injected service for handling restore theme commands.</param>
|
||||
public RestoreThemeEndpoint(ICommandHandler<RestoreThemeCommand, Result<Success>> restoreThemeCommandHandler)
|
||||
{
|
||||
_restoreThemeCommandHandler = restoreThemeCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.POST);
|
||||
Routes(ApiRoutes.Themes.RESTORE_THEME);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restores the soft deleted bundled theme identified by <paramref name="request"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the theme to restore.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(RestoreThemeRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<Success> result = await _restoreThemeCommandHandler.HandleAsync(request.ToCommand(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.NoContent(), Problem);
|
||||
}
|
||||
}
|
||||
+120
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="RestoreThemeEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RestoreThemeEndpointSummary : Summary<RestoreThemeEndpoint, RestoreThemeRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RestoreThemeEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
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<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"ThemeIdCannotBeEmpty"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -41,7 +41,8 @@ public class SetCurrentThemeEndpointSummary : Summary<SetCurrentThemeEndpoint, S
|
||||
PreviewPath: "preview.png",
|
||||
InstallSource: ThemeInstallSource.Bundled,
|
||||
IsCurrent: true,
|
||||
InstalledAtUtc: DateTime.UtcNow
|
||||
InstalledAtUtc: DateTime.UtcNow,
|
||||
IsDeleted: false
|
||||
));
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
|
||||
+2
-1
@@ -33,7 +33,8 @@ public class GetCurrentThemeEndpointSummary : Summary<GetCurrentThemeEndpoint, E
|
||||
PreviewPath: "preview.png",
|
||||
InstallSource: ThemeInstallSource.Bundled,
|
||||
IsCurrent: true,
|
||||
InstalledAtUtc: DateTime.UtcNow
|
||||
InstalledAtUtc: DateTime.UtcNow,
|
||||
IsDeleted: false
|
||||
));
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -44,7 +44,8 @@ public class GetThemeTemplateEndpointSummary : Summary<GetThemeTemplateEndpoint,
|
||||
PreviewPath: "preview.png",
|
||||
InstallSource: ThemeInstallSource.Bundled,
|
||||
IsCurrent: true,
|
||||
InstalledAtUtc: DateTime.UtcNow
|
||||
InstalledAtUtc: DateTime.UtcNow,
|
||||
IsDeleted: false
|
||||
),
|
||||
"<html><body><div class=\"content\">{{content}}</div></body></html>"
|
||||
));
|
||||
|
||||
+4
-2
@@ -34,7 +34,8 @@ public class GetThemesEndpointSummary : Summary<GetThemesEndpoint, EmptyRequest>
|
||||
PreviewPath: "preview.png",
|
||||
InstallSource: ThemeInstallSource.Bundled,
|
||||
IsCurrent: true,
|
||||
InstalledAtUtc: DateTime.UtcNow
|
||||
InstalledAtUtc: DateTime.UtcNow,
|
||||
IsDeleted: false
|
||||
),
|
||||
new (
|
||||
Id: Guid.NewGuid(),
|
||||
@@ -46,7 +47,8 @@ public class GetThemesEndpointSummary : Summary<GetThemesEndpoint, EmptyRequest>
|
||||
PreviewPath: "preview.jpg",
|
||||
InstallSource: ThemeInstallSource.Uploaded,
|
||||
IsCurrent: null,
|
||||
InstalledAtUtc: DateTime.UtcNow
|
||||
InstalledAtUtc: DateTime.UtcNow,
|
||||
IsDeleted: false
|
||||
)
|
||||
});
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace Lumina.Presentation.Web.Common.DTO.Themes;
|
||||
/// <param name="Version">The version of the theme, using semantic version form.</param>
|
||||
/// <param name="PreviewUrl">The URL of the theme preview image.</param>
|
||||
/// <param name="IsBundled">Whether the theme ships with the application or noy.</param>
|
||||
/// <param name="IsDeleted">Whether the theme was deleted by the user, which is only possible for bundled themes.</param>
|
||||
[DebuggerDisplay("Id: {Id}, Name: {Name}")]
|
||||
public sealed record ThemeInfoDto(
|
||||
string Id,
|
||||
@@ -22,5 +23,6 @@ public sealed record ThemeInfoDto(
|
||||
string Author,
|
||||
string Version,
|
||||
string PreviewUrl,
|
||||
bool IsBundled
|
||||
bool IsBundled,
|
||||
bool IsDeleted
|
||||
);
|
||||
@@ -19,6 +19,7 @@ namespace Lumina.Presentation.Web.Common.DTO.Themes;
|
||||
/// <param name="InstallSource">The source the theme was installed from.</param>
|
||||
/// <param name="IsCurrent">Whether the theme is the currently active one.</param>
|
||||
/// <param name="InstalledAtUtc">The UTC timestamp at which the theme was installed.</param>
|
||||
/// <param name="IsDeleted">Whether the theme was deleted by the user, which is only possible for bundled themes.</param>
|
||||
[DebuggerDisplay("ThemeId: {ThemeId}, Name: {Name}")]
|
||||
public sealed record ThemeResponseDto(
|
||||
Guid Id,
|
||||
@@ -30,5 +31,6 @@ public sealed record ThemeResponseDto(
|
||||
string? PreviewPath,
|
||||
ThemeInstallSource InstallSource,
|
||||
bool? IsCurrent,
|
||||
DateTime InstalledAtUtc
|
||||
DateTime InstalledAtUtc,
|
||||
bool IsDeleted
|
||||
);
|
||||
@@ -0,0 +1,14 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Requests.Themes;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to restore a soft deleted bundled theme.
|
||||
/// </summary>
|
||||
/// <param name="ThemeId">The manifest id of the bundled theme to restore.</param>
|
||||
[DebuggerDisplay("ThemeId: {ThemeId}")]
|
||||
public record RestoreThemeRequest(
|
||||
string? ThemeId
|
||||
);
|
||||
@@ -18,6 +18,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}";
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ public static partial class WebRoutes
|
||||
public const string GET_THEMES = "{culture}/admin/themes/api-get-themes";
|
||||
public const string INSTALL_THEME = "{culture}/admin/themes/api-install-theme";
|
||||
public const string SET_CURRENT_THEME = "{culture}/admin/themes/api-set-current-theme";
|
||||
public const string RESTORE_THEME = "{culture}/admin/themes/api-restore-theme/{themeId}";
|
||||
public const string DELETE_THEME = "{culture}/admin/themes/api-delete-theme/{themeId}";
|
||||
public const string DOWNLOAD_THEME = "{culture}/admin/themes/api-download-theme/{themeId}";
|
||||
public const string THEME_ASSETS = "theme-assets/{themeId}/{*path}";
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.Authorization;
|
||||
using Lumina.Presentation.Web.Common.Requests.Themes;
|
||||
using Lumina.Presentation.Web.Common.Routes;
|
||||
using Lumina.Presentation.Web.Core.Endpoints.Common;
|
||||
using Lumina.Presentation.Web.Core.Themes;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Core.Endpoints.Admin.Themes.RestoreTheme;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/{culture}/admin/themes/api-restore-theme/{themeId}</c> route.
|
||||
/// </summary>
|
||||
public class RestoreThemeEndpoint : BaseEndpoint<RestoreThemeRequest, IResult>
|
||||
{
|
||||
private readonly ThemeService _themeService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RestoreThemeEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="themeService">Injected service for interactions with the remote theme endpoints.</param>
|
||||
public RestoreThemeEndpoint(ThemeService themeService)
|
||||
{
|
||||
_themeService = themeService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
base.Configure();
|
||||
Verbs(Http.POST);
|
||||
Routes(WebRoutes.Themes.RESTORE_THEME);
|
||||
DontAutoTag();
|
||||
Options(options => options.WithTags("Themes"));
|
||||
Policies(AuthorizationPolicies.REQUIRE_ADMIN_ROLE);
|
||||
EnableAntiforgery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restores the soft deleted bundled theme identified by <paramref name="request"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the theme to restore.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(RestoreThemeRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.ThemeId))
|
||||
return Results.Problem(statusCode: StatusCodes.Status400BadRequest, detail: "The theme id cannot be empty.");
|
||||
|
||||
await _themeService.RestoreThemeAsync(request.ThemeId, cancellationToken).ConfigureAwait(false);
|
||||
return JsonSuccess();
|
||||
}
|
||||
}
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>Herunterladen</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>Wiederherstellen</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>Hochladen</value>
|
||||
</data>
|
||||
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>Download</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>Restore</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>Upload</value>
|
||||
</data>
|
||||
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>Descargar</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>Restaurar</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>Subir</value>
|
||||
</data>
|
||||
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>Télécharger</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>Restaurer</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>Téléverser</value>
|
||||
</data>
|
||||
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>Scarica</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>Ripristina</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>Carica</value>
|
||||
</data>
|
||||
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>ダウンロード</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>復元</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>アップロード</value>
|
||||
</data>
|
||||
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>Download</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>Restore</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>Upload</value>
|
||||
</data>
|
||||
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>Descarcă</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>Restaurează</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>Încarcă</value>
|
||||
</data>
|
||||
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>Скачать</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>Восстановить</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>Загрузить</value>
|
||||
</data>
|
||||
|
||||
@@ -144,6 +144,9 @@
|
||||
<data name="Download" xml:space="preserve">
|
||||
<value>下载</value>
|
||||
</data>
|
||||
<data name="Restore" xml:space="preserve">
|
||||
<value>恢复</value>
|
||||
</data>
|
||||
<data name="Upload" xml:space="preserve">
|
||||
<value>上传</value>
|
||||
</data>
|
||||
|
||||
@@ -94,6 +94,16 @@ public sealed class ThemeService
|
||||
await _apiHttpClient.DeleteAsync(ApiRoutes.Themes.DELETE_THEME.Replace("{themeId}", themeId), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restores a soft deleted bundled theme from its shipped archive.
|
||||
/// </summary>
|
||||
/// <param name="themeId">The identifier of the bundled theme to restore.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public async Task RestoreThemeAsync(string themeId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _apiHttpClient.PostAsync<ThemeResponseDto, object>(ApiRoutes.Themes.RESTORE_THEME.Replace("{themeId}", themeId), new { }, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the theme and raw template source selected for a page key.
|
||||
/// </summary>
|
||||
@@ -134,6 +144,6 @@ public sealed class ThemeService
|
||||
private static ThemeInfoDto ToThemeInfo(ThemeResponseDto theme)
|
||||
{
|
||||
string previewUrl = string.IsNullOrWhiteSpace(theme.PreviewPath) ? "/admin/theme-placeholder.svg" : $"/theme-assets/{theme.ThemeId}/{theme.PreviewPath}";
|
||||
return new ThemeInfoDto(theme.ThemeId, theme.Name, theme.Description, theme.Author, theme.Version, previewUrl, theme.InstallSource == ThemeInstallSource.Bundled);
|
||||
return new ThemeInfoDto(theme.ThemeId, theme.Name, theme.Description, theme.Author, theme.Version, previewUrl, theme.InstallSource == ThemeInstallSource.Bundled, theme.IsDeleted);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
@using Lumina.Presentation.Web.Common.DTO.Themes
|
||||
@using Lumina.Presentation.Web.Common.Services
|
||||
@using Microsoft.AspNetCore.Mvc.Localization
|
||||
@using System.Linq
|
||||
|
||||
@{
|
||||
var themes = ViewData["themes"] as IReadOnlyList<ThemeInfoDto>;
|
||||
@@ -14,7 +15,7 @@
|
||||
int bundledThemeCount = 0;
|
||||
if (themes is not null)
|
||||
foreach (ThemeInfoDto theme in themes)
|
||||
if (theme.IsBundled)
|
||||
if (theme.IsBundled && !theme.IsDeleted)
|
||||
bundledThemeCount++;
|
||||
}
|
||||
|
||||
@@ -109,7 +110,7 @@
|
||||
@foreach (ThemeInfoDto theme in themes)
|
||||
{
|
||||
bool isCurrent = theme.Id == currentThemeId;
|
||||
bool isLastRemaining = themes.Count <= 1 || (theme.IsBundled && bundledThemeCount == 1);
|
||||
bool isLastRemaining = themes.Count(theme => !theme.IsDeleted) <= 1 || (theme.IsBundled && bundledThemeCount == 1);
|
||||
<tr data-theme-id="@theme.Id">
|
||||
<td class="text-center">
|
||||
<button type="button" class="theme-preview-button" data-preview-url="@theme.PreviewUrl" data-preview-title="@theme.Name" title="@_localizer["Preview"]">
|
||||
@@ -122,9 +123,16 @@
|
||||
<td>@(theme.IsBundled ? _localizer["Bundled"] : _localizer["UserProvided"])</td>
|
||||
<td class="text-end">
|
||||
<div class="form-row">
|
||||
<button type="button" class="confirm-button activate-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.SET_CURRENT_THEME)" @(isCurrent ? "disabled" : null)>@_localizer["Activate"]</button>
|
||||
<button type="button" class="confirm-button download-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DOWNLOAD_THEME, new { themeId = theme.Id })">@_localizer["Download"]</button>
|
||||
<button type="button" class="abort-button delete-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DELETE_THEME, new { themeId = theme.Id })" @(isLastRemaining ? "disabled" : null)>@_localizer["Delete"]</button>
|
||||
@if (theme.IsDeleted)
|
||||
{
|
||||
<button type="button" class="confirm-button restore-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.RESTORE_THEME, new { themeId = theme.Id })">@_localizer["Restore"]</button>
|
||||
}
|
||||
else
|
||||
{
|
||||
<button type="button" class="confirm-button activate-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.SET_CURRENT_THEME)" @(isCurrent ? "disabled" : null)>@_localizer["Activate"]</button>
|
||||
<button type="button" class="confirm-button download-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DOWNLOAD_THEME, new { themeId = theme.Id })">@_localizer["Download"]</button>
|
||||
<button type="button" class="abort-button delete-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DELETE_THEME, new { themeId = theme.Id })" @(isLastRemaining ? "disabled" : null)>@_localizer["Delete"]</button>
|
||||
}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -171,6 +179,16 @@
|
||||
}
|
||||
}
|
||||
|
||||
// restores the soft deleted bundled theme of the clicked row
|
||||
async function handleRestoreClick(event) {
|
||||
const button = event.target.closest('.restore-theme-button');
|
||||
if (!button)
|
||||
return;
|
||||
// an empty JSON body is sent so the antiforgery-protected POST request binds the route theme id without a JsonException
|
||||
await callApiPostAsync(button.dataset.url, {});
|
||||
reloadPage();
|
||||
}
|
||||
|
||||
// downloads the archive of the theme of the clicked row
|
||||
function handleDownloadClick(event) {
|
||||
const button = event.target.closest('.download-theme-button');
|
||||
@@ -227,6 +245,7 @@
|
||||
document.addEventListener('click', handleActivateClick);
|
||||
document.addEventListener('click', handleDownloadClick);
|
||||
document.addEventListener('click', handleDeleteClick);
|
||||
document.addEventListener('click', handleRestoreClick);
|
||||
document.addEventListener('click', handlePreviewClick);
|
||||
|
||||
// register cleanup for these listeners
|
||||
@@ -236,6 +255,7 @@
|
||||
document.removeEventListener('click', handleActivateClick);
|
||||
document.removeEventListener('click', handleDownloadClick);
|
||||
document.removeEventListener('click', handleDeleteClick);
|
||||
document.removeEventListener('click', handleRestoreClick);
|
||||
document.removeEventListener('click', handlePreviewClick);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user