using PnvPanel.Api.Common; using PnvPanel.Application.Admin.Plans; using PnvPanel.Application.Common.Messaging; using PnvPanel.Infrastructure.Identity; namespace PnvPanel.Api.Endpoints; public static class AdminPlanEndpoints { public static IEndpointRouteBuilder MapAdminPlanEndpoints(this IEndpointRouteBuilder app) { var admin = app.MapGroup("/api/admin/plans") .WithTags("Admin.Plans") .RequireAuthorization(policy => policy.RequireRole(RoleNames.Admin)); admin.MapGet("", ListPlans).Produces>(); admin.MapPost("", CreatePlan).Produces(); admin.MapPut("/{id:guid}", UpdatePlan).Produces(); admin.MapDelete("/{id:guid}", DeletePlan).Produces(StatusCodes.Status204NoContent); return app; } private static async Task ListPlans(ISender sender, CancellationToken cancellationToken) { var result = await sender.Send(new ListAdminPlansQuery(), cancellationToken); return result.ToHttpResult(); } private static async Task CreatePlan( CreatePlanCommand command, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(command, cancellationToken); return result.ToHttpResult(); } private static async Task UpdatePlan( Guid id, UpdatePlanBody body, ISender sender, CancellationToken cancellationToken ) { var command = new UpdatePlanCommand( id, body.Name, body.ConfigCount, body.SortOrder, body.IsEnabled ); var result = await sender.Send(command, cancellationToken); return result.ToHttpResult(); } private static async Task DeletePlan( Guid id, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new DeletePlanCommand(id), cancellationToken); return result.ToHttpResult(); } } public sealed record UpdatePlanBody(string Name, int ConfigCount, int SortOrder, bool IsEnabled);