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.Mvc;
|
||||
using Mws.Application.Accounts;
|
||||
@@ -7,37 +8,37 @@ namespace Mws.Api.Controllers;
|
||||
[ApiController]
|
||||
[Route("api/accounts")]
|
||||
[Authorize]
|
||||
public class AccountsController(IAccountService accountService) : ControllerBase
|
||||
public class AccountsController(ISender sender) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
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]
|
||||
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}")]
|
||||
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}")]
|
||||
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();
|
||||
}
|
||||
|
||||
[HttpPost("{id:guid}/reset-password")]
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user