2026-08-17 21:02:25 +07:00
|
|
|
using AutoMapper;
|
|
|
|
|
using MediatR;
|
2026-08-19 23:25:08 +07:00
|
|
|
using mws.backend.dotnet.application.Common;
|
|
|
|
|
using mws.backend.dotnet.application.Permissions;
|
|
|
|
|
using mws.backend.dotnet.domain.Documents;
|
2026-08-17 21:02:25 +07:00
|
|
|
|
2026-08-19 23:25:08 +07:00
|
|
|
namespace mws.backend.dotnet.application.Documents;
|
2026-08-17 21:02:25 +07:00
|
|
|
|
|
|
|
|
public record UpdateDocumentCommand(Guid UserId, Guid DocumentId, UpdateDocumentRequest Request) : IRequest<DocumentDto>;
|
|
|
|
|
|
|
|
|
|
public class UpdateDocumentHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<UpdateDocumentCommand, DocumentDto>
|
|
|
|
|
{
|
|
|
|
|
public async Task<DocumentDto> Handle(UpdateDocumentCommand command, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var doc = await DocumentAccess.GetDocumentForUserAsync(uow, command.UserId, command.DocumentId, ct);
|
|
|
|
|
await DocumentAccess.EnsureDocumentPermissionAsync(uow, command.UserId, doc.ProjectId, PermissionAction.Edit, ct);
|
|
|
|
|
|
|
|
|
|
var request = command.Request;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(request.Title))
|
|
|
|
|
{
|
|
|
|
|
throw new BadRequestException("Title is required");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doc.Title = request.Title.Trim();
|
|
|
|
|
if (doc.Type == DocumentType.Document)
|
|
|
|
|
{
|
|
|
|
|
doc.Content = request.Content;
|
|
|
|
|
doc.UpdatedAt = DateTime.UtcNow;
|
|
|
|
|
doc.UpdatedBy = command.UserId;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
doc.UpdatedAt = DateTime.UtcNow;
|
|
|
|
|
doc.UpdatedBy = command.UserId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await uow.SaveChangesAsync(ct);
|
|
|
|
|
return mapper.Map<DocumentDto>(doc);
|
|
|
|
|
}
|
|
|
|
|
}
|