2026-04-18 19:25:35 -03:00
|
|
|
using SIGCM2.Application.Abstractions;
|
|
|
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
|
|
|
|
using SIGCM2.Application.Rubros.Common;
|
|
|
|
|
using SIGCM2.Application.Rubros.Dtos;
|
|
|
|
|
|
|
|
|
|
namespace SIGCM2.Application.Rubros.GetTree;
|
|
|
|
|
|
|
|
|
|
public sealed class GetRubroTreeQueryHandler : ICommandHandler<GetRubroTreeQuery, IReadOnlyList<RubroTreeNodeDto>>
|
|
|
|
|
{
|
|
|
|
|
private readonly IRubroRepository _repo;
|
2026-04-19 08:25:13 -03:00
|
|
|
private readonly IAvisoQueryRepository _avisoQuery;
|
2026-04-18 19:25:35 -03:00
|
|
|
|
2026-04-19 08:25:13 -03:00
|
|
|
public GetRubroTreeQueryHandler(IRubroRepository repo, IAvisoQueryRepository avisoQuery)
|
2026-04-18 19:25:35 -03:00
|
|
|
{
|
|
|
|
|
_repo = repo;
|
2026-04-19 08:25:13 -03:00
|
|
|
_avisoQuery = avisoQuery;
|
2026-04-18 19:25:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<RubroTreeNodeDto>> Handle(GetRubroTreeQuery query)
|
|
|
|
|
{
|
|
|
|
|
var all = await _repo.GetAllAsync(query.IncluirInactivos);
|
2026-04-19 08:25:13 -03:00
|
|
|
var ids = all.Select(r => r.Id).ToList();
|
|
|
|
|
// CAT-002: single batch call — avoids N+1 when PRD-002 activates the real implementation
|
|
|
|
|
var avisoCounts = await _avisoQuery.CountAvisosBatchAsync(ids);
|
|
|
|
|
return RubroTreeBuilder.Build(all, query.IncluirInactivos, avisoCounts);
|
2026-04-18 19:25:35 -03:00
|
|
|
}
|
|
|
|
|
}
|