using FluentResults; using IdentityShroud.Api.Mappers; using IdentityShroud.Core.Contracts; using IdentityShroud.Core.Model; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; namespace IdentityShroud.Api; public record ClientCreateReponse(int Id, string ClientId); /// /// The part of the api below realms/{slug}/clients /// public static class ClientApi { public const string ClientGetRouteName = "ClientGet"; public static void MapEndpoints(this IEndpointRouteBuilder erp) { RouteGroupBuilder clientsGroup = erp.MapGroup("clients"); clientsGroup.MapPost("", ClientCreate) .Validate() .WithName("ClientCreate") .Produces(StatusCodes.Status201Created); var clientIdGroup = clientsGroup.MapGroup("{clientId}") .AddEndpointFilter(); clientIdGroup.MapGet("", ClientGet) .WithName(ClientGetRouteName); } private static Ok ClientGet( Guid realmId, int clientId, HttpContext context) { Client client = (Client)context.Items["ClientEntity"]!; return TypedResults.Ok(new ClientMapper().ToDto(client)); } private static async Task, InternalServerError>> ClientCreate( Guid realmId, ClientCreateRequest request, [FromServices] IClientService service, HttpContext context, CancellationToken cancellationToken) { Realm realm = context.GetValidatedRealm(); Result result = await service.Create(realm.Id, request, cancellationToken); if (result.IsFailed) { throw new NotImplementedException(); } Client client = result.Value; return TypedResults.CreatedAtRoute( new ClientCreateReponse(client.Id, client.ClientId), ClientGetRouteName, new RouteValueDictionary() { ["realmId"] = realm.Id, ["clientId"] = client.Id, }); } }