using Indotalent.ConfigBackEnd.Extensions; using Indotalent.Features.Performance.Promotion.Cqrs; using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; namespace Indotalent.Features.Performance.Promotion; public static class PromotionEndpoint { public static void MapPromotionEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("/performance-promotion").WithTags("Performance Promotions") .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser()); group.MapGet("/", async (IMediator mediator) => { var result = await mediator.Send(new GetPromotionListQuery()); return result.ToApiResponse("Promotion list retrieved successfully"); }) .WithName("GetPromotionList"); group.MapGet("/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new GetPromotionByIdQuery(id)); return result.ToApiResponse(result is not null ? "Promotion detail retrieved successfully" : $"Promotion with ID {id} not found"); }) .WithName("GetPromotionById"); group.MapPost("/", async (CreatePromotionRequest request, IMediator mediator) => { var result = await mediator.Send(new CreatePromotionCommand(request)); return result.ToApiResponse("Promotion has been created successfully", StatusCodes.Status201Created); }) .WithName("CreatePromotion"); group.MapPost("/update", async (UpdatePromotionRequest request, IMediator mediator) => { var result = await mediator.Send(new UpdatePromotionCommand(request)); if (!result.Success) return ((object?)null).ToApiResponse("Update failed."); return result.ToApiResponse("Promotion has been updated successfully"); }) .WithName("UpdatePromotion"); group.MapPost("/delete/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new DeletePromotionByIdCommand(new DeletePromotionByIdRequest(id))); if (!result) return ((object?)null).ToApiResponse("Delete failed."); return true.ToApiResponse("Promotion has been deleted successfully"); }) .WithName("DeletePromotionById"); } }