Add user and role management features
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
namespace Distribution.Features.Role;
|
||||
|
||||
public interface IRoleRepository
|
||||
{
|
||||
Task<Models.Role?> GetByIdAsync(Guid id);
|
||||
Task<List<Models.Role>> GetAllAsync();
|
||||
Task<List<Models.RolePermission>> GetPermissionsAsync(Guid roleId);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Distribution.Features.Role;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/roles")]
|
||||
[Authorize]
|
||||
public class RoleController(RoleService service) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll() =>
|
||||
Ok(await service.GetAllAsync());
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<IActionResult> GetById(Guid id)
|
||||
{
|
||||
var result = await service.GetByIdAsync(id);
|
||||
return result is null ? NotFound() : Ok(result);
|
||||
}
|
||||
|
||||
[HttpGet("{id:guid}/permissions")]
|
||||
public async Task<IActionResult> GetPermissions(Guid id)
|
||||
{
|
||||
var result = await service.GetPermissionsAsync(id);
|
||||
return result is null ? NotFound() : Ok(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Distribution.Features.Role;
|
||||
|
||||
public record RoleResponse(Guid Id, string Name, int Level, bool IsActive, DateTime CreatedAt);
|
||||
|
||||
public record RolePermissionResponse(Guid PermissionId, string Code, string Description);
|
||||
@@ -0,0 +1,19 @@
|
||||
using Distribution.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Distribution.Features.Role;
|
||||
|
||||
public class RoleRepository(AppDBContext context) : IRoleRepository
|
||||
{
|
||||
public async Task<Models.Role?> GetByIdAsync(Guid id) =>
|
||||
await context.Roles.FindAsync(id);
|
||||
|
||||
public async Task<List<Models.Role>> GetAllAsync() =>
|
||||
await context.Roles.ToListAsync();
|
||||
|
||||
public async Task<List<Models.RolePermission>> GetPermissionsAsync(Guid roleId) =>
|
||||
await context.RolePermissions
|
||||
.Include(rp => rp.Permission)
|
||||
.Where(rp => rp.FK_RoleId == roleId && rp.IsActive)
|
||||
.ToListAsync();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace Distribution.Features.Role;
|
||||
|
||||
public class RoleService(IRoleRepository repo)
|
||||
{
|
||||
public async Task<List<RoleResponse>> GetAllAsync() =>
|
||||
(await repo.GetAllAsync())
|
||||
.Where(r => r.IsActive)
|
||||
.Select(r => new RoleResponse(r.Id, r.Name, r.Level, r.IsActive, r.CreatedAt))
|
||||
.ToList();
|
||||
|
||||
public async Task<RoleResponse?> GetByIdAsync(Guid id)
|
||||
{
|
||||
var role = await repo.GetByIdAsync(id);
|
||||
if (role is null || !role.IsActive) return null;
|
||||
return new RoleResponse(role.Id, role.Name, role.Level, role.IsActive, role.CreatedAt);
|
||||
}
|
||||
|
||||
public async Task<List<RolePermissionResponse>?> GetPermissionsAsync(Guid roleId)
|
||||
{
|
||||
var role = await repo.GetByIdAsync(roleId);
|
||||
if (role is null || !role.IsActive) return null;
|
||||
return (await repo.GetPermissionsAsync(roleId))
|
||||
.Select(rp => new RolePermissionResponse(rp.FK_PermissionId, rp.Permission.Code, rp.Permission.Description))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user