116 lines
3.7 KiB
C#
116 lines
3.7 KiB
C#
using Socialize.Infrastructure.Security;
|
|
namespace Socialize.Modules.Projects.Handlers;
|
|
|
|
public record CreateProjectRequest(
|
|
Guid WorkspaceId,
|
|
Guid ClientId,
|
|
string Name,
|
|
DateTimeOffset StartDate,
|
|
DateTimeOffset EndDate,
|
|
string? Description,
|
|
string? Notes);
|
|
|
|
public class CreateProjectRequestValidator
|
|
: Validator<CreateProjectRequest>
|
|
{
|
|
public CreateProjectRequestValidator()
|
|
{
|
|
RuleFor(x => x.WorkspaceId).NotEmpty();
|
|
RuleFor(x => x.ClientId).NotEmpty();
|
|
RuleFor(x => x.Name).NotEmpty().MaximumLength(256);
|
|
RuleFor(x => x.StartDate).NotEmpty();
|
|
RuleFor(x => x.EndDate)
|
|
.NotEmpty()
|
|
.GreaterThanOrEqualTo(x => x.StartDate);
|
|
RuleFor(x => x.Description).MaximumLength(4000);
|
|
RuleFor(x => x.Notes).MaximumLength(4000);
|
|
}
|
|
}
|
|
|
|
public class CreateProjectHandler(
|
|
AppDbContext dbContext,
|
|
AccessScopeService accessScopeService)
|
|
: Endpoint<CreateProjectRequest, ProjectDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/projects");
|
|
Options(o => o.WithTags("Projects"));
|
|
}
|
|
|
|
public override async Task HandleAsync(CreateProjectRequest request, CancellationToken ct)
|
|
{
|
|
if (!accessScopeService.CanManageWorkspace(User, request.WorkspaceId))
|
|
{
|
|
await SendForbiddenAsync(ct);
|
|
return;
|
|
}
|
|
|
|
bool workspaceExists = await dbContext.Workspaces
|
|
.AnyAsync(workspace => workspace.Id == request.WorkspaceId, ct);
|
|
|
|
if (!workspaceExists)
|
|
{
|
|
AddError(request => request.WorkspaceId, "The selected workspace does not exist.");
|
|
await SendErrorsAsync(StatusCodes.Status400BadRequest, ct);
|
|
return;
|
|
}
|
|
|
|
bool clientExists = await dbContext.Clients
|
|
.AnyAsync(
|
|
client => client.Id == request.ClientId && client.WorkspaceId == request.WorkspaceId,
|
|
ct);
|
|
|
|
if (!clientExists)
|
|
{
|
|
AddError(request => request.ClientId, "The selected client does not belong to the active workspace.");
|
|
await SendErrorsAsync(StatusCodes.Status400BadRequest, ct);
|
|
return;
|
|
}
|
|
|
|
string normalizedName = request.Name.Trim();
|
|
|
|
bool duplicateProject = await dbContext.Projects
|
|
.AnyAsync(
|
|
project => project.ClientId == request.ClientId && project.Name == normalizedName,
|
|
ct);
|
|
|
|
if (duplicateProject)
|
|
{
|
|
AddError(request => request.Name, "A project with this name already exists for the selected client.");
|
|
await SendErrorsAsync(StatusCodes.Status409Conflict, ct);
|
|
return;
|
|
}
|
|
|
|
Project project = new()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
WorkspaceId = request.WorkspaceId,
|
|
ClientId = request.ClientId,
|
|
Name = normalizedName,
|
|
Description = string.IsNullOrWhiteSpace(request.Description) ? null : request.Description.Trim(),
|
|
Notes = string.IsNullOrWhiteSpace(request.Notes) ? null : request.Notes.Trim(),
|
|
Status = "Planned",
|
|
StartDate = request.StartDate,
|
|
EndDate = request.EndDate,
|
|
CreatedAt = DateTimeOffset.UtcNow,
|
|
};
|
|
|
|
dbContext.Projects.Add(project);
|
|
await dbContext.SaveChangesAsync(ct);
|
|
|
|
ProjectDto dto = new(
|
|
project.Id,
|
|
project.WorkspaceId,
|
|
project.ClientId,
|
|
project.Name,
|
|
project.Description,
|
|
project.Notes,
|
|
project.Status,
|
|
project.StartDate,
|
|
project.EndDate);
|
|
|
|
await SendAsync(dto, StatusCodes.Status201Created, ct);
|
|
}
|
|
}
|