Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wtmaxim committed Jun 7, 2024
1 parent b059c51 commit df70a46
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FSH.Framework.Core.Identity.Roles.Features.CreateOrUpdateRole;
namespace FSH.Framework.Core.Identity.Roles.Features.CreateOrUpdateRole;

public class CreateOrUpdateRoleCommand
{
public string Name { get; set; }
public string Id { get; set; } = default!;
public string Name { get; set; } = default!;
public string? Description { get; set; }
}

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions api/framework/Core/Identity/Roles/Features/RoleResponse.cs

This file was deleted.

12 changes: 5 additions & 7 deletions api/framework/Core/Identity/Roles/IRoleService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using FSH.Framework.Core.Identity.Roles.Features;
using FSH.Framework.Core.Identity.Roles.Features.CreateOrUpdateRole;
using FSH.Framework.Core.Identity.Roles.Features.DeleteRole;
using FSH.Framework.Core.Identity.Roles.Features.CreateOrUpdateRole;

namespace FSH.Framework.Core.Identity.Roles;

public interface IRoleService
{
Task<IEnumerable<RoleResponse>> GetAllRolesAsync();
Task<RoleResponse?> GetRoleByIdAsync(string id);
Task<RoleResponse> CreateOrUpdateRoleAsync(CreateOrUpdateRoleCommand command);
Task DeleteRoleAsync(DeleteRoleCommand command);
Task<IEnumerable<RoleDto>> GetRolesAsync();
Task<RoleDto?> GetRoleAsync(string id);
Task<RoleDto> CreateOrUpdateRoleAsync(CreateOrUpdateRoleCommand command);
Task DeleteRoleAsync(string id);
}

8 changes: 8 additions & 0 deletions api/framework/Core/Identity/Roles/RoleDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace FSH.Framework.Core.Identity.Roles;

