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.
89 lines
4.6 KiB
C#
89 lines
4.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Mws.Application.Common.Repositories;
|
|
using Mws.Domain.Projects;
|
|
|
|
namespace Mws.Infrastructure.Persistence.Repositories;
|
|
|
|
public class ProjectRepository(AppDbContext db) : RepositoryBase<Project>(db), IProjectRepository
|
|
{
|
|
public Task<Project?> GetByIdAsync(Guid id, CancellationToken ct = default) =>
|
|
Set.FirstOrDefaultAsync(p => p.Id == id, ct);
|
|
|
|
public Task<Project?> GetForUserAsync(Guid userId, Guid projectId, CancellationToken ct = default) =>
|
|
Set.FirstOrDefaultAsync(p => p.Id == projectId && p.Members.Any(m => m.UserId == userId), ct);
|
|
|
|
public Task<List<Project>> GetForUserAsync(Guid userId, CancellationToken ct = default) =>
|
|
Set.Where(p => p.Members.Any(m => m.UserId == userId))
|
|
.OrderByDescending(p => p.UpdatedAt)
|
|
.ToListAsync(ct);
|
|
|
|
public Task<List<Project>> SearchForUserAsync(Guid userId, string? term, CancellationToken ct = default)
|
|
{
|
|
var query = Set.Where(p => p.Members.Any(m => m.UserId == userId));
|
|
if (!string.IsNullOrWhiteSpace(term))
|
|
{
|
|
var lower = term.Trim().ToLower();
|
|
query = query.Where(p => p.Name.ToLower().Contains(lower));
|
|
}
|
|
|
|
return query.OrderByDescending(p => p.UpdatedAt).ToListAsync(ct);
|
|
}
|
|
|
|
public Task<int> CountMembersAsync(Guid projectId, CancellationToken ct = default) =>
|
|
Db.ProjectMembers.CountAsync(m => m.ProjectId == projectId, ct);
|
|
|
|
public Task<bool> IsMemberAsync(Guid projectId, Guid userId, CancellationToken ct = default) =>
|
|
Db.ProjectMembers.AnyAsync(m => m.ProjectId == projectId && m.UserId == userId, ct);
|
|
|
|
public Task<MemberRole?> GetMemberRoleAsync(Guid projectId, Guid userId, CancellationToken ct = default) =>
|
|
Db.ProjectMembers
|
|
.Where(m => m.ProjectId == projectId && m.UserId == userId)
|
|
.Select(m => (MemberRole?)m.Role)
|
|
.SingleOrDefaultAsync(ct);
|
|
|
|
public Task<ProjectMember?> GetMemberAsync(Guid projectId, Guid userId, CancellationToken ct = default) =>
|
|
Db.ProjectMembers.FirstOrDefaultAsync(m => m.ProjectId == projectId && m.UserId == userId, ct);
|
|
|
|
public Task<ProjectMember?> GetMemberWithUserAsync(Guid projectId, Guid userId, CancellationToken ct = default) =>
|
|
Db.ProjectMembers.Include(m => m.User).FirstOrDefaultAsync(m => m.ProjectId == projectId && m.UserId == userId, ct);
|
|
|
|
public Task<List<ProjectMember>> GetMembersWithUserAsync(Guid projectId, CancellationToken ct = default) =>
|
|
Db.ProjectMembers
|
|
.Where(m => m.ProjectId == projectId)
|
|
.Include(m => m.User)
|
|
.OrderByDescending(m => m.Role)
|
|
.ThenBy(m => m.User.DisplayName)
|
|
.ToListAsync(ct);
|
|
|
|
public Task<int> CountOwnersAsync(Guid projectId, CancellationToken ct = default) =>
|
|
Db.ProjectMembers.CountAsync(m => m.ProjectId == projectId && m.Role == MemberRole.Owner, ct);
|
|
|
|
public Task<List<Guid>> GetProjectIdsForUserAsync(Guid userId, CancellationToken ct = default) =>
|
|
Set.Where(p => p.Members.Any(m => m.UserId == userId)).Select(p => p.Id).ToListAsync(ct);
|
|
|
|
public Task<List<Guid>> GetOwnedProjectIdsAsync(Guid userId, CancellationToken ct = default) =>
|
|
Db.ProjectMembers.Where(m => m.UserId == userId && m.Role == MemberRole.Owner).Select(m => m.ProjectId).ToListAsync(ct);
|
|
|
|
public Task<List<Guid>> GetDocumentViewableProjectIdsAsync(Guid userId, CancellationToken ct = default) =>
|
|
Db.ProjectMembers
|
|
.Where(m => m.UserId == userId &&
|
|
(m.Role == MemberRole.Owner ||
|
|
Db.ProjectMemberPermissions.Any(p => p.ProjectId == m.ProjectId && p.UserId == userId && p.Screen == ProjectPermissionScreens.Documents && p.CanView)))
|
|
.Select(m => m.ProjectId)
|
|
.ToListAsync(ct);
|
|
|
|
public void AddMember(ProjectMember member) => Db.ProjectMembers.Add(member);
|
|
|
|
public void RemoveMember(ProjectMember member) => Db.ProjectMembers.Remove(member);
|
|
|
|
public Task<ProjectMemberPermission?> GetMemberPermissionAsync(Guid projectId, Guid userId, string screen, CancellationToken ct = default) =>
|
|
Db.ProjectMemberPermissions.FirstOrDefaultAsync(p => p.ProjectId == projectId && p.UserId == userId && p.Screen == screen, ct);
|
|
|
|
public Task<Dictionary<Guid, ProjectMemberPermission>> GetMemberPermissionsAsync(Guid projectId, string screen, CancellationToken ct = default) =>
|
|
Db.ProjectMemberPermissions
|
|
.Where(p => p.ProjectId == projectId && p.Screen == screen)
|
|
.ToDictionaryAsync(p => p.UserId, ct);
|
|
|
|
public void AddMemberPermission(ProjectMemberPermission permission) => Db.ProjectMemberPermissions.Add(permission);
|
|
}
|