using Indotalent.ConfigBackEnd.Extensions; using Indotalent.Features.Payroll.Deduction.Cqrs; using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; namespace Indotalent.Features.Payroll.Deduction; public static class DeductionEndpoint { public static void MapDeductionEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("/deduction").WithTags("Deductions") .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser()); group.MapGet("/", async (IMediator mediator) => { var result = await mediator.Send(new GetDeductionListQuery()); return result.ToApiResponse("Deduction components retrieved successfully"); }) .WithName("GetDeductionList") .WithTags("Deductions"); group.MapGet("/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new GetDeductionByIdQuery(id)); return result.ToApiResponse(result is not null ? "Deduction component detail retrieved successfully" : $"Deduction component with ID {id} not found"); }) .WithName("GetDeductionById") .WithTags("Deductions"); group.MapPost("/", async (CreateDeductionRequest request, IMediator mediator) => { var result = await mediator.Send(new CreateDeductionCommand(request)); return result.ToApiResponse("Deduction component has been created successfully", StatusCodes.Status201Created); }) .WithName("CreateDeduction") .WithTags("Deductions"); group.MapPost("/update", async (UpdateDeductionRequest request, IMediator mediator) => { var result = await mediator.Send(new UpdateDeductionCommand(request)); if (!result.Success) { return ((object?)null).ToApiResponse("Update failed. The deduction data could not be found."); } return result.ToApiResponse("Deduction component has been updated successfully"); }) .WithName("UpdateDeduction") .WithTags("Deductions"); group.MapPost("/delete/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new DeleteDeductionByIdCommand(new DeleteDeductionByIdRequest(id))); if (!result) { return ((object?)null).ToApiResponse("Delete failed. The deduction data could not be found."); } return true.ToApiResponse("Deduction component has been deleted successfully"); }) .WithName("DeleteDeductionById") .WithTags("Deductions"); } }