61 lines
2.4 KiB
C#
61 lines
2.4 KiB
C#
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<TaskItem>(db), ITaskRepository
|
||
|
|
{
|
||
|
|
public Task<TaskItem?> GetByIdAsync(Guid id, CancellationToken ct = default) =>
|
||
|
|
Set.FirstOrDefaultAsync(t => t.Id == id, ct);
|
||
|
|
|
||
|
|
public Task<TaskItem?> GetWithAssigneeAsync(Guid id, CancellationToken ct = default) =>
|
||
|
|
Set.Include(t => t.Assignee).FirstOrDefaultAsync(t => t.Id == id, ct);
|
||
|
|
|
||
|
|
public Task<List<TaskItem>> 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<List<TaskItem>> SearchWithAssigneeAsync(List<Guid> 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<Dictionary<TaskStatus, int>> 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<List<TaskItem>> GetRecentForProjectAsync(Guid projectId, int take, CancellationToken ct = default) =>
|
||
|
|
Set.Where(t => t.ProjectId == projectId).OrderByDescending(t => t.UpdatedAt).Take(take).ToListAsync(ct);
|
||
|
|
}
|