Add UserRole handling
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Imprink.Domain.Repositories;
|
||||
using Imprink.Domain.Repositories.Products;
|
||||
using Imprink.Domain.Repositories.Users;
|
||||
|
||||
namespace Imprink.Application;
|
||||
|
||||
|
||||
7
src/Imprink.Application/Users/Dtos/RoleDto.cs
Normal file
7
src/Imprink.Application/Users/Dtos/RoleDto.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Imprink.Application.Users.Dtos;
|
||||
|
||||
public class RoleDto
|
||||
{
|
||||
public required Guid RoleId { get; set; }
|
||||
public required string RoleName { get; set; }
|
||||
}
|
||||
7
src/Imprink.Application/Users/Dtos/UserRoleDto.cs
Normal file
7
src/Imprink.Application/Users/Dtos/UserRoleDto.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Imprink.Application.Users.Dtos;
|
||||
|
||||
public class UserRoleDto
|
||||
{
|
||||
public required string UserId { get; set; }
|
||||
public required Guid RoleId { get; set; }
|
||||
}
|
||||
@@ -1,15 +1,22 @@
|
||||
using Imprink.Domain.Entities.Users;
|
||||
using Imprink.Application.Users.Dtos;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Users;
|
||||
|
||||
public record GetUserRolesCommand(string Sub) : IRequest<IEnumerable<Role>>;
|
||||
public record GetUserRolesCommand(string Sub) : IRequest<IEnumerable<RoleDto>>;
|
||||
|
||||
public class GetUserRolesHandler(IUnitOfWork uw): IRequestHandler<GetUserRolesCommand, IEnumerable<Role>>
|
||||
public class GetUserRolesHandler(IUnitOfWork uw): IRequestHandler<GetUserRolesCommand, IEnumerable<RoleDto>>
|
||||
{
|
||||
public async Task<IEnumerable<Role>> Handle(GetUserRolesCommand request, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<RoleDto>> Handle(GetUserRolesCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken)) return [];
|
||||
return await uw.UserRoleRepository.GetUserRolesAsync(request.Sub, cancellationToken);;
|
||||
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken)) return [];
|
||||
|
||||
var roles = await uw.UserRoleRepository.GetUserRolesAsync(request.Sub, cancellationToken);
|
||||
|
||||
return roles.Select(role => new RoleDto
|
||||
{
|
||||
RoleId = role.Id,
|
||||
RoleName = role.RoleName
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
using Imprink.Domain.Entities.Users;
|
||||
using Imprink.Domain.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Users.Roles;
|
||||
|
||||
public record AddUserToRoleCommand(string UserId, Guid RoleId) : IRequest<bool>;
|
||||
|
||||
public class AddUserToRoleCommandHandler(
|
||||
IUserRoleRepository userRoleRepository,
|
||||
IRoleRepository roleRepository,
|
||||
IUserRepository userRepository)
|
||||
: IRequestHandler<AddUserToRoleCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(AddUserToRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var userExists = await userRepository.UserExistsAsync(request.UserId, cancellationToken);
|
||||
if (!userExists)
|
||||
return false;
|
||||
|
||||
var roleExists = await roleRepository.RoleExistsAsync(request.RoleId, cancellationToken);
|
||||
if (!roleExists)
|
||||
return false;
|
||||
|
||||
var isAlreadyInRole = await userRoleRepository.IsUserInRoleAsync(request.UserId, request.RoleId, cancellationToken);
|
||||
if (isAlreadyInRole)
|
||||
return true;
|
||||
|
||||
var userRole = new UserRole
|
||||
{
|
||||
UserId = request.UserId,
|
||||
RoleId = request.RoleId
|
||||
};
|
||||
|
||||
await userRoleRepository.AddUserRoleAsync(userRole, cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
using Imprink.Domain.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Users.Roles;
|
||||
|
||||
public record RemoveUserFromRoleCommand(string UserId, Guid RoleId) : IRequest<bool>;
|
||||
|
||||
public class RemoveUserFromRoleCommandHandler(
|
||||
IUserRoleRepository userRoleRepository,
|
||||
IRoleRepository roleRepository,
|
||||
IUserRepository userRepository)
|
||||
: IRequestHandler<RemoveUserFromRoleCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(RemoveUserFromRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var userExists = await userRepository.UserExistsAsync(request.UserId, cancellationToken);
|
||||
if (!userExists)
|
||||
return false;
|
||||
|
||||
var roleExists = await roleRepository.RoleExistsAsync(request.RoleId, cancellationToken);
|
||||
if (!roleExists)
|
||||
return false;
|
||||
|
||||
var userRole = await userRoleRepository.GetUserRoleAsync(request.UserId, request.RoleId, cancellationToken);
|
||||
if (userRole == null)
|
||||
return true;
|
||||
|
||||
await userRoleRepository.RemoveUserRoleAsync(userRole, cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
29
src/Imprink.Application/Users/SetUserRoleHandler.cs
Normal file
29
src/Imprink.Application/Users/SetUserRoleHandler.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Imprink.Application.Users.Dtos;
|
||||
using Imprink.Domain.Entities.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Users;
|
||||
|
||||
public record SetUserRoleCommand(string Sub, Guid RoleId) : IRequest<UserRoleDto?>;
|
||||
|
||||
public class SetUserRoleHandler(IUnitOfWork uw) : IRequestHandler<SetUserRoleCommand, UserRoleDto?>
|
||||
{
|
||||
public async Task<UserRoleDto?> Handle(SetUserRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken)) return null;
|
||||
|
||||
var userRole = new UserRole
|
||||
{
|
||||
UserId = request.Sub,
|
||||
RoleId = request.RoleId
|
||||
};
|
||||
|
||||
var addedRole = await uw.UserRoleRepository.AddUserRoleAsync(userRole, cancellationToken);
|
||||
|
||||
return new UserRoleDto
|
||||
{
|
||||
UserId = addedRole.UserId,
|
||||
RoleId = addedRole.RoleId
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user