41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using AutoMapper;
|
|||
|
|
using MediatR;
|
||
|
|
using Mws.Application.Common;
|
||
|
|
using Mws.Application.Permissions;
|
||
|
|
using Mws.Domain.Documents;
|
||
|
|
|
||
|
|
namespace Mws.Application.Documents;
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|