using Indotalent.ConfigBackEnd.Extensions; using Indotalent.Features.Inventory.Product.Cqrs; using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; namespace Indotalent.Features.Inventory.Product; public static class ProductEndpoint { public static void MapProductEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("/product").WithTags("Products") .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser()); group.MapGet("/", async (IMediator mediator) => { var result = await mediator.Send(new GetProductListQuery()); return result.ToApiResponse("Product list retrieved successfully"); }) .WithName("GetProductList") .WithTags("Products"); group.MapGet("/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new GetProductByIdQuery(id)); return result.ToApiResponse(result is not null ? "Product detail retrieved successfully" : $"Product with ID {id} not found"); }) .WithName("GetProductById") .WithTags("Products"); group.MapGet("/lookup", async (IMediator mediator) => { var result = await mediator.Send(new LookupProductQuery()); return result.ToApiResponse("Product lookup data retrieved successfully"); }) .WithName("GetProductLookup") .WithTags("Products"); group.MapPost("/", async (CreateProductRequest request, IMediator mediator) => { var result = await mediator.Send(new CreateProductCommand(request)); return result.ToApiResponse("Product has been created successfully", StatusCodes.Status201Created); }) .WithName("CreateProduct") .WithTags("Products"); group.MapPost("/update", async (UpdateProductRequest request, IMediator mediator) => { var result = await mediator.Send(new UpdateProductCommand(request)); if (!result.Success) { return ((object?)null).ToApiResponse("Update failed. The product data could not be found."); } return result.ToApiResponse("Product has been updated successfully"); }) .WithName("UpdateProduct") .WithTags("Products"); group.MapPost("/delete/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new DeleteProductByIdCommand(new DeleteProductByIdRequest(id))); if (!result) { return ((object?)null).ToApiResponse("Delete failed. The product data could not be found."); } return true.ToApiResponse("Product has been deleted successfully"); }) .WithName("DeleteProductById") .WithTags("Products"); } }