73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
|
|
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);
|
||
|
|
|
||
|
|
/// <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)
|
||
|
|
{
|
||
|
|
RouteGroupBuilder clientsGroup = erp.MapGroup("clients");
|
||
|
|
|
||
|
|
clientsGroup.MapPost("", ClientCreate)
|
||
|
|
.Validate<ClientCreateRequest>()
|
||
|
|
.WithName("ClientCreate")
|
||
|
|
.Produces(StatusCodes.Status201Created);
|
||
|
|
|
||
|
|
var clientIdGroup = clientsGroup.MapGroup("{clientId}")
|
||
|
|
.AddEndpointFilter<ClientIdValidationFilter>();
|
||
|
|
|
||
|
|
clientIdGroup.MapGet("", ClientGet)
|
||
|
|
.WithName(ClientGetRouteName);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static Ok<ClientRepresentation> ClientGet(
|
||
|
|
Guid realmId,
|
||
|
|
int clientId,
|
||
|
|
HttpContext context)
|
||
|
|
{
|
||
|
|
Client client = (Client)context.Items["ClientEntity"]!;
|
||
|
|
return TypedResults.Ok(new ClientMapper().ToDto(client));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static async Task<Results<CreatedAtRoute<ClientCreateReponse>, InternalServerError>>
|
||
|
|
ClientCreate(
|
||
|
|
Guid realmId,
|
||
|
|
ClientCreateRequest request,
|
||
|
|
[FromServices] IClientService service,
|
||
|
|
HttpContext context,
|
||
|
|
CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
Realm realm = context.GetValidatedRealm();
|
||
|
|
Result<Client> 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,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|