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:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user