|
| 1 | +using ECommerceAPI.Resource; |
| 2 | +using ECommerceApplication.Request; |
| 3 | +using ECommerceApplication.UseCase; |
| 4 | +using ECommerceInfrastructure.Auth.Identities; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using System.Security.Claims; |
| 7 | + |
| 8 | +namespace ECommerceAPI; |
| 9 | + |
| 10 | +internal static class Endpoint |
| 11 | +{ |
| 12 | + public static void Configure(IEndpointRouteBuilder app) |
| 13 | + { |
| 14 | + app.MapGet("/api/v1/customers", async ( |
| 15 | + [FromServices] FindOneCustomer useCase, |
| 16 | + ClaimsPrincipal user) => |
| 17 | + { |
| 18 | + var customer = new CustomerIdentity(user); |
| 19 | + var customerDto = await useCase.Execute(new(Id: customer.Id)); |
| 20 | + return Results.Ok(new CustomerResource(customerDto)); |
| 21 | + }) |
| 22 | + .RequireAuthorization() |
| 23 | + .WithSummary("Find") |
| 24 | + .WithOpenApi() |
| 25 | + .Produces(200, typeof(CustomerResource)); |
| 26 | + |
| 27 | + app.MapPost("/api/v1/customers", async ( |
| 28 | + [FromServices] RegisterCustomer useCase, |
| 29 | + [FromBody] CreateCustomerRequest request) => |
| 30 | + { |
| 31 | + await useCase.Execute(request); |
| 32 | + return Results.Created(); |
| 33 | + }) |
| 34 | + .AllowAnonymous() |
| 35 | + .WithSummary("Register") |
| 36 | + .WithOpenApi() |
| 37 | + .Produces(201); |
| 38 | + |
| 39 | + app.MapPatch("/api/v1/customers", async ( |
| 40 | + [FromServices] UpdateCustomerNameAndEmail useCase, |
| 41 | + [FromBody] UpdateCustomerRequest request, |
| 42 | + ClaimsPrincipal user) => |
| 43 | + { |
| 44 | + var customer = new CustomerIdentity(user); |
| 45 | + request.Id = customer.Id; |
| 46 | + await useCase.Execute(request); |
| 47 | + return Results.NoContent(); |
| 48 | + }) |
| 49 | + .RequireAuthorization() |
| 50 | + .WithSummary("Update") |
| 51 | + .WithOpenApi() |
| 52 | + .Produces(204); |
| 53 | + |
| 54 | + app.MapDelete("/api/v1/customers", async ( |
| 55 | + [FromServices] RemoveCustomer useCase, |
| 56 | + ClaimsPrincipal user) => |
| 57 | + { |
| 58 | + var customer = new CustomerIdentity(user); |
| 59 | + await useCase.Execute(new(Id: customer.Id)); |
| 60 | + return Results.NoContent(); |
| 61 | + }) |
| 62 | + .RequireAuthorization() |
| 63 | + .WithSummary("Remove") |
| 64 | + .WithOpenApi() |
| 65 | + .Produces(204); |
| 66 | + } |
| 67 | +} |
0 commit comments