Esempio n. 1
0
        public async Task <IActionResult> Edit(ReservationEditViewModel model)
        {
            //If the model state is invalid OR Arrival is earlier than now OR later than or the same date as departure, return the view for resubmission
            if (!ModelState.IsValid ||
                DateTime.Compare(model.Arrival, DateTime.Now) < 0 ||
                DateTime.Compare(model.Arrival, model.Departure) >= 0)
            {
                //Repopulate the customer and room pairs for the dropdowns/lists
                model.Customers = _customerRepository.Items.Select(item => new CustomerPair()
                {
                    Id        = item.Id,
                    FirstName = item.FirstName,
                    LastName  = item.LastName
                }).ToList();

                model.Rooms = _roomRepository.Items.Where(item => item.IsAvailable == true || item.Id == model.RoomId).Select(item => new RoomPair()
                {
                    Id         = item.Id,
                    RoomNumber = item.RoomNumber
                }).ToList();

                return(View(model));
            }

            //Caluclate TotalSum
            #region Sum Calculation

            IQueryable <Customer> selectedCustomers = _customerRepository.Items.Where(item => model.SelectedCustomerIds.Contains(item.Id));

            int    adults        = selectedCustomers.Where(item => item.IsAdult).Count();
            int    children      = selectedCustomers.Where(item => !item.IsAdult).Count();
            double adultBedPrice = _roomRepository.Items.FirstOrDefault(room => room.Id == model.RoomId).AdultBedPrice;
            double childBedPrice = _roomRepository.Items.FirstOrDefault(room => room.Id == model.RoomId).ChildBedPrice;

            int days = (model.Departure - model.Arrival).Days;

            double totalSum = CalculateTotalSum(adults, adultBedPrice, children, childBedPrice, model.IsAllInclusive, model.BreakfastIncluded, days);
            #endregion

            var user = await _userManager.GetUserAsync(User);

            //Make a Reservation object
            Reservation reservation = new Reservation()
            {
                Id                   = model.Id,
                RoomId               = model.RoomId,
                CreatorId            = user.Id,
                Arrival              = model.Arrival,
                Departure            = model.Departure,
                BreakfastIncluded    = model.BreakfastIncluded,
                IsAllInclusive       = model.IsAllInclusive,
                TotalSum             = totalSum,
                CustomerReservations = model.SelectedCustomerIds.Select(customerId => new CustomerReservation()
                {
                    CustomerId    = customerId,
                    ReservationId = model.Id
                }).ToList()
            };

            //Check if the reservation's room has been changed
            if (oldRoomId != 0 && oldRoomId != reservation.RoomId)
            {
                //Vacate the room that was previously assigned to this reservation
                await VacateRoom(oldRoomId);

                oldRoomId = 0;
            }

            await _reservationRepository.AddOrUpdate(reservation);

            return(RedirectToAction("Index"));
        }