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,3 +1,4 @@
|
|||||||
|
using MediatR;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Mws.Application.Accounts;
|
using Mws.Application.Accounts;
|
||||||
@@ -7,37 +8,37 @@ namespace Mws.Api.Controllers;
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/accounts")]
|
[Route("api/accounts")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class AccountsController(IAccountService accountService) : ControllerBase
|
public class AccountsController(ISender sender) : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<List<AccountDto>>> GetAll([FromQuery] string? q, CancellationToken ct)
|
public async Task<ActionResult<List<AccountDto>>> GetAll([FromQuery] string? q, CancellationToken ct)
|
||||||
{
|
{
|
||||||
return Ok(await accountService.GetAccountsAsync(User.GetUserId(), q, ct));
|
return Ok(await sender.Send(new GetAccountsQuery(User.GetUserId(), q), ct));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult<AccountDto>> Create([FromBody] CreateAccountRequest request, CancellationToken ct)
|
public async Task<ActionResult<AccountDto>> Create([FromBody] CreateAccountRequest request, CancellationToken ct)
|
||||||
{
|
{
|
||||||
return Ok(await accountService.CreateAccountAsync(User.GetUserId(), request, ct));
|
return Ok(await sender.Send(new CreateAccountCommand(User.GetUserId(), request), ct));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{id:guid}")]
|
[HttpPut("{id:guid}")]
|
||||||
public async Task<ActionResult<AccountDto>> Update(Guid id, [FromBody] UpdateAccountRequest request, CancellationToken ct)
|
public async Task<ActionResult<AccountDto>> Update(Guid id, [FromBody] UpdateAccountRequest request, CancellationToken ct)
|
||||||
{
|
{
|
||||||
return Ok(await accountService.UpdateAccountAsync(User.GetUserId(), id, request, ct));
|
return Ok(await sender.Send(new UpdateAccountCommand(User.GetUserId(), id, request), ct));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{id:guid}")]
|
[HttpDelete("{id:guid}")]
|
||||||
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||||||
{
|
{
|
||||||
await accountService.DeleteAccountAsync(User.GetUserId(), id, ct);
|
await sender.Send(new DeleteAccountCommand(User.GetUserId(), id), ct);
|
||||||
return NoContent();
|
return NoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{id:guid}/reset-password")]
|
[HttpPost("{id:guid}/reset-password")]
|
||||||
public async Task<IActionResult> ResetPassword(Guid id, [FromBody] ResetPasswordRequest request, CancellationToken ct)
|
public async Task<IActionResult> ResetPassword(Guid id, [FromBody] ResetPasswordRequest request, CancellationToken ct)
|
||||||
{
|
{
|
||||||
await accountService.ResetPasswordAsync(User.GetUserId(), id, request, ct);
|
await sender.Send(new ResetPasswordCommand(User.GetUserId(), id, request), ct);
|
||||||
return NoContent();
|
return NoContent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Mws.Application.Accounts;
|
|
||||||
using Mws.Application.Auth;
|
using Mws.Application.Auth;
|
||||||
using Mws.Application.Common;
|
using Mws.Application.Common;
|
||||||
using Mws.Application.Documents;
|
using Mws.Application.Documents;
|
||||||
@@ -40,7 +39,6 @@ public static class DependencyInjection
|
|||||||
services.AddSingleton(jwtOptions);
|
services.AddSingleton(jwtOptions);
|
||||||
services.AddScoped<ITokenService, JwtTokenService>();
|
services.AddScoped<ITokenService, JwtTokenService>();
|
||||||
|
|
||||||
services.AddScoped<IAccountService, AccountService>();
|
|
||||||
services.AddScoped<IPermissionService, PermissionService>();
|
services.AddScoped<IPermissionService, PermissionService>();
|
||||||
services.AddScoped<IRoleService, RoleService>();
|
services.AddScoped<IRoleService, RoleService>();
|
||||||
services.AddScoped<IProjectService, ProjectService>();
|
services.AddScoped<IProjectService, ProjectService>();
|
||||||
|
|||||||
Reference in New Issue
Block a user