Convert Projects module to MediatR commands/queries
Replaces IProjectService/ProjectService. IProjectMemberService is left in place for now — ProjectMembersController still depends on it until the next task converts it too. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Domain.Projects;
|
||||
|
||||
namespace Mws.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,
|
||||
CreatedAt = now,
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user