示例#1
0
        public async Task <IActionResult> Confirm(int?id, string checkIn, string checkOut, int adults, int children, string roomType)
        {
            var checkInDate   = DateTime.ParseExact(checkIn, "MM/dd/yyyy", CultureInfo.InvariantCulture);
            var checkOutDate  = DateTime.ParseExact(checkOut, "MM/dd/yyyy", CultureInfo.InvariantCulture);
            var selectedHotel = this.hotelService.FindSingleHotelById(id).Result;

            // Will get first available room by given type
            var selectedAvailableRoom = selectedHotel.Rooms
                                        .Where(room => room.Stays.All(res => res.DepartureDate <= checkInDate || res.ArrivalDate >= checkOutDate))
                                        .FirstOrDefault(r => r.RoomType == roomType);

            // Additional validation for all required parameters
            if (checkOutDate <= checkInDate || adults < 1 || id == null ||
                selectedAvailableRoom.MaxOccupancy < adults + children)
            {
                throw new ArgumentOutOfRangeException("Error: Invalid input data!");
            }

            var reservation = new Stay
            {
                RoomType           = roomType,
                ArrivalDate        = checkInDate,
                DepartureDate      = checkOutDate,
                Price              = selectedAvailableRoom.Price,
                TotalPrice         = selectedAvailableRoom.Price * (checkOutDate - checkInDate).Days,
                HotelId            = (int)id,
                RoomId             = selectedAvailableRoom.Id,
                MoneySpend         = selectedAvailableRoom.Price * (checkOutDate - checkInDate).Days,
                ApplicationUserId  = this.userManager.GetUserId(User),
                ConfirmationNumber = this.roomService.GenerateConfirmationNumber(selectedHotel.Name),
                Adults             = adults,
                Children           = children,
                BookedOn           = DateTime.Now,
                IsCanceled         = false,
                PointsSpend        = 0,
                PointsEarned       = (int)selectedAvailableRoom.Price * (checkOutDate - checkInDate).Days * StaticData.PointsMultiplier,
            };

            //var user = await userManager.GetUserAsync(User);
            //user.Points += reservation.PointsEarned;
            //Add the points to the user account, so he/she can use it later on

            roomService.AddReservation(reservation);
            var confirmResViewModel = new ConfrimResViewModel
            {
                StayId             = reservation.Id,
                HotelName          = reservation.Hotel.Name,
                ConfirmationNumber = reservation.ConfirmationNumber,
                CheckInTime        = reservation.Hotel.Info.CheckIn,
                CheckOutTime       = reservation.Hotel.Info.CheckOut,
                Nights             = (checkOutDate - checkInDate).Days.ToString(),
                Adults             = reservation.Adults.ToString(),
                Children           = reservation.Children.ToString(),
                CheckIn            = reservation.ArrivalDate.ToString("MM/dd/yyyy"),
                CheckOut           = reservation.DepartureDate.ToString("MM/dd/yyyy"),
                BookedOn           = reservation.BookedOn,
                IsCanceled         = false,
            };

            return(this.View(confirmResViewModel));
        }