Esempio n. 1
0
        public IActionResult CreateNewReservation(ReservationInputModel model)
        {
            if (model.CheckIn.Date >= model.CheckOut.Date || model.CheckIn.Date < DateTime.Now.Date)
            {
                HttpContext.Session.SetString("Message", "Please enter valid CheckIn and CheckOut Dates");
                return(RedirectToAction(nameof(Index)));
            }

            if ((model.Adults <= 0 && model.Children <= 0) || model.Adults < 0 || model.Children < 0)
            {
                HttpContext.Session.SetString("Message", "Please check Room capacity for Adults and Children.");
                return(RedirectToAction(nameof(Index)));
            }

            var season = _seasonRepository.GetSeasonByDate(DateTime.Now);
            var room   = _roomRepository.GetRoomById(model.RoomId);
            var meal   = _mealRepository.GetMealById(model.MealId);

            var roomRateBySeasonIdAndRoomId = _roomRateRepository.GetRoomRateBySeasonIdAndRoomId(season.Id, model.RoomId);
            var mealRateBySeasonIdAndRoomId = _mealRateRepository.GetMealRateBySeasonIdAndRoomId(season.Id, model.MealId);

            var numOfRoomsForAdults   = model.Adults / room.MaxNumOfAdults + (model.Adults % room.MaxNumOfAdults > 0 ? 1 : 0);
            var numOfRoomsForChildren = model.Children / room.MaxNumOfChildren + (model.Children % room.MaxNumOfChildren > 0 ? 1 : 0);
            var numOfRooms            = numOfRoomsForAdults > numOfRoomsForChildren ? numOfRoomsForAdults : numOfRoomsForChildren;
            var numOfDays             = model.CheckOut.Subtract(model.CheckIn).Days;

            var total = _reservationRepository.CalculateReservationCost(numOfRooms,
                                                                        numOfDays, model.Adults + model.Children,
                                                                        roomRateBySeasonIdAndRoomId.Price,
                                                                        mealRateBySeasonIdAndRoomId.Price);

            var reservation = Reservation.New(model.Name,
                                              model.Email,
                                              model.Country,
                                              model.Adults,
                                              model.Children,
                                              room,
                                              meal,
                                              season,
                                              model.CheckIn,
                                              model.CheckOut,
                                              total);

            _reservationRepository.SaveReservation(reservation);

            total = decimal.Round(total, 4);

            ViewBag.Total = total;

            return(View("SuccessReservation", model));
        }