using Indotalent.ConfigBackEnd.Extensions; using Indotalent.Features.Payroll.Income.Cqrs; using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; namespace Indotalent.Features.Payroll.Income; public static class IncomeEndpoint { public static void MapIncomeEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("/income").WithTags("Incomes") .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser()); group.MapGet("/", async (IMediator mediator) => { var result = await mediator.Send(new GetIncomeListQuery()); return result.ToApiResponse("Income components retrieved successfully"); }) .WithName("GetIncomeList") .WithTags("Incomes"); group.MapGet("/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new GetIncomeByIdQuery(id)); return result.ToApiResponse(result is not null ? "Income component detail retrieved successfully" : $"Income component with ID {id} not found"); }) .WithName("GetIncomeById") .WithTags("Incomes"); group.MapPost("/", async (CreateIncomeRequest request, IMediator mediator) => { var result = await mediator.Send(new CreateIncomeCommand(request)); return result.ToApiResponse("Income component has been created successfully", StatusCodes.Status201Created); }) .WithName("CreateIncome") .WithTags("Incomes"); group.MapPost("/update", async (UpdateIncomeRequest request, IMediator mediator) => { var result = await mediator.Send(new UpdateIncomeCommand(request)); if (!result.Success) { return ((object?)null).ToApiResponse("Update failed. The income data could not be found."); } return result.ToApiResponse("Income component has been updated successfully"); }) .WithName("UpdateIncome") .WithTags("Incomes"); group.MapPost("/delete/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new DeleteIncomeByIdCommand(new DeleteIncomeByIdRequest(id))); if (!result) { return ((object?)null).ToApiResponse("Delete failed. The income data could not be found."); } return true.ToApiResponse("Income component has been deleted successfully"); }) .WithName("DeleteIncomeById") .WithTags("Incomes"); } }