2026-02-20 17:35:38 +01:00
|
|
|
using FluentResults;
|
2026-02-22 09:27:48 +01:00
|
|
|
using IdentityShroud.Api.Mappers;
|
2026-02-20 17:35:38 +01:00
|
|
|
using IdentityShroud.Core.Contracts;
|
|
|
|
|
using IdentityShroud.Core.Model;
|
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace IdentityShroud.Api;
|
|
|
|
|
|
|
|
|
|
|
2026-02-22 09:27:48 +01:00
|
|
|
|
2026-02-20 17:35:38 +01:00
|
|
|
public record ClientCreateReponse(int Id, string ClientId);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The part of the api below realms/{slug}/clients
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ClientApi
|
|
|
|
|
{
|
|
|
|
|
public const string ClientGetRouteName = "ClientGet";
|
|
|
|
|
|
|
|
|
|
public static void MapEndpoints(this IEndpointRouteBuilder erp)
|
|
|
|
|
{
|
2026-02-21 20:15:46 +01:00
|
|
|
RouteGroupBuilder clientsGroup = erp.MapGroup("clients");
|
|
|
|
|
|
|
|
|
|
clientsGroup.MapPost("", ClientCreate)
|
2026-02-20 17:35:38 +01:00
|
|
|
.Validate<ClientCreateRequest>()
|
|
|
|
|
.WithName("ClientCreate")
|
|
|
|
|
.Produces(StatusCodes.Status201Created);
|
2026-02-21 20:15:46 +01:00
|
|
|
|
|
|
|
|
var clientIdGroup = clientsGroup.MapGroup("{clientId}")
|
|
|
|
|
.AddEndpointFilter<ClientIdValidationFilter>();
|
|
|
|
|
|
|
|
|
|
clientIdGroup.MapGet("", ClientGet)
|
2026-02-20 17:35:38 +01:00
|
|
|
.WithName(ClientGetRouteName);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 09:27:48 +01:00
|
|
|
private static Ok<ClientRepresentation> ClientGet(
|
|
|
|
|
Guid realmId,
|
|
|
|
|
int clientId,
|
|
|
|
|
HttpContext context)
|
2026-02-20 17:35:38 +01:00
|
|
|
{
|
2026-02-22 09:27:48 +01:00
|
|
|
Client client = (Client)context.Items["ClientEntity"]!;
|
|
|
|
|
return TypedResults.Ok(new ClientMapper().ToDto(client));
|
2026-02-20 17:35:38 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-21 20:15:46 +01:00
|
|
|
private static async Task<Results<CreatedAtRoute<ClientCreateReponse>, InternalServerError>>
|
2026-02-20 17:35:38 +01:00
|
|
|
ClientCreate(
|
2026-02-22 09:27:48 +01:00
|
|
|
Guid realmId,
|
2026-02-20 17:35:38 +01:00
|
|
|
ClientCreateRequest request,
|
|
|
|
|
[FromServices] IClientService service,
|
|
|
|
|
HttpContext context,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Realm realm = context.GetValidatedRealm();
|
|
|
|
|
Result<Client> result = await service.Create(realm.Id, request, cancellationToken);
|
2026-02-21 20:15:46 +01:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
2026-02-20 17:35:38 +01:00
|
|
|
}
|
|
|
|
|
}
|