Files
mws.backend.dotnet/mws.application/Projects/ProjectService.cs
T
namdh f72aaa2329 Commit MWS backend source tree
The ASP.NET Core solution (mws.api/mws.application/mws.domain/
mws.infrastructure) existed only as untracked working files. Adding
it to version control, plus a .gitignore for build output and local
tooling directories, so the CQRS/mediator refactor plan has a real
git history to branch and diff against.
2026-08-13 23:10:22 +07:00

128 lines
4.8 KiB
C#

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);
}