2026-02-27 17:57:42 +00:00
|
|
|
using IdentityShroud.Core.Contracts;
|
2026-02-08 11:57:57 +01:00
|
|
|
using IdentityShroud.Core.Messages.Realm;
|
2026-02-15 19:06:09 +01:00
|
|
|
using IdentityShroud.Core.Model;
|
2026-02-08 11:57:57 +01:00
|
|
|
using IdentityShroud.Core.Services;
|
2026-02-06 19:58:01 +01:00
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
2026-02-08 11:57:57 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2026-02-06 19:58:01 +01:00
|
|
|
|
|
|
|
|
namespace IdentityShroud.Api;
|
|
|
|
|
|
2026-02-15 19:06:09 +01:00
|
|
|
public static class HttpContextExtensions
|
|
|
|
|
{
|
|
|
|
|
public static Realm GetValidatedRealm(this HttpContext context) => (Realm)context.Items["RealmEntity"]!;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 17:57:42 +00:00
|
|
|
// api: api/v1/realms/{realmId}/....
|
|
|
|
|
// api: api/v1/realms/{realmId}/clients/{clientId}
|
|
|
|
|
|
2026-02-15 19:06:09 +01:00
|
|
|
|
|
|
|
|
|
2026-02-08 18:00:24 +01:00
|
|
|
public static class RealmApi
|
2026-02-06 19:58:01 +01:00
|
|
|
{
|
2026-02-27 17:57:42 +00:00
|
|
|
public static void MapRealmEndpoints(IEndpointRouteBuilder erp)
|
2026-02-06 19:58:01 +01:00
|
|
|
{
|
2026-02-27 17:57:42 +00:00
|
|
|
var realmsGroup = erp.MapGroup("/api/v1/realms");
|
2026-02-08 18:00:24 +01:00
|
|
|
realmsGroup.MapPost("", RealmCreate)
|
|
|
|
|
.Validate<RealmCreateRequest>()
|
|
|
|
|
.WithName("Create Realm")
|
|
|
|
|
.Produces(StatusCodes.Status201Created);
|
2026-02-06 19:58:01 +01:00
|
|
|
|
2026-02-27 17:57:42 +00:00
|
|
|
var realmIdGroup = realmsGroup.MapGroup("{realmId}")
|
|
|
|
|
.AddEndpointFilter<RealmIdValidationFilter>();
|
|
|
|
|
|
|
|
|
|
ClientApi.MapEndpoints(realmIdGroup);
|
|
|
|
|
|
|
|
|
|
|
2026-02-06 19:58:01 +01:00
|
|
|
|
|
|
|
|
}
|
2026-02-08 18:00:24 +01:00
|
|
|
|
|
|
|
|
private static async Task<Results<Created<RealmCreateResponse>, InternalServerError>>
|
|
|
|
|
RealmCreate(RealmCreateRequest request, [FromServices] IRealmService service)
|
|
|
|
|
{
|
|
|
|
|
var response = await service.Create(request);
|
|
|
|
|
if (response.IsSuccess)
|
|
|
|
|
return TypedResults.Created($"/realms/{response.Value.Slug}", response.Value);
|
|
|
|
|
|
|
|
|
|
// TODO make helper to convert failure response to a proper HTTP result.
|
|
|
|
|
return TypedResults.InternalServerError();
|
|
|
|
|
}
|
2026-02-06 19:58:01 +01:00
|
|
|
}
|