using PostFundManagement.Application.Abstractions.Services; using PostFundManagement.Application.Extensions; using PostFundManagement.Domain.Abstractions; using PostFundManagement.Domain.Errors; using PostFundManagement.Domain.Extensions; using PostFundManagement.Domain.Models.Security; namespace PostFundManagement.Api.Endpoints.Users; [ApiVersionTarget(1)] public sealed class CreateUserEndpoint : IEndpoint { public void Map(IEndpointRouteBuilder builder) { builder.MapPost("/api/users/{tenantId}/{idNumber}", async (string tenantId, string idNumber, CreateUserRequest request, HttpContext context, IUserService userService, CancellationToken cancellationToken = default) => await userService.CreateUserAsync(tenantId, idNumber, request, context.GetUserId(), cancellationToken) switch { { IsSuccess: true } => Results.Ok(), var r when r.HasError() => Results.Forbid(), var r when r.HasError() => Results.BadRequest(r.Errors.ToProblems()), var r when r.HasError() => Results.BadRequest(r.Errors.ToProblems()), var r => Results.InternalServerError(r.Errors.ToProblems()), }) .WithDescription("Creates a new user in the system") .RequireAuthorization(Constants.MainScope) .WithName(typeof(CreateUserEndpoint).ToEndpointName()) .MapToApiVersion(new ApiVersion(1)) .Produces(StatusCodes.Status200OK) .Produces(StatusCodes.Status404NotFound) .Produces(StatusCodes.Status401Unauthorized) .Produces(StatusCodes.Status403Forbidden) .Produces(StatusCodes.Status400BadRequest) .Produces(StatusCodes.Status500InternalServerError) .WithTags(EndpointTags.Users); } }