Api/Brizco.Repository/Handlers/Routines/GetRoutineShiftsQueryHandle...

55 lines
2.2 KiB
C#
Raw Normal View History

namespace Brizco.Repository.Handlers.Routines;
2023-11-19 22:06:49 +03:30
2024-08-09 21:42:04 +03:30
public class GetRoutineShiftsQueryHandler(IRepositoryWrapper repositoryWrapper)
: IRequestHandler<GetRoutineShiftsQuery, List<RoutineShiftResponseDto>>
2023-11-19 22:06:49 +03:30
{
public async Task<List<RoutineShiftResponseDto>> Handle(GetRoutineShiftsQuery request, CancellationToken cancellationToken)
{
var routineShiftResponse = new List<RoutineShiftResponseDto>();
2024-08-09 21:42:04 +03:30
var shiftRoutines = await repositoryWrapper.SetRepository<ShiftRoutine>()
2023-11-19 22:06:49 +03:30
.TableNoTracking
.Where(s => s.RoutineId == request.Id)
.ToListAsync(cancellationToken);
foreach (var shiftRoutine in shiftRoutines)
{
2024-08-09 21:42:04 +03:30
var shift = await repositoryWrapper.SetRepository<Shift>()
2023-11-19 22:06:49 +03:30
.TableNoTracking
.Where(s => s.Id == shiftRoutine.ShiftId)
.Select(ShiftMapper.ProjectToSDto)
.FirstOrDefaultAsync(cancellationToken);
if (shift == null)
continue;
shift.Days.ForEach(d =>
2023-11-19 22:06:49 +03:30
{
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
});
}
});
if (request.SelectedDate > 0)
{
var selectedDate = DateTimeExtensions.UnixTimeStampToDateTime(request.SelectedDate);
2024-08-09 21:42:04 +03:30
var existedShiftPlan = await repositoryWrapper.SetRepository<ShiftPlan>()
.TableNoTracking
.FirstOrDefaultAsync(s => s.ShiftId == shift.Id && s.PlanFor.Date == selectedDate.Date, cancellationToken);
shift.IsCompleted = existedShiftPlan?.IsCompleted ?? false;
shift.CurrentShiftPlanId = existedShiftPlan?.Id ?? default;
shift.HasCurrentShiftPlan = existedShiftPlan != null;
}
2023-11-19 22:06:49 +03:30
}
return routineShiftResponse;
}
}