using PnvPanel.Api.Common; using PnvPanel.Application.Admin.News; using LiteCqrs; using PnvPanel.Application.Common.Models; using PnvPanel.Application.News; using PnvPanel.Infrastructure.Identity; namespace PnvPanel.Api.Endpoints; public static class AdminNewsEndpoints { public static IEndpointRouteBuilder MapAdminNewsEndpoints(this IEndpointRouteBuilder app) { var admin = app.MapGroup("/api/admin/news") .WithTags("Admin.News") .RequireAuthorization(policy => policy.RequireRole(RoleNames.Admin)); admin.MapGet("", ListAdminNews).Produces>(); admin.MapPost("", CreatePost).Produces(); admin.MapPut("/{id:guid}", UpdatePost).Produces(); admin.MapDelete("/{id:guid}", DeletePost).Produces(StatusCodes.Status204NoContent); return app; } private static async Task ListAdminNews( int page, int pageSize, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new ListAdminNewsQuery(page, pageSize), cancellationToken); return result.ToHttpResult(); } private static async Task CreatePost( CreatePostCommand command, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(command, cancellationToken); return result.ToHttpResult(); } private static async Task UpdatePost( Guid id, UpdatePostBody body, ISender sender, CancellationToken cancellationToken ) { var command = new UpdatePostCommand(id, body.Title, body.Body); var result = await sender.Send(command, cancellationToken); return result.ToHttpResult(); } private static async Task DeletePost( Guid id, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new DeletePostCommand(id), cancellationToken); return result.ToHttpResult(); } } public sealed record UpdatePostBody(string Title, string Body);