ref: project structure
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.application.Permissions;
|
||||
|
||||
namespace mws.backend.dotnet.application.Users;
|
||||
|
||||
public record UpdateUserCommand(Guid ActorUserId, Guid Id, UpdateUserRequest Request) : IRequest<UserDto>;
|
||||
|
||||
public class UpdateUserHandler(IUnitOfWork uow, IPermissionService permissions, IMapper mapper)
|
||||
: IRequestHandler<UpdateUserCommand, UserDto>
|
||||
{
|
||||
private const string Screen = "users";
|
||||
|
||||
public async Task<UserDto> Handle(UpdateUserCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Edit, ct);
|
||||
|
||||
var user = await uow.Users.GetByIdWithRoleAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("User not found");
|
||||
|
||||
var request = command.Request;
|
||||
if (request.RoleId.HasValue)
|
||||
{
|
||||
var role = await uow.Roles.GetByIdAsync(request.RoleId.Value, ct)
|
||||
?? throw new BadRequestException("Role not found");
|
||||
}
|
||||
|
||||
user.DisplayName = request.DisplayName.Trim();
|
||||
user.IsActive = request.IsActive;
|
||||
user.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return mapper.Map<UserDto>(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user