Convert Documents module to MediatR commands/queries
Replaces IDocumentService/DocumentService. The permission-check and descendant-deletion logic moves to a shared DocumentAccess static helper used by all seven handlers. 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.Documents;
|
||||
@@ -7,50 +8,50 @@ namespace Mws.Api.Controllers;
|
||||
[ApiController]
|
||||
[Route("api")]
|
||||
[Authorize]
|
||||
public class DocumentsController(IDocumentService documentService) : ControllerBase
|
||||
public class DocumentsController(ISender sender) : ControllerBase
|
||||
{
|
||||
[HttpGet("documents/search")]
|
||||
public async Task<ActionResult<List<DocumentNodeDto>>> Search([FromQuery] string? q, CancellationToken ct)
|
||||
{
|
||||
return Ok(await documentService.SearchAsync(User.GetUserId(), q ?? string.Empty, ct));
|
||||
return Ok(await sender.Send(new SearchDocumentsQuery(User.GetUserId(), q ?? string.Empty), ct));
|
||||
}
|
||||
|
||||
[HttpGet("projects/{projectId:guid}/documents")]
|
||||
public async Task<ActionResult<List<DocumentNodeDto>>> GetTree(Guid projectId, CancellationToken ct)
|
||||
{
|
||||
return Ok(await documentService.GetTreeAsync(User.GetUserId(), projectId, ct));
|
||||
return Ok(await sender.Send(new GetDocumentTreeQuery(User.GetUserId(), projectId), ct));
|
||||
}
|
||||
|
||||
[HttpPost("projects/{projectId:guid}/documents")]
|
||||
public async Task<ActionResult<DocumentDto>> Create(Guid projectId, [FromBody] CreateDocumentRequest request, CancellationToken ct)
|
||||
{
|
||||
var result = await documentService.CreateAsync(User.GetUserId(), projectId, request, ct);
|
||||
var result = await sender.Send(new CreateDocumentCommand(User.GetUserId(), projectId, request), ct);
|
||||
return CreatedAtAction(nameof(Get), new { id = result.Id }, result);
|
||||
}
|
||||
|
||||
[HttpGet("documents/{id:guid}")]
|
||||
public async Task<ActionResult<DocumentDto>> Get(Guid id, CancellationToken ct)
|
||||
{
|
||||
return Ok(await documentService.GetAsync(User.GetUserId(), id, ct));
|
||||
return Ok(await sender.Send(new GetDocumentQuery(User.GetUserId(), id), ct));
|
||||
}
|
||||
|
||||
[HttpPut("documents/{id:guid}")]
|
||||
public async Task<ActionResult<DocumentDto>> Update(Guid id, [FromBody] UpdateDocumentRequest request, CancellationToken ct)
|
||||
{
|
||||
return Ok(await documentService.UpdateAsync(User.GetUserId(), id, request, ct));
|
||||
return Ok(await sender.Send(new UpdateDocumentCommand(User.GetUserId(), id, request), ct));
|
||||
}
|
||||
|
||||
[HttpPut("documents/{id:guid}/move")]
|
||||
public async Task<ActionResult<DocumentDto>> Move(Guid id, [FromBody] MoveDocumentRequest request, CancellationToken ct)
|
||||
{
|
||||
await documentService.MoveAsync(User.GetUserId(), id, request.NewParentId, ct);
|
||||
return Ok(await documentService.GetAsync(User.GetUserId(), id, ct));
|
||||
await sender.Send(new MoveDocumentCommand(User.GetUserId(), id, request.NewParentId), ct);
|
||||
return Ok(await sender.Send(new GetDocumentQuery(User.GetUserId(), id), ct));
|
||||
}
|
||||
|
||||
[HttpDelete("documents/{id:guid}")]
|
||||
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||||
{
|
||||
await documentService.DeleteAsync(User.GetUserId(), id, ct);
|
||||
await sender.Send(new DeleteDocumentCommand(User.GetUserId(), id), ct);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user