Files
2026-09-20 16:21:15 +07:00

41 lines
1.5 KiB
C#

using AutoMapper;
using MediatR;
using mws.backend.dotnet.application.Audit;
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.application.Permissions;
namespace mws.backend.dotnet.application.Projects;
public record UpdateProjectCommand(Guid UserId, Guid ProjectId, UpdateProjectRequest Request) : IRequest<ProjectDto>, IAuditableRequest
{
public string Action => "Project.Update";
public string EntityType => "Project";
public Guid? EntityId => ProjectId;
public string? EntityName => Request.Name;
}
public class UpdateProjectHandler(IUnitOfWork uow, IMapper mapper, IPermissionService permissions) : IRequestHandler<UpdateProjectCommand, ProjectDto>
{
public async Task<ProjectDto> Handle(UpdateProjectCommand command, CancellationToken ct)
{
await permissions.EnsureAsync(command.UserId, "projects", PermissionAction.Edit, ct);
var project = await ProjectAccess.GetForUserOrThrowAsync(uow, command.UserId, command.ProjectId, ct);
var request = command.Request;
if (string.IsNullOrWhiteSpace(request.Name))
{
throw new BadRequestException("Project name is required");
}
project.Name = request.Name.Trim();
project.Description = request.Description;
project.Status = request.Status;
project.UpdatedBy = command.UserId;
project.UpdatedAt = DateTime.UtcNow;
await uow.SaveChangesAsync(ct);
return mapper.Map<ProjectDto>(project);
}
}