using Indotalent.ConfigBackEnd.Extensions; using Indotalent.Features.Sales.Invoice.Cqrs; using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; namespace Indotalent.Features.Sales.Invoice; public static class InvoiceEndpoint { public static void MapInvoiceEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("/invoice").WithTags("Invoices") .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser()); group.MapGet("/", async (IMediator mediator) => { var result = await mediator.Send(new GetInvoiceListQuery()); return result.ToApiResponse("Invoice list retrieved successfully"); }) .WithName("GetInvoiceList"); group.MapGet("/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new GetInvoiceByIdQuery(id)); return result.ToApiResponse(result is not null ? "Invoice detail retrieved successfully" : $"Invoice with ID {id} not found"); }) .WithName("GetInvoiceById"); group.MapGet("/sales-order-detail/{soId}", async (string soId, IMediator mediator) => { var result = await mediator.Send(new GetSalesOrderDetailForInvoiceQuery(soId)); return result.ToApiResponse(result is not null ? "Sales order detail for invoice retrieved successfully" : $"Sales order with ID {soId} not found"); }) .WithName("GetSalesOrderDetailForInvoice"); group.MapGet("/lookup", async (IMediator mediator) => { var result = await mediator.Send(new GetInvoiceLookupQuery()); return result.ToApiResponse("Lookup data retrieved successfully"); }) .WithName("GetInvoiceLookup"); group.MapPost("/", async (CreateInvoiceRequest request, IMediator mediator) => { var result = await mediator.Send(new CreateInvoiceCommand(request)); return result.ToApiResponse("Invoice has been created successfully", StatusCodes.Status201Created); }) .WithName("CreateInvoice"); group.MapPost("/update", async (UpdateInvoiceRequest request, IMediator mediator) => { var result = await mediator.Send(new UpdateInvoiceCommand(request)); if (!result) { return ((object?)null).ToApiResponse("Update failed. Invoice not found."); } return result.ToApiResponse("Invoice has been updated successfully"); }) .WithName("UpdateInvoice"); group.MapPost("/delete/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new DeleteInvoiceByIdCommand(id)); if (!result) { return ((object?)null).ToApiResponse("Delete failed. Invoice not found."); } return true.ToApiResponse("Invoice has been deleted successfully"); }) .WithName("DeleteInvoiceById"); } }