Skip to content

Commit

Permalink
Moved roles to Immediate.Apis
Browse files Browse the repository at this point in the history
- Role order is now non-nullable and defaults to `0`
- Added more constraints on properties of Faq and OgmaRole
  • Loading branch information
Atulin committed Sep 6, 2024
1 parent 0339010 commit 1940225
Show file tree
Hide file tree
Showing 28 changed files with 2,884 additions and 543 deletions.
47 changes: 0 additions & 47 deletions Ogma3/Api/V1/Roles/Commands/CreateRole.cs

This file was deleted.

29 changes: 0 additions & 29 deletions Ogma3/Api/V1/Roles/Commands/DeleteRole.cs

This file was deleted.

47 changes: 0 additions & 47 deletions Ogma3/Api/V1/Roles/Commands/UpdateRole.cs

This file was deleted.

46 changes: 46 additions & 0 deletions Ogma3/Api/V1/Roles/CreateRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using FluentValidation;
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Identity;
using Ogma3.Data.Roles;
using Ogma3.Infrastructure.ServiceRegistrations;

namespace Ogma3.Api.V1.Roles;

using ReturnType = Results<Conflict<string>, CreatedAtRoute<RoleDto>>;

[Handler]
[MapPost("api/roles")]
[Authorize(Policy = AuthorizationPolicies.RequireAdminRole)]
public static partial class CreateRole
{
public sealed record Command(string Name, bool IsStaff, string Color, byte Order);

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator() => RuleFor(r => r.Name).NotEmpty();
}

private static async ValueTask<ReturnType> HandleAsync(
Command request,
RoleManager<OgmaRole> roleManager,
CancellationToken _
)
{
if (await roleManager.RoleExistsAsync(request.Name)) return TypedResults.Conflict($"Role {request.Name} already exists");

var role = new OgmaRole
{
Name = request.Name,
IsStaff = request.IsStaff,
Color = request.Color,
Order = request.Order,
};

await roleManager.CreateAsync(role);

return TypedResults.CreatedAtRoute(role.ToDto(), nameof(GetRoleById), new GetRoleById.Query(role.Id));
}
}
34 changes: 34 additions & 0 deletions Ogma3/Api/V1/Roles/DeleteRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Identity;
using Ogma3.Data.Roles;
using Ogma3.Infrastructure.ServiceRegistrations;

namespace Ogma3.Api.V1.Roles;

using ReturnType = Results<Ok<long>, NotFound>;

[Handler]
[MapDelete("api/roles")]
[Authorize(Policy = AuthorizationPolicies.RequireAdminRole)]
public static partial class DeleteRole
{
public sealed record Command(long RoleId);

private static async ValueTask<ReturnType> HandleAsync(
Command request,
RoleManager<OgmaRole> roleManager,
CancellationToken _
)
{
var role = await roleManager.FindByIdAsync(request.RoleId.ToString());

if (role is null) return TypedResults.NotFound();

await roleManager.DeleteAsync(role);

return TypedResults.Ok(role.Id);
}
}
29 changes: 29 additions & 0 deletions Ogma3/Api/V1/Roles/GetAllRoles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Ogma3.Data;
using Ogma3.Data.Roles;

namespace Ogma3.Api.V1.Roles;

[Handler]
[MapGet("api/roles")]
public static partial class GetAllRoles
{
public sealed record Query;

private static async ValueTask<Ok<RoleDto[]>> HandleAsync(
Query _,
ApplicationDbContext context,
CancellationToken cancellationToken
)
{
var roles = await context.Roles
.OrderByDescending(r => r.Order)
.ProjectToDto()
.ToArrayAsync(cancellationToken);

return TypedResults.Ok(roles);
}
}
29 changes: 29 additions & 0 deletions Ogma3/Api/V1/Roles/GetRoleById.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Ogma3.Data;
using Ogma3.Data.Roles;

namespace Ogma3.Api.V1.Roles;

using ReturnType = Results<Ok<RoleDto>, NotFound>;

[Handler]
[MapGet("api/roles/{roleId:long}")]
public static partial class GetRoleById
{
internal static void CustomizeEndpoint(IEndpointConventionBuilder endpoint) => endpoint.WithName(nameof(GetRoleById));

public sealed record Query(long RoleId);

private static async ValueTask<ReturnType> HandleAsync(Query request, ApplicationDbContext context, CancellationToken cancellationToken)
{
var role = await context.Roles
.Where(r => r.Id == request.RoleId)
.ProjectToDto()
.FirstOrDefaultAsync(cancellationToken);

return role is null ? TypedResults.NotFound() : TypedResults.Ok(role);
}
}
30 changes: 0 additions & 30 deletions Ogma3/Api/V1/Roles/Queries/GetAllRoles.cs

This file was deleted.

28 changes: 0 additions & 28 deletions Ogma3/Api/V1/Roles/Queries/GetRoleById.cs

This file was deleted.

45 changes: 0 additions & 45 deletions Ogma3/Api/V1/Roles/RolesController.cs

This file was deleted.

Loading

0 comments on commit 1940225

Please sign in to comment.