Convert Tasks module to MediatR commands/queries
Replaces ITaskService/TaskService, completing the CQRS/mediator refactor. All 9 old service interfaces are now gone except IPermissionService, ITokenService, and IPasswordHasher, which stay plain injected services by design (see spec). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Domain.Tasks;
|
||||
using TaskPriority = Mws.Domain.Tasks.TaskPriority;
|
||||
using TaskStatus = Mws.Domain.Tasks.TaskStatus;
|
||||
|
||||
namespace Mws.Application.Tasks;
|
||||
|
||||
public record CreateTaskCommand(Guid UserId, Guid ProjectId, CreateTaskRequest Request) : IRequest<TaskDto>;
|
||||
|
||||
public class CreateTaskHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<CreateTaskCommand, TaskDto>
|
||||
{
|
||||
public async Task<TaskDto> Handle(CreateTaskCommand command, CancellationToken ct)
|
||||
{
|
||||
await TaskAccess.EnsureMemberAccessAsync(uow, command.UserId, command.ProjectId, ct);
|
||||
|
||||
var request = command.Request;
|
||||
if (string.IsNullOrWhiteSpace(request.Title))
|
||||
{
|
||||
throw new BadRequestException("Task title is required");
|
||||
}
|
||||
|
||||
if (request.AssigneeId.HasValue && !await uow.Projects.IsMemberAsync(command.ProjectId, request.AssigneeId.Value, ct))
|
||||
{
|
||||
throw new BadRequestException("Assignee must be a member of the project");
|
||||
}
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var task = new TaskItem
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
ProjectId = command.ProjectId,
|
||||
Title = request.Title.Trim(),
|
||||
Description = request.Description,
|
||||
Status = request.Status ?? TaskStatus.Todo,
|
||||
Priority = request.Priority ?? TaskPriority.Medium,
|
||||
AssigneeId = request.AssigneeId,
|
||||
DueDate = request.DueDate,
|
||||
CreatedBy = command.UserId,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
};
|
||||
|
||||
uow.Tasks.Add(task);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return await TaskAccess.GetDtoAsync(uow, mapper, task.Id, ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
|
||||
namespace Mws.Application.Tasks;
|
||||
|
||||
public record DeleteTaskCommand(Guid UserId, Guid TaskId) : IRequest;
|
||||
|
||||
public class DeleteTaskHandler(IUnitOfWork uow) : IRequestHandler<DeleteTaskCommand>
|
||||
{
|
||||
public async Task Handle(DeleteTaskCommand command, CancellationToken ct)
|
||||
{
|
||||
var task = await TaskAccess.GetTaskForUserAsync(uow, command.UserId, command.TaskId, ct);
|
||||
uow.Tasks.Remove(task);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
|
||||
namespace Mws.Application.Tasks;
|
||||
|
||||
public record UpdateTaskCommand(Guid UserId, Guid TaskId, UpdateTaskRequest Request) : IRequest<TaskDto>;
|
||||
|
||||
public class UpdateTaskHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<UpdateTaskCommand, TaskDto>
|
||||
{
|
||||
public async Task<TaskDto> Handle(UpdateTaskCommand command, CancellationToken ct)
|
||||
{
|
||||
var task = await TaskAccess.GetTaskForUserAsync(uow, command.UserId, command.TaskId, ct);
|
||||
|
||||
var request = command.Request;
|
||||
if (string.IsNullOrWhiteSpace(request.Title))
|
||||
{
|
||||
throw new BadRequestException("Task title is required");
|
||||
}
|
||||
|
||||
if (request.AssigneeId.HasValue && !await uow.Projects.IsMemberAsync(task.ProjectId, request.AssigneeId.Value, ct))
|
||||
{
|
||||
throw new BadRequestException("Assignee must be a member of the project");
|
||||
}
|
||||
|
||||
task.Title = request.Title.Trim();
|
||||
task.Description = request.Description;
|
||||
task.Status = request.Status;
|
||||
task.Priority = request.Priority;
|
||||
task.AssigneeId = request.AssigneeId;
|
||||
task.DueDate = request.DueDate;
|
||||
task.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return await TaskAccess.GetDtoAsync(uow, mapper, task.Id, ct);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using Mws.Application.Common;
|
||||
using TaskPriority = Mws.Domain.Tasks.TaskPriority;
|
||||
using TaskStatus = Mws.Domain.Tasks.TaskStatus;
|
||||
|
||||
namespace Mws.Application.Tasks;
|
||||
|
||||
public interface ITaskService
|
||||
{
|
||||
Task<List<TaskDto>> GetTasksAsync(Guid userId, Guid projectId, TaskStatus? status, TaskPriority? priority, Guid? assigneeId, CancellationToken ct = default);
|
||||
Task<TaskDto> GetAsync(Guid userId, Guid taskId, CancellationToken ct = default);
|
||||
Task<TaskDto> CreateAsync(Guid userId, Guid projectId, CreateTaskRequest request, CancellationToken ct = default);
|
||||
Task<TaskDto> UpdateAsync(Guid userId, Guid taskId, UpdateTaskRequest request, CancellationToken ct = default);
|
||||
Task DeleteAsync(Guid userId, Guid taskId, CancellationToken ct = default);
|
||||
Task<List<TaskDto>> SearchAsync(Guid userId, string term, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
|
||||
namespace Mws.Application.Tasks;
|
||||
|
||||
public record GetTaskQuery(Guid UserId, Guid TaskId) : IRequest<TaskDto>;
|
||||
|
||||
public class GetTaskHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<GetTaskQuery, TaskDto>
|
||||
{
|
||||
public async Task<TaskDto> Handle(GetTaskQuery query, CancellationToken ct)
|
||||
{
|
||||
var task = await TaskAccess.GetTaskForUserAsync(uow, query.UserId, query.TaskId, ct);
|
||||
return await TaskAccess.GetDtoAsync(uow, mapper, task.Id, ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
using TaskPriority = Mws.Domain.Tasks.TaskPriority;
|
||||
using TaskStatus = Mws.Domain.Tasks.TaskStatus;
|
||||
|
||||
namespace Mws.Application.Tasks;
|
||||
|
||||
public record GetTasksQuery(Guid UserId, Guid ProjectId, TaskStatus? Status, TaskPriority? Priority, Guid? AssigneeId)
|
||||
: IRequest<List<TaskDto>>;
|
||||
|
||||
public class GetTasksHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<GetTasksQuery, List<TaskDto>>
|
||||
{
|
||||
public async Task<List<TaskDto>> Handle(GetTasksQuery query, CancellationToken ct)
|
||||
{
|
||||
await TaskAccess.EnsureMemberAccessAsync(uow, query.UserId, query.ProjectId, ct);
|
||||
|
||||
var tasks = await uow.Tasks.GetForProjectAsync(query.ProjectId, query.Status, query.Priority, query.AssigneeId, ct);
|
||||
return mapper.Map<List<TaskDto>>(tasks);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Mws.Application.Common;
|
||||
|
||||
namespace Mws.Application.Tasks;
|
||||
|
||||
public record SearchTasksQuery(Guid UserId, string Term) : IRequest<List<TaskDto>>;
|
||||
|
||||
public class SearchTasksHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<SearchTasksQuery, List<TaskDto>>
|
||||
{
|
||||
public async Task<List<TaskDto>> Handle(SearchTasksQuery query, CancellationToken ct)
|
||||
{
|
||||
var projectIds = await uow.Projects.GetProjectIdsForUserAsync(query.UserId, ct);
|
||||
var tasks = await uow.Tasks.SearchWithAssigneeAsync(projectIds, query.Term, 20, ct);
|
||||
return mapper.Map<List<TaskDto>>(tasks);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using AutoMapper;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Domain.Tasks;
|
||||
|
||||
namespace Mws.Application.Tasks;
|
||||
|
||||
internal static class TaskAccess
|
||||
{
|
||||
public static async Task<TaskDto> GetDtoAsync(IUnitOfWork uow, IMapper mapper, Guid id, CancellationToken ct)
|
||||
{
|
||||
var task = await uow.Tasks.GetWithAssigneeAsync(id, ct)
|
||||
?? throw new NotFoundException("Task not found");
|
||||
return mapper.Map<TaskDto>(task);
|
||||
}
|
||||
|
||||
public static async Task<TaskItem> GetTaskForUserAsync(IUnitOfWork uow, Guid userId, Guid taskId, CancellationToken ct)
|
||||
{
|
||||
var task = await uow.Tasks.GetByIdAsync(taskId, ct)
|
||||
?? throw new NotFoundException("Task not found");
|
||||
|
||||
if (!await uow.Projects.IsMemberAsync(task.ProjectId, userId, ct))
|
||||
{
|
||||
throw new NotFoundException("Task not found");
|
||||
}
|
||||
return task;
|
||||
}
|
||||
|
||||
public static async Task EnsureMemberAccessAsync(IUnitOfWork uow, Guid userId, Guid projectId, CancellationToken ct)
|
||||
{
|
||||
if (!await uow.Projects.IsMemberAsync(projectId, userId, ct))
|
||||
{
|
||||
throw new NotFoundException("Project not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
using AutoMapper;
|
||||
using Mws.Application.Common;
|
||||
using Mws.Domain.Tasks;
|
||||
using TaskPriority = Mws.Domain.Tasks.TaskPriority;
|
||||
using TaskStatus = Mws.Domain.Tasks.TaskStatus;
|
||||
|
||||
namespace Mws.Application.Tasks;
|
||||
|
||||
public class TaskService(IUnitOfWork uow, IMapper mapper) : ITaskService
|
||||
{
|
||||
public async Task<List<TaskDto>> GetTasksAsync(Guid userId, Guid projectId, TaskStatus? status, TaskPriority? priority, Guid? assigneeId, CancellationToken ct = default)
|
||||
{
|
||||
await EnsureMemberAccessAsync(userId, projectId, ct);
|
||||
|
||||
var tasks = await uow.Tasks.GetForProjectAsync(projectId, status, priority, assigneeId, ct);
|
||||
return mapper.Map<List<TaskDto>>(tasks);
|
||||
}
|
||||
|
||||
public async Task<TaskDto> GetAsync(Guid userId, Guid taskId, CancellationToken ct = default)
|
||||
{
|
||||
var task = await GetTaskForUserAsync(userId, taskId, ct);
|
||||
return await GetDtoAsync(task.Id, ct);
|
||||
}
|
||||
|
||||
public async Task<TaskDto> CreateAsync(Guid userId, Guid projectId, CreateTaskRequest request, CancellationToken ct = default)
|
||||
{
|
||||
await EnsureMemberAccessAsync(userId, projectId, ct);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.Title))
|
||||
{
|
||||
throw new BadRequestException("Task title is required");
|
||||
}
|
||||
|
||||
if (request.AssigneeId.HasValue && !await IsMemberAsync(request.AssigneeId.Value, projectId, ct))
|
||||
{
|
||||
throw new BadRequestException("Assignee must be a member of the project");
|
||||
}
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var task = new TaskItem
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
ProjectId = projectId,
|
||||
Title = request.Title.Trim(),
|
||||
Description = request.Description,
|
||||
Status = request.Status ?? TaskStatus.Todo,
|
||||
Priority = request.Priority ?? TaskPriority.Medium,
|
||||
AssigneeId = request.AssigneeId,
|
||||
DueDate = request.DueDate,
|
||||
CreatedBy = userId,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
};
|
||||
|
||||
uow.Tasks.Add(task);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return await GetDtoAsync(task.Id, ct);
|
||||
}
|
||||
|
||||
public async Task<TaskDto> UpdateAsync(Guid userId, Guid taskId, UpdateTaskRequest request, CancellationToken ct = default)
|
||||
{
|
||||
var task = await GetTaskForUserAsync(userId, taskId, ct);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.Title))
|
||||
{
|
||||
throw new BadRequestException("Task title is required");
|
||||
}
|
||||
|
||||
if (request.AssigneeId.HasValue && !await IsMemberAsync(request.AssigneeId.Value, task.ProjectId, ct))
|
||||
{
|
||||
throw new BadRequestException("Assignee must be a member of the project");
|
||||
}
|
||||
|
||||
task.Title = request.Title.Trim();
|
||||
task.Description = request.Description;
|
||||
task.Status = request.Status;
|
||||
task.Priority = request.Priority;
|
||||
task.AssigneeId = request.AssigneeId;
|
||||
task.DueDate = request.DueDate;
|
||||
task.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return await GetDtoAsync(task.Id, ct);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid userId, Guid taskId, CancellationToken ct = default)
|
||||
{
|
||||
var task = await GetTaskForUserAsync(userId, taskId, ct);
|
||||
uow.Tasks.Remove(task);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<List<TaskDto>> SearchAsync(Guid userId, string term, CancellationToken ct = default)
|
||||
{
|
||||
var projectIds = await uow.Projects.GetProjectIdsForUserAsync(userId, ct);
|
||||
var tasks = await uow.Tasks.SearchWithAssigneeAsync(projectIds, term, 20, ct);
|
||||
return mapper.Map<List<TaskDto>>(tasks);
|
||||
}
|
||||
|
||||
private async Task<TaskDto> GetDtoAsync(Guid id, CancellationToken ct)
|
||||
{
|
||||
var task = await uow.Tasks.GetWithAssigneeAsync(id, ct)
|
||||
?? throw new NotFoundException("Task not found");
|
||||
return mapper.Map<TaskDto>(task);
|
||||
}
|
||||
|
||||
private async Task<TaskItem> GetTaskForUserAsync(Guid userId, Guid taskId, CancellationToken ct = default)
|
||||
{
|
||||
var task = await uow.Tasks.GetByIdAsync(taskId, ct)
|
||||
?? throw new NotFoundException("Task not found");
|
||||
|
||||
if (!await IsMemberAsync(userId, task.ProjectId, ct))
|
||||
{
|
||||
throw new NotFoundException("Task not found");
|
||||
}
|
||||
return task;
|
||||
}
|
||||
|
||||
private async Task EnsureMemberAccessAsync(Guid userId, Guid projectId, CancellationToken ct = default)
|
||||
{
|
||||
if (!await IsMemberAsync(userId, projectId, ct))
|
||||
{
|
||||
throw new NotFoundException("Project not found");
|
||||
}
|
||||
}
|
||||
|
||||
private Task<bool> IsMemberAsync(Guid userId, Guid projectId, CancellationToken ct = default) =>
|
||||
uow.Projects.IsMemberAsync(projectId, userId, ct);
|
||||
}
|
||||
Reference in New Issue
Block a user