public async Task <ActionResult <UULResponse> > GetTimeSlots(int year, int month, int day) { UULResponse response; try { var rulesDto = await RulesDao.GetCurrentRulesDTOOrDefault(_context); if (rulesDto == null) { return(Error.RulesNotFound.CreateErrorResponse(_logger, "GetTimeSlots")); } DateOperations.GetTimeSlotsBoundsUtc(rulesDto.TimeSlotSpan, year, month, day, out DateTime start, out DateTime end); var slots = await TimeSlotsDao.GetTimeSlotsByUtcBounds(_context, start, end); var data = new ScheduleDTO() { Date = year + "/" + month + "/" + day, GymId = null, TimeSlots = slots }; response = new UULResponse() { Success = true, Message = year + "/" + month + "/" + day, Data = data }; } catch (Exception e) { response = Error.TimeSlotsGetFailed.CreateErrorResponse(_logger, "GetTimeSlots", e); } return(response); }
public async Task <ActionResult <UULResponse> > GetRules() { UULResponse response; try { var rulesDTO = await RulesDao.GetCurrentRulesDTOOrDefault(_context); response = new UULResponse() { Success = true, Message = "Active Rules", Data = rulesDTO }; } catch (Exception e) { response = Error.RulesGetFailed.CreateErrorResponse(_logger, "GetRules", e); } return(response); }
private RulesService() { rulesDao = new RulesDao(); }
private async Task <ActionResult <UULResponse> > BookTimeSlotByGym(BookTimeSlotDTO dto, int gymId) { UULResponse response; // TODO refactor to use exceptions var currentUser = HttpContext.User; try { var userInfo = SecHelper.GetUserInfo(currentUser.Claims); var user = await _context.Users.Where(u => u.Login.Equals(userInfo.Login) && u.ApartmentCode.Equals(userInfo.ApartmentCode)).SingleOrDefaultAsync(); if (user is null) { return(Error.ProfileNotFound.CreateErrorResponse(_logger, "BookTimeSlotsByGym")); } if (!user.IsActivated) { return(Error.ProfileNotActivated.CreateErrorResponse(_logger, "BookTimesSlotsByGym")); } var timeSlot = await _context.TimeSlots .Include(t => t.OccupiedBy) .Include(t => t.Gym) .FirstOrDefaultAsync(t => t.ID == dto.TimeslotId); if (timeSlot is null) { return(Error.TimeSlotNotFound.CreateErrorResponse(_logger, "BookTimesSlotsByGym")); } var rulesDto = await RulesDao.GetCurrentRulesDTOOrDefault(_context); if (rulesDto is null) { return(Error.RulesNotFound.CreateErrorResponse(_logger, "BookTimesSlotsByGym")); } DateOperations.GetTodayTimeSlotsBoundsUtc(rulesDto.TimeSlotSpan, out DateTime todayStart, out DateTime todayEnd); if (!timeSlot.Gym.IsOpen) { return(Error.GymClosed.CreateErrorResponse(_logger, "BookTimesSlotsByGym")); } if (!(timeSlot.Start.IsWithinBounds(todayStart, todayEnd))) { return(Error.TimeSlotNotToday.CreateErrorResponse(_logger, "BookTimesSlotsByGym")); } if (timeSlot.OccupiedBy.Count >= rulesDto.PersonsPerTimeSlot) { return(Error.TimeSlotFull.CreateErrorResponse(_logger, "BookTimesSlotsByGym")); } if (await AlreadyBookedInBoundsUTC(dto.HabitantId, todayStart, todayEnd)) { return(Error.TimeSlotOverbooking.CreateErrorResponse(_logger, "BookTimesSlotsByGym")); } Habitant habitant = await _context.Habitants.FindAsync(dto.HabitantId); if (habitant is null) { return(Error.ProfileHabitantLookupFailed.CreateErrorResponse(_logger, "BookTimesSlotsByGym")); } timeSlot.OccupiedBy.Add(habitant); habitant.LastGymVisit = timeSlot.Start; _context.TimeSlots.Update(timeSlot); _context.Habitants.Update(habitant); var success = await _context.SaveChangesAsync() != 0; var slots = gymId == -1 ? await TimeSlotsDao.GetTimeSlotsByUtcBounds(_context, todayStart, todayEnd) : await TimeSlotsDao.GetTimeSlotsByUtcBounds(_context, gymId, todayStart, todayEnd); var data = new ScheduleDTO() { Date = todayStart.Year + "/" + todayStart.Month + "/" + todayStart.Day, GymId = gymId == -1 ? null : gymId, TimeSlots = slots }; response = new UULResponse() { Success = success, Message = "Booked", Data = data }; } catch (Exception e) { response = Error.TimeSlotsBookingFailed.CreateErrorResponse(_logger, "BookTimesSlotsByGym", e); } return(response); }