public GenericListResponse <AvailableReservationTime> GetRoomAvailableTimes(long roomId, DateTime?startDate, int duration, int take)
        {
            using (ReservationDataContext context = new ReservationDataContext())
            {
                if (duration <= 0)
                {
                    return(new GenericListResponse <AvailableReservationTime>("Invalid duration."));
                }

                var roomQuery = context.Rooms.Include(x => x.WorkingHours).Include(x => x.Reservations);
                var room      = roomQuery.SingleOrDefault(x => x.Id == roomId);
                if (room == null)
                {
                    return(new GenericListResponse <AvailableReservationTime>("Room not found."));
                }

                List <AvailableReservationTime> availableTimes = new List <AvailableReservationTime>();
                DateTime now             = DateTime.Now;
                DateTime currentDateTemp = (startDate != null && startDate > now ? startDate.Value.ToLocalDate() : now);
                DateTime currentDate     = currentDateTemp.Date;
                int      limit           = 1440 - duration;
                ReservationValidationService reservationValidationService = new ReservationValidationService();
                while (availableTimes.Count < take)
                {
                    for (int i = 0; i < limit && availableTimes.Count < take; i += 30)
                    {
                        DateTime rentStart = currentDate + TimeSpan.FromMinutes(i);
                        if (rentStart < now)
                        {
                            continue;
                        }

                        ReservationRequestPayload payload = new ReservationRequestPayload
                        {
                            RentStart = rentStart,
                            RentEnd   = rentStart + TimeSpan.FromMinutes(duration)
                        };

                        GenericStatusMessage availability = reservationValidationService.ValidateRoomAvailability(context, room, payload);
                        if (availability.Success)
                        {
                            var matchedWorkingHours = reservationValidationService.GetMatchingWorkingHours(payload, room);
                            availableTimes.Add(new AvailableReservationTime
                            {
                                RentStart = rentStart,
                                Price     = PriceHelper.CalculatePrice(matchedWorkingHours.PriceForHour, rentStart, payload.RentEnd)
                            });
                        }
                    }

                    currentDate += TimeSpan.FromDays(1);
                }
                return(new GenericListResponse <AvailableReservationTime>(availableTimes, availableTimes.Count));
            }
        }
示例#2
0
        public GenericObjectResponse <ReservationRequestResponse> AddReservation(long roomId, long requesterId, ReservationRequestPayload payload)
        {
            ReservationValidationService reservationValidationService = new ReservationValidationService();

            using (ReservationDataContext context = new ReservationDataContext())
            {
                Room               room = context.Rooms.Include(x => x.WorkingHours).Single(x => x.Id == roomId);
                WorkingHours       matchedWorkingHours = reservationValidationService.GetMatchingWorkingHours(payload, room);
                ReservationRequest reservationRequest  = new ReservationRequest()
                {
                    RentStart   = payload.RentStart,
                    RentEnd     = payload.RentEnd,
                    Status      = ReservationStatus.Pending,
                    Description = payload.Description,
                    FinalPrice  = PriceHelper.CalculatePrice(matchedWorkingHours.PriceForHour, payload.RentStart, payload.RentEnd),
                    RoomId      = roomId,
                    UserId      = requesterId
                };
                context.ReservationRequests.Add(reservationRequest);
                context.SaveChanges();

                return(new GenericObjectResponse <ReservationRequestResponse>(ConvertToResponse(reservationRequest)));
            }
        }