public class RoleDto
{
public string Id { get; set; } = default!;
public string Name { get; set; } = default!;
public string? Description { get; set; }
}
4 changes: 4 additions & 0 deletions api/framework/Infrastructure/Identity/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using FSH.Framework.Infrastructure.Auth;
using FSH.Framework.Infrastructure.Identity.Persistence;
using FSH.Framework.Infrastructure.Identity.Roles;
using FSH.Framework.Infrastructure.Identity.Roles.Endpoints;
using FSH.Framework.Infrastructure.Identity.Tokens;
using FSH.Framework.Infrastructure.Identity.Tokens.Endpoints;
using FSH.Framework.Infrastructure.Identity.Users;
Expand Down Expand Up @@ -51,6 +52,9 @@ public static IEndpointRouteBuilder MapIdentityEndpoints(this IEndpointRouteBuil

var tokens = app.MapGroup("api/token").WithTags("token");
tokens.MapTokenEndpoints();

var roles = app.MapGroup("api/roles").WithTags("roles");
roles.MapRoleEndpoints();
return app;
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FSH.Framework.Core.Identity.Roles;
using FSH.Framework.Core.Identity.Roles;
using FSH.Framework.Core.Identity.Roles.Features.CreateOrUpdateRole;
using FSH.Framework.Infrastructure.Auth.Policy;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace FSH.Framework.Infrastructure.Identity.Roles.Endpoints;

public static class CreateOrUpdateRoleEndpoint
{
public static RouteHandlerBuilder MapCreateOrUpdateRoleEndpoint(this IEndpointRouteBuilder endpoints)
{
return endpoints.MapPost("/api/roles", async (FshRole role, IRoleService roleService) =>
return endpoints.MapPost("/api/roles", async (CreateOrUpdateRoleCommand request, IRoleService roleService) =>
{
var result = await roleService.CreateOrUpdateRoleAsync(role);
return Results.Ok(result);
return await roleService.CreateOrUpdateRoleAsync(request);
})
.WithName("CreateOrUpdateRole")
.WithSummary("Create or update a role")
.RequirePermission("Permissions.Roles.Create")
.WithDescription("Create a new role or update an existing role.");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using FSH.Framework.Core.Identity.Roles;
using FSH.Framework.Infrastructure.Auth.Policy;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace FSH.Framework.Infrastructure.Identity.Roles.Endpoints;
Expand All @@ -11,10 +13,11 @@ public static RouteHandlerBuilder MapDeleteRoleEndpoint(this IEndpointRouteBuild
return endpoints.MapDelete("/api/roles/{id}", async (string id, IRoleService roleService) =>
{
await roleService.DeleteRoleAsync(id);
return Results.NoContent();
})
.WithName("DeleteRole")
.WithSummary("Delete a role by ID")
.RequirePermission("Permissions.Roles.Delete")
.WithDescription("Remove a role from the system by its ID.");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Routing;

namespace FSH.Framework.Infrastructure.Identity.Roles.Endpoints;

internal static class Extensions
{
public static IEndpointRouteBuilder MapRoleEndpoints(this IEndpointRouteBuilder app)
{
app.MapGetRoleEndpoint();
app.MapGetRolesEndpoint();
app.MapDeleteRoleEndpoint();
app.MapCreateOrUpdateRoleEndpoint();
return app;
}
}

Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FSH.Framework.Core.Identity.Roles;
using FSH.Framework.Core.Identity.Roles;
using FSH.Framework.Infrastructure.Auth.Policy;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace FSH.Framework.Infrastructure.Identity.Roles.Endpoints;

public static class GetRoleByIdEndpoint
{
public static RouteHandlerBuilder MapGetRoleByIdEndpoint(this IEndpointRouteBuilder endpoints)
public static RouteHandlerBuilder MapGetRoleEndpoint(this IEndpointRouteBuilder endpoints)
{
return endpoints.MapGet("/api/roles/{id}", async (string id, IRoleService roleService) =>
{
var role = await roleService.GetRoleByIdAsync(id);
return role != null ? Results.Ok(role) : Results.NotFound();
return await roleService.GetRoleAsync(id);
})
.WithName("GetRoleById")
.WithSummary("Get role details by ID")
.RequirePermission("Permissions.Roles.View")
.WithDescription("Retrieve the details of a role by its ID.");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FSH.Framework.Core.Identity.Roles;
using FSH.Framework.Infrastructure.Auth.Policy;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace FSH.Framework.Infrastructure.Identity.Roles.Endpoints;
public static class GetRolesEndpoint
{
public static RouteHandlerBuilder MapGetAllRolesEndpoint(this IEndpointRouteBuilder endpoints)
public static RouteHandlerBuilder MapGetRolesEndpoint(this IEndpointRouteBuilder endpoints)
{
return endpoints.MapGet("/api/roles", async (RoleManager<FshRole> roleManager) =>
return endpoints.MapGet("/api/roles", async (IRoleService roleService) =>
{
var roles = await roleManager.Roles.ToListAsync();
return Results.Ok(roles);
return await roleService.GetRolesAsync();
})
.WithName("GetAllRoles")
.WithSummary("Get a list of all roles")
Expand Down
37 changes: 20 additions & 17 deletions api/framework/Infrastructure/Identity/Roles/RoleService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FSH.Framework.Core.Identity.Roles;
using FSH.Framework.Core.Identity.Roles.Features;
using FSH.Framework.Core.Exceptions;
using FSH.Framework.Core.Identity.Roles;
using FSH.Framework.Core.Identity.Roles.Features.CreateOrUpdateRole;
using FSH.Framework.Core.Identity.Roles.Features.DeleteRole;
using Microsoft.AspNetCore.Identity;

namespace FSH.Framework.Infrastructure.Identity.Roles;
Expand All @@ -15,25 +14,29 @@ public RoleService(RoleManager<FshRole> roleManager)
_roleManager = roleManager;
}

public async Task<IEnumerable<RoleResponse>> GetAllRolesAsync()
public async Task<IEnumerable<RoleDto>> GetRolesAsync()
{
return await Task.Run(() => _roleManager.Roles
.Select(role => new RoleResponse { Id = role.Id, Name = role.Name, Description = role.Description })
.Select(role => new RoleDto { Id = role.Id, Name = role.Name, Description = role.Description })

Check warning on line 20 in api/framework/Infrastructure/Identity/Roles/RoleService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 20 in api/framework/Infrastructure/Identity/Roles/RoleService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
.ToList());
}

public async Task<RoleResponse?> GetRoleByIdAsync(string id)
public async Task<RoleDto?> GetRoleAsync(string id)
{
var role = await _roleManager.FindByIdAsync(id);
return role != null ? new RoleResponse { Id = role.Id, Name = role.Name, Description = role.Description } : null;
FshRole? role = await _roleManager.FindByIdAsync(id);

_ = role ?? throw new NotFoundException("Role Not Found.");

return new RoleDto { Id = role.Id, Name = role.Name, Description = role.Description };

Check warning on line 30 in api/framework/Infrastructure/Identity/Roles/RoleService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 30 in api/framework/Infrastructure/Identity/Roles/RoleService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
}

public async Task<RoleResponse> CreateOrUpdateRoleAsync(CreateOrUpdateRoleCommand command)
public async Task<RoleDto> CreateOrUpdateRoleAsync(CreateOrUpdateRoleCommand command)
{
var role = await _roleManager.FindByNameAsync(command.Name);
FshRole? role = await _roleManager.FindByIdAsync(command.Id);

if (role != null)
{
role.Name = command.Name;
role.Description = command.Description;
await _roleManager.UpdateAsync(role);
}
Expand All @@ -43,15 +46,15 @@ public async Task<RoleResponse> CreateOrUpdateRoleAsync(CreateOrUpdateRoleComman
await _roleManager.CreateAsync(role);
}

return new RoleResponse { Id = role.Id, Name = role.Name, Description = role.Description };
return new RoleDto { Id = role.Id, Name = role.Name, Description = role.Description };

Check warning on line 49 in api/framework/Infrastructure/Identity/Roles/RoleService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 49 in api/framework/Infrastructure/Identity/Roles/RoleService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
}

public async Task DeleteRoleAsync(DeleteRoleCommand command)
public async Task DeleteRoleAsync(string id)
{
var role = await _roleManager.FindByIdAsync(command.Id);
if (role != null)
{
await _roleManager.DeleteAsync(role);
}
FshRole? role = await _roleManager.FindByIdAsync(id);

_ = role ?? throw new NotFoundException("Role Not Found.");

await _roleManager.DeleteAsync(role);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FSH.Framework.Core.Exceptions;
using System.Security.Claims;
using FSH.Framework.Core.Identity.Users.Abstractions;
using FSH.Framework.Infrastructure.Auth.Policy;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
Expand Down

0 comments on commit df70a46

Please sign in to comment.