45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using AutoMapper;
|
|
using MediatR;
|
|
using mws.backend.dotnet.application.Common;
|
|
using mws.backend.dotnet.domain.Projects;
|
|
|
|
namespace mws.backend.dotnet.application.Projects;
|
|
|
|
public record CreateProjectCommand(Guid UserId, CreateProjectRequest Request) : IRequest<ProjectDto>;
|
|
|
|
public class CreateProjectHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<CreateProjectCommand, ProjectDto>
|
|
{
|
|
public async Task<ProjectDto> Handle(CreateProjectCommand command, CancellationToken ct)
|
|
{
|
|
var request = command.Request;
|
|
if (string.IsNullOrWhiteSpace(request.Name))
|
|
{
|
|
throw new BadRequestException("Project name is required");
|
|
}
|
|
|
|
var now = DateTime.UtcNow;
|
|
var project = new Project
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Name = request.Name.Trim(),
|
|
Description = request.Description,
|
|
Status = ProjectStatus.Active,
|
|
CreatedBy = command.UserId,
|
|
CreatedAt = now,
|
|
UpdatedBy = command.UserId,
|
|
UpdatedAt = now,
|
|
};
|
|
|
|
project.Members.Add(new ProjectMember
|
|
{
|
|
ProjectId = project.Id,
|
|
UserId = command.UserId,
|
|
Role = MemberRole.Owner,
|
|
});
|
|
|
|
uow.Projects.Add(project);
|
|
await uow.SaveChangesAsync(ct);
|
|
return mapper.Map<ProjectDto>(project);
|
|
}
|
|
}
|