Convert Accounts module to MediatR commands/queries
Replaces IAccountService/AccountService with one command/query per operation, following the pattern set in the Auth module. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,116 +0,0 @@
|
||||
using AutoMapper;
|
||||
using Mws.Application.Auth;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Application.Permissions;
|
||||
using Mws.Domain.Users;
|
||||
|
||||
namespace Mws.Application.Accounts;
|
||||
|
||||
public class AccountService(IUnitOfWork uow, IPasswordHasher passwordHasher, IPermissionService permissions, IMapper mapper) : IAccountService
|
||||
{
|
||||
private const string Screen = "accounts";
|
||||
|
||||
public async Task<List<AccountDto>> GetAccountsAsync(Guid actorUserId, string? term, CancellationToken ct = default)
|
||||
{
|
||||
await permissions.EnsureAsync(actorUserId, Screen, PermissionAction.View, ct);
|
||||
|
||||
var users = await uow.Users.SearchWithRoleAsync(term, null, ct);
|
||||
return mapper.Map<List<AccountDto>>(users);
|
||||
}
|
||||
|
||||
public async Task<AccountDto> CreateAccountAsync(Guid actorUserId, CreateAccountRequest request, CancellationToken ct = default)
|
||||
{
|
||||
await permissions.EnsureAsync(actorUserId, Screen, PermissionAction.Create, ct);
|
||||
|
||||
var username = request.Username.Trim();
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(request.Password))
|
||||
{
|
||||
throw new BadRequestException("Username and password are required");
|
||||
}
|
||||
|
||||
if (await uow.Users.ExistsByUsernameAsync(username, ct))
|
||||
{
|
||||
throw new BadRequestException("Username already exists");
|
||||
}
|
||||
|
||||
var role = await uow.Roles.GetByIdAsync(request.RoleId, ct)
|
||||
?? throw new BadRequestException("Role not found");
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Username = username,
|
||||
PasswordHash = passwordHasher.Hash(request.Password),
|
||||
DisplayName = request.DisplayName.Trim(),
|
||||
RoleId = role.Id,
|
||||
IsActive = true,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
};
|
||||
|
||||
uow.Users.Add(user);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
user.Role = role;
|
||||
return mapper.Map<AccountDto>(user);
|
||||
}
|
||||
|
||||
public async Task<AccountDto> UpdateAccountAsync(Guid actorUserId, Guid id, UpdateAccountRequest request, CancellationToken ct = default)
|
||||
{
|
||||
await permissions.EnsureAsync(actorUserId, Screen, PermissionAction.Edit, ct);
|
||||
|
||||
var user = await uow.Users.GetByIdWithRoleAsync(id, ct)
|
||||
?? throw new NotFoundException("Account not found");
|
||||
|
||||
var role = await uow.Roles.GetByIdAsync(request.RoleId, ct)
|
||||
?? throw new BadRequestException("Role not found");
|
||||
|
||||
user.DisplayName = request.DisplayName.Trim();
|
||||
user.RoleId = role.Id;
|
||||
user.Role = role;
|
||||
user.IsActive = request.IsActive;
|
||||
user.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return mapper.Map<AccountDto>(user);
|
||||
}
|
||||
|
||||
public async Task DeleteAccountAsync(Guid actorUserId, Guid id, CancellationToken ct = default)
|
||||
{
|
||||
await permissions.EnsureAsync(actorUserId, Screen, PermissionAction.Delete, ct);
|
||||
|
||||
var user = await uow.Users.GetByIdAsync(id, ct)
|
||||
?? throw new NotFoundException("Account not found");
|
||||
|
||||
var soleOwnerProjectIds = await uow.Projects.GetOwnedProjectIdsAsync(id, ct);
|
||||
|
||||
foreach (var projectId in soleOwnerProjectIds)
|
||||
{
|
||||
var ownerCount = await uow.Projects.CountOwnersAsync(projectId, ct);
|
||||
if (ownerCount <= 1)
|
||||
{
|
||||
throw new BadRequestException("Cannot delete an account that is the sole owner of a project");
|
||||
}
|
||||
}
|
||||
|
||||
uow.Users.Remove(user);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task ResetPasswordAsync(Guid actorUserId, Guid id, ResetPasswordRequest request, CancellationToken ct = default)
|
||||
{
|
||||
await permissions.EnsureAsync(actorUserId, Screen, PermissionAction.Edit, ct);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.NewPassword))
|
||||
{
|
||||
throw new BadRequestException("New password is required");
|
||||
}
|
||||
|
||||
var user = await uow.Users.GetByIdAsync(id, ct)
|
||||
?? throw new NotFoundException("Account not found");
|
||||
|
||||
user.PasswordHash = passwordHasher.Hash(request.NewPassword);
|
||||
user.UpdatedAt = DateTime.UtcNow;
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Mws.Application.Auth;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Application.Permissions;
|
||||
using Mws.Domain.Users;
|
||||
|
||||
namespace Mws.Application.Accounts;
|
||||
|
||||
public record CreateAccountCommand(Guid ActorUserId, CreateAccountRequest Request) : IRequest<AccountDto>;
|
||||
|
||||
public class CreateAccountHandler(IUnitOfWork uow, IPasswordHasher passwordHasher, IPermissionService permissions, IMapper mapper)
|
||||
: IRequestHandler<CreateAccountCommand, AccountDto>
|
||||
{
|
||||
private const string Screen = "accounts";
|
||||
|
||||
public async Task<AccountDto> Handle(CreateAccountCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Create, ct);
|
||||
|
||||
var request = command.Request;
|
||||
var username = request.Username.Trim();
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(request.Password))
|
||||
{
|
||||
throw new BadRequestException("Username and password are required");
|
||||
}
|
||||
|
||||
if (await uow.Users.ExistsByUsernameAsync(username, ct))
|
||||
{
|
||||
throw new BadRequestException("Username already exists");
|
||||
}
|
||||
|
||||
var role = await uow.Roles.GetByIdAsync(request.RoleId, ct)
|
||||
?? throw new BadRequestException("Role not found");
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Username = username,
|
||||
PasswordHash = passwordHasher.Hash(request.Password),
|
||||
DisplayName = request.DisplayName.Trim(),
|
||||
RoleId = role.Id,
|
||||
IsActive = true,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
};
|
||||
|
||||
uow.Users.Add(user);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
user.Role = role;
|
||||
return mapper.Map<AccountDto>(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Application.Permissions;
|
||||
|
||||
namespace Mws.Application.Accounts;
|
||||
|
||||
public record DeleteAccountCommand(Guid ActorUserId, Guid Id) : IRequest;
|
||||
|
||||
public class DeleteAccountHandler(IUnitOfWork uow, IPermissionService permissions) : IRequestHandler<DeleteAccountCommand>
|
||||
{
|
||||
private const string Screen = "accounts";
|
||||
|
||||
public async Task Handle(DeleteAccountCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Delete, ct);
|
||||
|
||||
var user = await uow.Users.GetByIdAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("Account not found");
|
||||
|
||||
var soleOwnerProjectIds = await uow.Projects.GetOwnedProjectIdsAsync(command.Id, ct);
|
||||
|
||||
foreach (var projectId in soleOwnerProjectIds)
|
||||
{
|
||||
var ownerCount = await uow.Projects.CountOwnersAsync(projectId, ct);
|
||||
if (ownerCount <= 1)
|
||||
{
|
||||
throw new BadRequestException("Cannot delete an account that is the sole owner of a project");
|
||||
}
|
||||
}
|
||||
|
||||
uow.Users.Remove(user);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using MediatR;
|
||||
using Mws.Application.Auth;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Application.Permissions;
|
||||
|
||||
namespace Mws.Application.Accounts;
|
||||
|
||||
public record ResetPasswordCommand(Guid ActorUserId, Guid Id, ResetPasswordRequest Request) : IRequest;
|
||||
|
||||
public class ResetPasswordHandler(IUnitOfWork uow, IPasswordHasher passwordHasher, IPermissionService permissions)
|
||||
: IRequestHandler<ResetPasswordCommand>
|
||||
{
|
||||
private const string Screen = "accounts";
|
||||
|
||||
public async Task Handle(ResetPasswordCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Edit, ct);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(command.Request.NewPassword))
|
||||
{
|
||||
throw new BadRequestException("New password is required");
|
||||
}
|
||||
|
||||
var user = await uow.Users.GetByIdAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("Account not found");
|
||||
|
||||
user.PasswordHash = passwordHasher.Hash(command.Request.NewPassword);
|
||||
user.UpdatedAt = DateTime.UtcNow;
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Application.Permissions;
|
||||
|
||||
namespace Mws.Application.Accounts;
|
||||
|
||||
public record UpdateAccountCommand(Guid ActorUserId, Guid Id, UpdateAccountRequest Request) : IRequest<AccountDto>;
|
||||
|
||||
public class UpdateAccountHandler(IUnitOfWork uow, IPermissionService permissions, IMapper mapper)
|
||||
: IRequestHandler<UpdateAccountCommand, AccountDto>
|
||||
{
|
||||
private const string Screen = "accounts";
|
||||
|
||||
public async Task<AccountDto> Handle(UpdateAccountCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Edit, ct);
|
||||
|
||||
var user = await uow.Users.GetByIdWithRoleAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("Account not found");
|
||||
|
||||
var request = command.Request;
|
||||
var role = await uow.Roles.GetByIdAsync(request.RoleId, ct)
|
||||
?? throw new BadRequestException("Role not found");
|
||||
|
||||
user.DisplayName = request.DisplayName.Trim();
|
||||
user.RoleId = role.Id;
|
||||
user.Role = role;
|
||||
user.IsActive = request.IsActive;
|
||||
user.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return mapper.Map<AccountDto>(user);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace Mws.Application.Accounts;
|
||||
|
||||
public interface IAccountService
|
||||
{
|
||||
Task<List<AccountDto>> GetAccountsAsync(Guid actorUserId, string? term, CancellationToken ct = default);
|
||||
Task<AccountDto> CreateAccountAsync(Guid actorUserId, CreateAccountRequest request, CancellationToken ct = default);
|
||||
Task<AccountDto> UpdateAccountAsync(Guid actorUserId, Guid id, UpdateAccountRequest request, CancellationToken ct = default);
|
||||
Task DeleteAccountAsync(Guid actorUserId, Guid id, CancellationToken ct = default);
|
||||
Task ResetPasswordAsync(Guid actorUserId, Guid id, ResetPasswordRequest request, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Application.Permissions;
|
||||
|
||||
namespace Mws.Application.Accounts;
|
||||
|
||||
public record GetAccountsQuery(Guid ActorUserId, string? Term) : IRequest<List<AccountDto>>;
|
||||
|
||||
public class GetAccountsHandler(IUnitOfWork uow, IPermissionService permissions, IMapper mapper)
|
||||
: IRequestHandler<GetAccountsQuery, List<AccountDto>>
|
||||
{
|
||||
private const string Screen = "accounts";
|
||||
|
||||
public async Task<List<AccountDto>> Handle(GetAccountsQuery query, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(query.ActorUserId, Screen, PermissionAction.View, ct);
|
||||
|
||||
var users = await uow.Users.SearchWithRoleAsync(query.Term, null, ct);
|
||||
return mapper.Map<List<AccountDto>>(users);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user