Files

41 lines
1.5 KiB
C#
Raw Permalink Normal View History

using AutoMapper;
using MediatR;
2026-09-20 16:21:15 +07:00
using mws.backend.dotnet.application.Audit;
2026-08-19 23:25:08 +07:00
using mws.backend.dotnet.application.Common;
2026-09-12 19:40:59 +07:00
using mws.backend.dotnet.application.Permissions;
2026-08-19 23:25:08 +07:00
namespace mws.backend.dotnet.application.Projects;
2026-09-20 16:21:15 +07:00
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;
}
2026-09-12 19:40:59 +07:00
public class UpdateProjectHandler(IUnitOfWork uow, IMapper mapper, IPermissionService permissions) : IRequestHandler<UpdateProjectCommand, ProjectDto>
{
public async Task<ProjectDto> Handle(UpdateProjectCommand command, CancellationToken ct)
{
2026-09-12 19:40:59 +07:00
await permissions.EnsureAsync(command.UserId, "projects", PermissionAction.Edit, ct);
2026-09-12 19:40:59 +07:00
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;
2026-08-19 23:25:08 +07:00
project.UpdatedBy = command.UserId;
project.UpdatedAt = DateTime.UtcNow;
await uow.SaveChangesAsync(ct);
return mapper.Map<ProjectDto>(project);
}
}