2026-08-13 23:10:22 +07:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-08-19 23:25:08 +07:00
|
|
|
using mws.backend.dotnet.application.Common;
|
|
|
|
|
using mws.backend.dotnet.application.Common.Repositories;
|
|
|
|
|
using mws.backend.dotnet.domain.Tasks;
|
|
|
|
|
using TaskPriority = mws.backend.dotnet.domain.Tasks.TaskPriority;
|
|
|
|
|
using TaskStatus = mws.backend.dotnet.domain.Tasks.TaskStatus;
|
2026-08-13 23:10:22 +07:00
|
|
|
|
2026-08-19 23:25:08 +07:00
|
|
|
namespace mws.backend.dotnet.infrastructure.Persistence.Repositories;
|
2026-08-13 23:10:22 +07:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2026-08-19 23:25:08 +07:00
|
|
|
public Task<PagedResult<TaskItem>> GetForProjectAsync(
|
|
|
|
|
Guid projectId, TaskStatus? status, TaskPriority? priority, Guid? assigneeId, int page, int pageSize, CancellationToken ct = default)
|
2026-08-13 23:10:22 +07:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-19 23:25:08 +07:00
|
|
|
return query.Include(t => t.Assignee).OrderByDescending(t => t.UpdatedAt).ToPagedResultAsync(page, pageSize, ct);
|
2026-08-13 23:10:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|