using Microsoft.EntityFrameworkCore; using Mws.Application.Common.Repositories; using Mws.Domain.Tasks; using TaskPriority = Mws.Domain.Tasks.TaskPriority; using TaskStatus = Mws.Domain.Tasks.TaskStatus; namespace Mws.Infrastructure.Persistence.Repositories; public class TaskRepository(AppDbContext db) : RepositoryBase(db), ITaskRepository { public Task GetByIdAsync(Guid id, CancellationToken ct = default) => Set.FirstOrDefaultAsync(t => t.Id == id, ct); public Task GetWithAssigneeAsync(Guid id, CancellationToken ct = default) => Set.Include(t => t.Assignee).FirstOrDefaultAsync(t => t.Id == id, ct); public Task> GetForProjectAsync( Guid projectId, TaskStatus? status, TaskPriority? priority, Guid? assigneeId, CancellationToken ct = default) { var query = Set.Where(t => t.ProjectId == projectId); if (status.HasValue) { query = query.Where(t => t.Status == status); } if (priority.HasValue) { query = query.Where(t => t.Priority == priority); } if (assigneeId.HasValue) { query = query.Where(t => t.AssigneeId == assigneeId); } return query.Include(t => t.Assignee).OrderByDescending(t => t.UpdatedAt).ToListAsync(ct); } public Task> SearchWithAssigneeAsync(List projectIds, string? term, int take, CancellationToken ct = default) { var query = Set.Where(t => projectIds.Contains(t.ProjectId)); if (!string.IsNullOrWhiteSpace(term)) { var lower = term.Trim().ToLower(); query = query.Where(t => t.Title.ToLower().Contains(lower)); } return query.Include(t => t.Assignee).OrderByDescending(t => t.UpdatedAt).Take(take).ToListAsync(ct); } public async Task> CountByStatusForProjectAsync(Guid projectId, CancellationToken ct = default) { return await Set.Where(t => t.ProjectId == projectId) .GroupBy(t => t.Status) .Select(g => new { Status = g.Key, Count = g.Count() }) .ToDictionaryAsync(a => a.Status, a => a.Count, ct); } public Task> GetRecentForProjectAsync(Guid projectId, int take, CancellationToken ct = default) => Set.Where(t => t.ProjectId == projectId).OrderByDescending(t => t.UpdatedAt).Take(take).ToListAsync(ct); }