Exemplo n.º 1
0
 public Task <ActionResult <UULResponse> > BookTimeSlot(int gymId, BookTimeSlotDTO dto) => BookTimeSlotByGym(dto, gymId);
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 public Task <ActionResult <UULResponse> > BookTimeSlot(BookTimeSlotDTO dto) => BookTimeSlotByGym(dto, -1);