-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Role order is now non-nullable and defaults to `0` - Added more constraints on properties of Faq and OgmaRole
- Loading branch information
Showing
28 changed files
with
2,884 additions
and
543 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.