ref: project structure
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.domain.Tasks;
|
||||
using TaskPriority = mws.backend.dotnet.domain.Tasks.TaskPriority;
|
||||
using TaskStatus = mws.backend.dotnet.domain.Tasks.TaskStatus;
|
||||
|
||||
namespace mws.backend.dotnet.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.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.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.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using mws.backend.dotnet.domain.Tasks;
|
||||
using TaskPriority = mws.backend.dotnet.domain.Tasks.TaskPriority;
|
||||
using TaskStatus = mws.backend.dotnet.domain.Tasks.TaskStatus;
|
||||
|
||||
namespace mws.backend.dotnet.application.Tasks;
|
||||
|
||||
public class CreateTaskRequest
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public TaskStatus? Status { get; set; }
|
||||
public TaskPriority? Priority { get; set; }
|
||||
public Guid? AssigneeId { get; set; }
|
||||
public DateTime? DueDate { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateTaskRequest
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public TaskStatus Status { get; set; } = TaskStatus.Todo;
|
||||
public TaskPriority Priority { get; set; } = TaskPriority.Medium;
|
||||
public Guid? AssigneeId { get; set; }
|
||||
public DateTime? DueDate { get; set; }
|
||||
}
|
||||
|
||||
public class TaskDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid ProjectId { get; set; }
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public TaskStatus Status { get; set; }
|
||||
public TaskPriority Priority { get; set; }
|
||||
public Guid? AssigneeId { get; set; }
|
||||
public string? AssigneeName { get; set; }
|
||||
public DateTime? DueDate { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.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,27 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using TaskPriority = mws.backend.dotnet.domain.Tasks.TaskPriority;
|
||||
using TaskStatus = mws.backend.dotnet.domain.Tasks.TaskStatus;
|
||||
|
||||
namespace mws.backend.dotnet.application.Tasks;
|
||||
|
||||
public record GetTasksQuery(Guid UserId, Guid ProjectId, TaskStatus? Status, TaskPriority? Priority, Guid? AssigneeId, int Page, int PageSize)
|
||||
: IRequest<PagedResult<TaskDto>>;
|
||||
|
||||
public class GetTasksHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<GetTasksQuery, PagedResult<TaskDto>>
|
||||
{
|
||||
public async Task<PagedResult<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, query.Page, query.PageSize, ct);
|
||||
return new PagedResult<TaskDto>
|
||||
{
|
||||
Items = mapper.Map<List<TaskDto>>(tasks.Items),
|
||||
TotalCount = tasks.TotalCount,
|
||||
Page = tasks.Page,
|
||||
PageSize = tasks.PageSize,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.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.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.domain.Tasks;
|
||||
|
||||
namespace mws.backend.dotnet.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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user