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.
This commit is contained in:
2026-08-13 23:10:22 +07:00
parent 96944ad2c3
commit f72aaa2329
94 changed files with 7758 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
using Mws.Domain.Projects;
namespace Mws.Application.Projects;
public class CreateProjectRequest
{
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
}
public class UpdateProjectRequest
{
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public ProjectStatus Status { get; set; } = ProjectStatus.Active;
}
public class AddMemberRequest
{
public Guid UserId { get; set; }
public MemberRole Role { get; set; } = MemberRole.Member;
}
public class ProjectMemberDto
{
public Guid UserId { get; set; }
public string Username { get; set; } = string.Empty;
public string DisplayName { get; set; } = string.Empty;
public MemberRole Role { get; set; }
public bool CanViewDocuments { get; set; }
public bool CanCreateDocuments { get; set; }
public bool CanEditDocuments { get; set; }
public bool CanDeleteDocuments { get; set; }
}
public class UpdateMemberDocumentPermissionsRequest
{
public bool CanViewDocuments { get; set; }
public bool CanCreateDocuments { get; set; }
public bool CanEditDocuments { get; set; }
public bool CanDeleteDocuments { get; set; }
}
public class ProjectDto
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public ProjectStatus Status { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class ProjectOverviewDto
{
public ProjectDto Project { get; set; } = null!;
public int MemberCount { get; set; }
public int DocumentCount { get; set; }
public Dictionary<string, int> TaskCountsByStatus { get; set; } = [];
public List<RecentTaskDto> RecentTasks { get; set; } = [];
public List<RecentDocumentDto> RecentDocuments { get; set; } = [];
}
public class RecentTaskDto
{
public Guid Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Status { get; set; } = string.Empty;
public DateTime UpdatedAt { get; set; }
}
public class RecentDocumentDto
{
public Guid Id { get; set; }
public string Title { get; set; } = string.Empty;
public DateTime UpdatedAt { get; set; }
}
@@ -0,0 +1,22 @@
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);
Task<ProjectMemberDto> AddMemberAsync(Guid userId, Guid projectId, AddMemberRequest request, CancellationToken ct = default);
Task<ProjectMemberDto> UpdateMemberDocumentPermissionsAsync(Guid userId, Guid projectId, Guid memberUserId, UpdateMemberDocumentPermissionsRequest request, CancellationToken ct = default);
Task RemoveMemberAsync(Guid userId, Guid projectId, Guid memberUserId, CancellationToken ct = default);
}
@@ -0,0 +1,151 @@
using Mws.Application.Common;
using Mws.Domain.Projects;
namespace Mws.Application.Projects;
public class ProjectMemberService(IUnitOfWork uow) : IProjectMemberService
{
public async Task<List<ProjectMemberDto>> GetMembersAsync(Guid userId, Guid projectId, CancellationToken ct = default)
{
var isMember = await uow.Projects.IsMemberAsync(projectId, userId, ct);
if (!isMember)
{
throw new NotFoundException("Project not found");
}
var members = await uow.Projects.GetMembersWithUserAsync(projectId, ct);
var permissions = await uow.Projects.GetMemberPermissionsAsync(projectId, ProjectPermissionScreens.Documents, ct);
return members.Select(m =>
{
permissions.TryGetValue(m.UserId, out var p);
return new ProjectMemberDto
{
UserId = m.UserId,
Username = m.User.Username,
DisplayName = m.User.DisplayName,
Role = m.Role,
CanViewDocuments = m.Role == MemberRole.Owner || (p?.CanView ?? false),
CanCreateDocuments = m.Role == MemberRole.Owner || (p?.CanCreate ?? false),
CanEditDocuments = m.Role == MemberRole.Owner || (p?.CanEdit ?? false),
CanDeleteDocuments = m.Role == MemberRole.Owner || (p?.CanDelete ?? false),
};
}).ToList();
}
public async Task<ProjectMemberDto> AddMemberAsync(Guid userId, Guid projectId, AddMemberRequest request, CancellationToken ct = default)
{
if (await GetMemberRoleAsync(userId, projectId, ct) != MemberRole.Owner)
{
throw new ForbiddenException("Only the project owner can add members");
}
var user = await uow.Users.GetByIdAsync(request.UserId, ct)
?? throw new NotFoundException("User not found");
var already = await uow.Projects.IsMemberAsync(projectId, request.UserId, ct);
if (already)
{
throw new BadRequestException("User is already a member of this project");
}
var role = request.Role is MemberRole.Owner or MemberRole.Member ? request.Role : MemberRole.Member;
var member = new ProjectMember
{
ProjectId = projectId,
UserId = request.UserId,
Role = role,
};
uow.Projects.AddMember(member);
uow.Projects.AddMemberPermission(new ProjectMemberPermission
{
ProjectId = projectId,
UserId = request.UserId,
Screen = ProjectPermissionScreens.Documents,
CanView = true,
CanCreate = role == MemberRole.Owner,
CanEdit = role == MemberRole.Owner,
CanDelete = role == MemberRole.Owner,
});
await uow.SaveChangesAsync(ct);
return new ProjectMemberDto
{
UserId = user.Id,
Username = user.Username,
DisplayName = user.DisplayName,
Role = member.Role,
CanViewDocuments = true,
CanCreateDocuments = role == MemberRole.Owner,
CanEditDocuments = role == MemberRole.Owner,
CanDeleteDocuments = role == MemberRole.Owner,
};
}
public async Task<ProjectMemberDto> UpdateMemberDocumentPermissionsAsync(
Guid userId, Guid projectId, Guid memberUserId, UpdateMemberDocumentPermissionsRequest request, CancellationToken ct = default)
{
if (await GetMemberRoleAsync(userId, projectId, ct) != MemberRole.Owner)
{
throw new ForbiddenException("Only the project owner can change member permissions");
}
var member = await uow.Projects.GetMemberWithUserAsync(projectId, memberUserId, ct)
?? throw new NotFoundException("Member not found in project");
if (member.Role == MemberRole.Owner)
{
throw new BadRequestException("Owner permissions cannot be changed");
}
var permission = await uow.Projects.GetMemberPermissionAsync(projectId, memberUserId, ProjectPermissionScreens.Documents, ct);
if (permission is null)
{
permission = new ProjectMemberPermission { ProjectId = projectId, UserId = memberUserId, Screen = ProjectPermissionScreens.Documents };
uow.Projects.AddMemberPermission(permission);
}
permission.CanView = request.CanViewDocuments;
permission.CanCreate = request.CanCreateDocuments;
permission.CanEdit = request.CanEditDocuments;
permission.CanDelete = request.CanDeleteDocuments;
await uow.SaveChangesAsync(ct);
return new ProjectMemberDto
{
UserId = member.UserId,
Username = member.User.Username,
DisplayName = member.User.DisplayName,
Role = member.Role,
CanViewDocuments = permission.CanView,
CanCreateDocuments = permission.CanCreate,
CanEditDocuments = permission.CanEdit,
CanDeleteDocuments = permission.CanDelete,
};
}
public async Task RemoveMemberAsync(Guid userId, Guid projectId, Guid memberUserId, CancellationToken ct = default)
{
if (await GetMemberRoleAsync(userId, projectId, ct) != MemberRole.Owner)
{
throw new ForbiddenException("Only the project owner can remove members");
}
var member = await uow.Projects.GetMemberAsync(projectId, memberUserId, ct)
?? throw new NotFoundException("Member not found in project");
var owners = await uow.Projects.CountOwnersAsync(projectId, ct);
if (member.Role == MemberRole.Owner && owners <= 1)
{
throw new BadRequestException("Cannot remove the last owner of the project");
}
uow.Projects.RemoveMember(member);
await uow.SaveChangesAsync(ct);
}
private Task<MemberRole?> GetMemberRoleAsync(Guid userId, Guid projectId, CancellationToken ct = default) =>
uow.Projects.GetMemberRoleAsync(projectId, userId, ct);
}
+127
View File
@@ -0,0 +1,127 @@
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);
}