Api/Brizco.Repository/Handlers/Routine/GetRoutineShiftsQueryHandle...

49 lines
1.7 KiB
C#

using Brizco.Domain.Entities.Shift;
namespace Brizco.Repository.Handlers.Routine;
public class GetRoutineShiftsQueryHandler : IRequestHandler<GetRoutineShiftsQuery,List<RoutineShiftResponseDto>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
public GetRoutineShiftsQueryHandler(IRepositoryWrapper repositoryWrapper)
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<List<RoutineShiftResponseDto>> Handle(GetRoutineShiftsQuery request, CancellationToken cancellationToken)
{
var routineShiftResponse = new List<RoutineShiftResponseDto>();
var shiftRoutines = await _repositoryWrapper.SetRepository<ShiftRoutine>()
.TableNoTracking
.Where(s => s.RoutineId == request.Id)
.ToListAsync(cancellationToken);
foreach (var shiftRoutine in shiftRoutines)
{
var shift = await _repositoryWrapper.SetRepository<Domain.Entities.Shift.Shift>()
.TableNoTracking
.Where(s => s.Id == shiftRoutine.ShiftId)
.Select(ShiftMapper.ProjectToSDto)
.FirstOrDefaultAsync(cancellationToken);
shift?.Days.ForEach(d =>
{
var routineShiftRes = routineShiftResponse.FirstOrDefault(s => s.Day == d);
if (routineShiftRes != null)
{
routineShiftRes.Shifts.Add(shift);
}
else
{
routineShiftResponse.Add(new RoutineShiftResponseDto
{
Shifts = new List<ShiftSDto>{shift},
Day = d
});
}
});
}
return routineShiftResponse;
}
}