From bca1447ad47f46d8c5c6bf442b8f52de55137bfa Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Fri, 9 Aug 2024 20:03:12 +0330 Subject: [PATCH] fix(GetShiftPlanSortByShift) --- .../CreateShiftPlanCommandHandler.cs | 28 ++++++++----------- .../ShiftPlans/GetShiftPlansQueryHandler.cs | 28 +++++++++++-------- .../Handlers/Users/GetStaffQueryHandler.cs | 6 ++++ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/Brizco.Repository/Handlers/ShiftPlans/CreateShiftPlanCommandHandler.cs b/Brizco.Repository/Handlers/ShiftPlans/CreateShiftPlanCommandHandler.cs index 9b13603..6f7bda2 100644 --- a/Brizco.Repository/Handlers/ShiftPlans/CreateShiftPlanCommandHandler.cs +++ b/Brizco.Repository/Handlers/ShiftPlans/CreateShiftPlanCommandHandler.cs @@ -1,24 +1,20 @@ namespace Brizco.Repository.Handlers.ShiftPlans; -public class CreateShiftPlanCommandHandler : IRequestHandler +public class CreateShiftPlanCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService) + : IRequestHandler { - private readonly IRepositoryWrapper _repositoryWrapper; - private readonly ICurrentUserService _currentUserService; + private readonly ICurrentUserService _currentUserService = currentUserService; - public CreateShiftPlanCommandHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService) - { - _repositoryWrapper = repositoryWrapper; - _currentUserService = currentUserService; - } public async Task Handle(CreateShiftPlanCommand request, CancellationToken cancellationToken) { - var shift = await _repositoryWrapper.SetRepository() + var shift = await repositoryWrapper.SetRepository() .TableNoTracking .FirstOrDefaultAsync(s => s.Id == request.ShiftId, cancellationToken); if (shift == null) throw new AppException("Shift not found", ApiResultStatusCode.NotFound); - var planFor = DateTimeExtensions.UnixTimeStampToDateTime(request.PlanDate); - var existedShiftPlan = await _repositoryWrapper.SetRepository() + var planDate = DateTimeExtensions.UnixTimeStampToDateTime(request.PlanDate); + var planFor = new DateTime(planDate.Year, planDate.Month, planDate.Day, shift.StartAt.Hours, shift.StartAt.Minutes, shift.StartAt.Seconds); + var existedShiftPlan = await repositoryWrapper.SetRepository() .TableNoTracking .FirstOrDefaultAsync(s => s.ShiftId == request.ShiftId && s.PlanFor.Date == planFor.Date, cancellationToken); @@ -27,7 +23,7 @@ public class CreateShiftPlanCommandHandler : IRequestHandler().Add(shiftPlan); - await _repositoryWrapper.SaveChangesAsync(cancellationToken); - await _repositoryWrapper.CommitAsync(cancellationToken); + repositoryWrapper.SetRepository().Add(shiftPlan); + await repositoryWrapper.SaveChangesAsync(cancellationToken); + await repositoryWrapper.CommitAsync(cancellationToken); return shiftPlan.AdaptToLDto(); } catch (Exception ) { - await _repositoryWrapper.RollBackAsync(cancellationToken); + await repositoryWrapper.RollBackAsync(cancellationToken); throw; } } diff --git a/Brizco.Repository/Handlers/ShiftPlans/GetShiftPlansQueryHandler.cs b/Brizco.Repository/Handlers/ShiftPlans/GetShiftPlansQueryHandler.cs index 6e22766..24a6793 100644 --- a/Brizco.Repository/Handlers/ShiftPlans/GetShiftPlansQueryHandler.cs +++ b/Brizco.Repository/Handlers/ShiftPlans/GetShiftPlansQueryHandler.cs @@ -17,7 +17,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu .Where(s => s.ComplexId == complexId); List shiftPlans = new List(); - bool setActiviesCount = false; + bool setActivitiesCount = false; if (request.SelectedDate == 0) { @@ -34,13 +34,13 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu .Skip(request.Page * 15).Take(15) .Select(ShiftPlanMapper.ProjectToSDto) .ToListAsync(cancellationToken); - setActiviesCount = true; + setActivitiesCount = true; } if (request.DateTimeQueryFilter != null) { - setActiviesCount = true; + setActivitiesCount = true; switch (request.DateTimeQueryFilter) { case DateTimeQueryFilter.Today: @@ -62,16 +62,21 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu DateTime startOfPastWeek = DateTime.Today.AddDays(pastWeekDelta).AddDays(-7); DateTime endOfPastWeek = startOfPastWeek.AddDays(6); shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfPastWeek.Date && s.PlanFor.Date <= endOfPastWeek.Date) - .OrderByDescending(s => s.CreatedAt) + .OrderByDescending(s => s.PlanFor) .Skip(request.Page * 15).Take(15) .Select(ShiftPlanMapper.ProjectToSDto) .ToListAsync(cancellationToken); break; case DateTimeQueryFilter.NextWeek: - DateTime startOfNextWeek = DateTime.Today.AddDays(7 - (int)DateTime.Today.DayOfWeek + 1); + + DateTime nextWeekToday = DateTime.Today; + int nextWeekDelta = DayOfWeek.Saturday - nextWeekToday.DayOfWeek; + if (nextWeekDelta > 0) nextWeekDelta -= 7; + DateTime startOfNextWeek = DateTime.Today.AddDays(nextWeekDelta).AddDays(+7); DateTime endOfNextWeek = startOfNextWeek.AddDays(6); + shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfNextWeek.Date && s.PlanFor.Date <= endOfNextWeek.Date) - .OrderByDescending(s => s.CreatedAt) + .OrderByDescending(s => s.PlanFor) .Skip(request.Page * 15).Take(15) .Select(ShiftPlanMapper.ProjectToSDto) .ToListAsync(cancellationToken); @@ -83,7 +88,8 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu DateTime startOfWeek = today.AddDays(delta); DateTime endOfWeek = startOfWeek.AddDays(6); shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfWeek.Date && s.PlanFor.Date <= endOfWeek.Date) - .OrderByDescending(s => s.CreatedAt) + .OrderBy(s=>s.PlanFor.TimeOfDay) + .ThenByDescending(s => s.PlanFor.Date) .Skip(request.Page * 15).Take(15) .Select(ShiftPlanMapper.ProjectToSDto) .ToListAsync(cancellationToken); @@ -93,7 +99,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu DateTime startOfPastMonth = new PersianDateTime(PersianDateTime.Today.AddMonths(-1).Year, PersianDateTime.Today.AddMonths(-1).Month, 1).ToDateTime(); DateTime endOfPastMonth = startOfPastMonth.AddMonths(1); shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfPastMonth.Date && s.PlanFor.Date < endOfPastMonth.Date) - .OrderByDescending(s => s.CreatedAt) + .OrderByDescending(s => s.PlanFor) .Skip(request.Page * 15).Take(15) .Select(ShiftPlanMapper.ProjectToSDto) .ToListAsync(cancellationToken); @@ -102,7 +108,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu DateTime startOfNextMonth = new PersianDateTime(PersianDateTime.Today.AddMonths(1).Year, PersianDateTime.Today.AddMonths(1).Month, 1).ToDateTime(); DateTime endOfNextMonth = startOfNextMonth.AddMonths(1); shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfNextMonth.Date && s.PlanFor.Date < endOfNextMonth.Date) - .OrderByDescending(s => s.CreatedAt) + .OrderByDescending(s => s.PlanFor) .Skip(request.Page * 15).Take(15) .Select(ShiftPlanMapper.ProjectToSDto) .ToListAsync(cancellationToken); @@ -112,7 +118,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu DateTime startOfThisMonth = new PersianDateTime(PersianDateTime.Today.Year, PersianDateTime.Today.Month, 1).ToDateTime(); DateTime endOfThisMonth = startOfThisMonth.AddMonths(1); shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfThisMonth.Date && s.PlanFor.Date < endOfThisMonth.Date) - .OrderByDescending(s => s.CreatedAt) + .OrderByDescending(s => s.PlanFor) .Skip(request.Page * 15).Take(15) .Select(ShiftPlanMapper.ProjectToSDto) .ToListAsync(cancellationToken); @@ -125,7 +131,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu } } - if (setActiviesCount) + if (setActivitiesCount) { foreach (var shiftPlan in shiftPlans) diff --git a/Brizco.Repository/Handlers/Users/GetStaffQueryHandler.cs b/Brizco.Repository/Handlers/Users/GetStaffQueryHandler.cs index c892d7e..03f64e8 100644 --- a/Brizco.Repository/Handlers/Users/GetStaffQueryHandler.cs +++ b/Brizco.Repository/Handlers/Users/GetStaffQueryHandler.cs @@ -34,6 +34,12 @@ public class GetStaffQueryHandler(IRepositoryWrapper repositoryWrapper,ICurrentU .Select(ActivityMapper.ProjectToSDto) .ToListAsync(cancellationToken); break; + case DateTimeQueryFilter.Tomorrow: + activities = await baseQuery.Where(s => s.SetFor.Date == DateTime.Now.AddDays(1).Date) + .Skip(request.Page * count).Take(count) + .Select(ActivityMapper.ProjectToSDto) + .ToListAsync(cancellationToken); + break; case DateTimeQueryFilter.PastWeek: DateTime pastWeekToday = DateTime.Today; int pastWeekDelta = DayOfWeek.Saturday - pastWeekToday.DayOfWeek;