using Microsoft.EntityFrameworkCore; using Mws.Application.Common.Repositories; using Mws.Domain.Documents; namespace Mws.Infrastructure.Persistence.Repositories; public class DocumentRepository(AppDbContext db) : RepositoryBase(db), IDocumentRepository { public Task GetByIdAsync(Guid id, CancellationToken ct = default) => Set.FirstOrDefaultAsync(d => d.Id == id, ct); public Task> GetTreeForProjectAsync(Guid projectId, CancellationToken ct = default) => Set.Where(d => d.ProjectId == projectId).OrderBy(d => d.Title).ToListAsync(ct); public Task GetInProjectAsync(Guid parentId, Guid projectId, CancellationToken ct = default) => Set.FirstOrDefaultAsync(d => d.Id == parentId && d.ProjectId == projectId, ct); public Task GetParentIdAsync(Guid documentId, CancellationToken ct = default) => Set.Where(d => d.Id == documentId).Select(d => d.ParentId).SingleAsync(ct); public Task> GetChildrenAsync(Guid parentId, CancellationToken ct = default) => Set.Where(d => d.ParentId == parentId).ToListAsync(ct); public Task> SearchAsync(List projectIds, string? term, int take, CancellationToken ct = default) { var query = Set.Where(d => projectIds.Contains(d.ProjectId)); if (!string.IsNullOrWhiteSpace(term)) { var lower = term.Trim().ToLower(); query = query.Where(d => d.Title.ToLower().Contains(lower)); } return query.OrderByDescending(d => d.UpdatedAt).Take(take).ToListAsync(ct); } public Task CountByTypeForProjectAsync(Guid projectId, DocumentType type, CancellationToken ct = default) => Set.CountAsync(d => d.ProjectId == projectId && d.Type == type, ct); public Task> GetRecentForProjectAsync(Guid projectId, DocumentType type, int take, CancellationToken ct = default) => Set.Where(d => d.ProjectId == projectId && d.Type == type).OrderByDescending(d => d.UpdatedAt).Take(take).ToListAsync(ct); }