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:
2026-08-14 08:47:02 +07:00
co-authored by Claude Sonnet 5
parent 3a926e8693
commit 7e265805c3
12 changed files with 208 additions and 151 deletions
+9 -8
View File
@@ -1,3 +1,4 @@
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Mws.Application.Projects;
@@ -7,49 +8,49 @@ namespace Mws.Api.Controllers;
[ApiController]
[Route("api/projects")]
[Authorize]
public class ProjectsController(IProjectService projectService) : ControllerBase
public class ProjectsController(ISender sender) : ControllerBase
{
[HttpGet]
public async Task<ActionResult<List<ProjectDto>>> GetAll(CancellationToken ct)
{
return Ok(await projectService.GetProjectsAsync(User.GetUserId(), ct));
return Ok(await sender.Send(new GetProjectsQuery(User.GetUserId()), ct));
}
[HttpGet("search")]
public async Task<ActionResult<List<ProjectDto>>> Search([FromQuery] string? q, CancellationToken ct)
{
return Ok(await projectService.SearchAsync(User.GetUserId(), q ?? string.Empty, ct));
return Ok(await sender.Send(new SearchProjectsQuery(User.GetUserId(), q ?? string.Empty), ct));
}
[HttpGet("{projectId:guid}")]
public async Task<ActionResult<ProjectDto>> Get(Guid projectId, CancellationToken ct)
{
return Ok(await projectService.GetProjectAsync(User.GetUserId(), projectId, ct));
return Ok(await sender.Send(new GetProjectQuery(User.GetUserId(), projectId), ct));
}
[HttpGet("{projectId:guid}/overview")]
public async Task<ActionResult<ProjectOverviewDto>> Overview(Guid projectId, CancellationToken ct)
{
return Ok(await projectService.GetOverviewAsync(User.GetUserId(), projectId, ct));
return Ok(await sender.Send(new GetProjectOverviewQuery(User.GetUserId(), projectId), ct));
}
[HttpPost]
public async Task<ActionResult<ProjectDto>> Create([FromBody] CreateProjectRequest request, CancellationToken ct)
{
var result = await projectService.CreateProjectAsync(User.GetUserId(), request, ct);
var result = await sender.Send(new CreateProjectCommand(User.GetUserId(), request), ct);
return CreatedAtAction(nameof(Get), new { projectId = result.Id }, result);
}
[HttpPut("{projectId:guid}")]
public async Task<ActionResult<ProjectDto>> Update(Guid projectId, [FromBody] UpdateProjectRequest request, CancellationToken ct)
{
return Ok(await projectService.UpdateProjectAsync(User.GetUserId(), projectId, request, ct));
return Ok(await sender.Send(new UpdateProjectCommand(User.GetUserId(), projectId, request), ct));
}
[HttpDelete("{projectId:guid}")]
public async Task<IActionResult> Delete(Guid projectId, CancellationToken ct)
{
await projectService.ArchiveProjectAsync(User.GetUserId(), projectId, ct);
await sender.Send(new ArchiveProjectCommand(User.GetUserId(), projectId), ct);
return NoContent();
}
}
@@ -0,0 +1,24 @@
using MediatR;
using Mws.Application.Common;
using Mws.Domain.Projects;
namespace Mws.Application.Projects;
public record ArchiveProjectCommand(Guid UserId, Guid ProjectId) : IRequest;
public class ArchiveProjectHandler(IUnitOfWork uow) : IRequestHandler<ArchiveProjectCommand>
{
public async Task Handle(ArchiveProjectCommand command, CancellationToken ct)
{
var project = await ProjectAccess.GetForUserOrThrowAsync(uow, command.UserId, command.ProjectId, ct);
if (await uow.Projects.GetMemberRoleAsync(command.ProjectId, command.UserId, ct) != MemberRole.Owner)
{
throw new ForbiddenException("Only the project owner can archive the project");
}
project.Status = ProjectStatus.Archived;
project.UpdatedAt = DateTime.UtcNow;
await uow.SaveChangesAsync(ct);
}
}
@@ -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);
}
}
@@ -0,0 +1,35 @@
using AutoMapper;
using MediatR;
using Mws.Application.Common;
using Mws.Domain.Projects;
namespace Mws.Application.Projects;
public record UpdateProjectCommand(Guid UserId, Guid ProjectId, UpdateProjectRequest Request) : IRequest<ProjectDto>;
public class UpdateProjectHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<UpdateProjectCommand, ProjectDto>
{
public async Task<ProjectDto> Handle(UpdateProjectCommand command, CancellationToken ct)
{
var project = await ProjectAccess.GetForUserOrThrowAsync(uow, command.UserId, command.ProjectId, ct);
if (MemberRole.Owner != await uow.Projects.GetMemberRoleAsync(command.ProjectId, command.UserId, ct))
{
throw new ForbiddenException("Only the project owner can update the project");
}
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.UpdatedAt = DateTime.UtcNow;
await uow.SaveChangesAsync(ct);
return mapper.Map<ProjectDto>(project);
}
}
@@ -1,18 +1,5 @@
using Mws.Application.Common;
namespace Mws.Application.Projects;
public interface IProjectService
{
Task<List<ProjectDto>> GetProjectsAsync(Guid userId, CancellationToken ct = default);
Task<ProjectDto> GetProjectAsync(Guid userId, Guid projectId, CancellationToken ct = default);
Task<ProjectDto> CreateProjectAsync(Guid userId, CreateProjectRequest request, CancellationToken ct = default);
Task<ProjectDto> UpdateProjectAsync(Guid userId, Guid projectId, UpdateProjectRequest request, CancellationToken ct = default);
Task ArchiveProjectAsync(Guid userId, Guid projectId, CancellationToken ct = default);
Task<ProjectOverviewDto> GetOverviewAsync(Guid userId, Guid projectId, CancellationToken ct = default);
Task<List<ProjectDto>> SearchAsync(Guid userId, string term, CancellationToken ct = default);
}
public interface IProjectMemberService
{
Task<List<ProjectMemberDto>> GetMembersAsync(Guid userId, Guid projectId, CancellationToken ct = default);
+13
View File
@@ -0,0 +1,13 @@
using Mws.Application.Common;
using Mws.Domain.Projects;
namespace Mws.Application.Projects;
internal static class ProjectAccess
{
public static async Task<Project> GetForUserOrThrowAsync(IUnitOfWork uow, Guid userId, Guid projectId, CancellationToken ct)
{
return await uow.Projects.GetForUserAsync(userId, projectId, ct)
?? throw new NotFoundException("Project not found");
}
}
-127
View File
@@ -1,127 +0,0 @@
using AutoMapper;
using Mws.Application.Common;
using Mws.Domain.Documents;
using Mws.Domain.Projects;
namespace Mws.Application.Projects;
public class ProjectService(IUnitOfWork uow, IMapper mapper) : IProjectService
{
public async Task<List<ProjectDto>> GetProjectsAsync(Guid userId, CancellationToken ct = default)
{
var projects = await uow.Projects.GetForUserAsync(userId, ct);
return mapper.Map<List<ProjectDto>>(projects);
}
public async Task<ProjectDto> GetProjectAsync(Guid userId, Guid projectId, CancellationToken ct = default)
{
var project = await GetProjectForUserAsync(userId, projectId, ct);
return mapper.Map<ProjectDto>(project);
}
public async Task<ProjectDto> CreateProjectAsync(Guid userId, CreateProjectRequest request, CancellationToken ct = default)
{
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 = userId,
Role = MemberRole.Owner,
});
uow.Projects.Add(project);
await uow.SaveChangesAsync(ct);
return mapper.Map<ProjectDto>(project);
}
public async Task<ProjectDto> UpdateProjectAsync(Guid userId, Guid projectId, UpdateProjectRequest request, CancellationToken ct = default)
{
var project = await GetProjectForUserAsync(userId, projectId, ct);
if (MemberRole.Owner != await GetMemberRoleAsync(userId, projectId, ct))
{
throw new ForbiddenException("Only the project owner can update the project");
}
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.UpdatedAt = DateTime.UtcNow;
await uow.SaveChangesAsync(ct);
return mapper.Map<ProjectDto>(project);
}
public async Task ArchiveProjectAsync(Guid userId, Guid projectId, CancellationToken ct = default)
{
var project = await GetProjectForUserAsync(userId, projectId, ct);
if (await GetMemberRoleAsync(userId, projectId, ct) != MemberRole.Owner)
{
throw new ForbiddenException("Only the project owner can archive the project");
}
project.Status = ProjectStatus.Archived;
project.UpdatedAt = DateTime.UtcNow;
await uow.SaveChangesAsync(ct);
}
public async Task<ProjectOverviewDto> GetOverviewAsync(Guid userId, Guid projectId, CancellationToken ct = default)
{
var project = await GetProjectForUserAsync(userId, projectId, ct);
var memberCount = await uow.Projects.CountMembersAsync(projectId, ct);
var documentCount = await uow.Documents.CountByTypeForProjectAsync(projectId, DocumentType.Document, ct);
var taskCounts = await uow.Tasks.CountByStatusForProjectAsync(projectId, ct);
var taskCountsByStatus = taskCounts.ToDictionary(kv => kv.Key.ToString(), kv => kv.Value);
var recentTasks = await uow.Tasks.GetRecentForProjectAsync(projectId, 5, ct);
var recentDocuments = await uow.Documents.GetRecentForProjectAsync(projectId, DocumentType.Document, 5, ct);
return new ProjectOverviewDto
{
Project = mapper.Map<ProjectDto>(project),
MemberCount = memberCount,
DocumentCount = documentCount,
TaskCountsByStatus = taskCountsByStatus,
RecentTasks = mapper.Map<List<RecentTaskDto>>(recentTasks),
RecentDocuments = mapper.Map<List<RecentDocumentDto>>(recentDocuments),
};
}
public async Task<List<ProjectDto>> SearchAsync(Guid userId, string term, CancellationToken ct = default)
{
var projects = await uow.Projects.SearchForUserAsync(userId, term, ct);
return mapper.Map<List<ProjectDto>>(projects);
}
protected async Task<Project> GetProjectForUserAsync(Guid userId, Guid projectId, CancellationToken ct = default)
{
return await uow.Projects.GetForUserAsync(userId, projectId, ct)
?? throw new NotFoundException("Project not found");
}
protected Task<MemberRole?> GetMemberRoleAsync(Guid userId, Guid projectId, CancellationToken ct = default) =>
uow.Projects.GetMemberRoleAsync(projectId, userId, ct);
}
@@ -0,0 +1,16 @@
using AutoMapper;
using MediatR;
using Mws.Application.Common;
namespace Mws.Application.Projects;
public record GetProjectQuery(Guid UserId, Guid ProjectId) : IRequest<ProjectDto>;
public class GetProjectHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<GetProjectQuery, ProjectDto>
{
public async Task<ProjectDto> Handle(GetProjectQuery query, CancellationToken ct)
{
var project = await ProjectAccess.GetForUserOrThrowAsync(uow, query.UserId, query.ProjectId, ct);
return mapper.Map<ProjectDto>(project);
}
}
@@ -0,0 +1,35 @@
using AutoMapper;
using MediatR;
using Mws.Application.Common;
using Mws.Domain.Documents;
namespace Mws.Application.Projects;
public record GetProjectOverviewQuery(Guid UserId, Guid ProjectId) : IRequest<ProjectOverviewDto>;
public class GetProjectOverviewHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<GetProjectOverviewQuery, ProjectOverviewDto>
{
public async Task<ProjectOverviewDto> Handle(GetProjectOverviewQuery query, CancellationToken ct)
{
var project = await ProjectAccess.GetForUserOrThrowAsync(uow, query.UserId, query.ProjectId, ct);
var memberCount = await uow.Projects.CountMembersAsync(query.ProjectId, ct);
var documentCount = await uow.Documents.CountByTypeForProjectAsync(query.ProjectId, DocumentType.Document, ct);
var taskCounts = await uow.Tasks.CountByStatusForProjectAsync(query.ProjectId, ct);
var taskCountsByStatus = taskCounts.ToDictionary(kv => kv.Key.ToString(), kv => kv.Value);
var recentTasks = await uow.Tasks.GetRecentForProjectAsync(query.ProjectId, 5, ct);
var recentDocuments = await uow.Documents.GetRecentForProjectAsync(query.ProjectId, DocumentType.Document, 5, ct);
return new ProjectOverviewDto
{
Project = mapper.Map<ProjectDto>(project),
MemberCount = memberCount,
DocumentCount = documentCount,
TaskCountsByStatus = taskCountsByStatus,
RecentTasks = mapper.Map<List<RecentTaskDto>>(recentTasks),
RecentDocuments = mapper.Map<List<RecentDocumentDto>>(recentDocuments),
};
}
}
@@ -0,0 +1,16 @@
using AutoMapper;
using MediatR;
using Mws.Application.Common;
namespace Mws.Application.Projects;
public record GetProjectsQuery(Guid UserId) : IRequest<List<ProjectDto>>;
public class GetProjectsHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<GetProjectsQuery, List<ProjectDto>>
{
public async Task<List<ProjectDto>> Handle(GetProjectsQuery query, CancellationToken ct)
{
var projects = await uow.Projects.GetForUserAsync(query.UserId, ct);
return mapper.Map<List<ProjectDto>>(projects);
}
}
@@ -0,0 +1,16 @@
using AutoMapper;
using MediatR;
using Mws.Application.Common;
namespace Mws.Application.Projects;
public record SearchProjectsQuery(Guid UserId, string Term) : IRequest<List<ProjectDto>>;
public class SearchProjectsHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<SearchProjectsQuery, List<ProjectDto>>
{
public async Task<List<ProjectDto>> Handle(SearchProjectsQuery query, CancellationToken ct)
{
var projects = await uow.Projects.SearchForUserAsync(query.UserId, query.Term, ct);
return mapper.Map<List<ProjectDto>>(projects);
}
}
@@ -39,7 +39,6 @@ public static class DependencyInjection
services.AddScoped<ITokenService, JwtTokenService>();
services.AddScoped<IPermissionService, PermissionService>();
services.AddScoped<IProjectService, ProjectService>();
services.AddScoped<IProjectMemberService, ProjectMemberService>();
services.AddScoped<IDocumentService, DocumentService>();
services.AddScoped<ITaskService, TaskService>();