예제 #1
0
        public void AddReservation(ReservationCreationDTO reservationCreationDTO)
        {
            reservationCreationDTO.NumberOfDays = reservationCreationDTO.EndDate.Subtract(reservationCreationDTO.BeginDate).Days + 1;
            var period        = _periodRepository.GetAll().Where(p => (p.Days <= reservationCreationDTO.NumberOfDays)).OrderByDescending(o => o.Days).FirstOrDefault();
            int priceModifier = 0;

            if (period != null)
            {
                reservationCreationDTO.PeriodId = period.PeriodId;
                priceModifier = period.PriceModifier;
            }
            var car = _carRepository.Get(reservationCreationDTO.CarId);

            reservationCreationDTO.Car             = AutoMapper.Mapper.Map <CarReservationDTO>(car);
            reservationCreationDTO.Car.IsAvailable = false;
            ChangeCarAvailability(reservationCreationDTO.Car.CarId, reservationCreationDTO.Car.IsAvailable);
            reservationCreationDTO.FinalPrice = CalculateRentFinalPrice(car.Price, reservationCreationDTO.NumberOfDays, priceModifier);
            var user = _userRepository.GetAll().FirstOrDefault(u => u.Username == HttpContext.Current.User.Identity.Name);

            reservationCreationDTO.UserId = user.UserId;
            reservationCreationDTO.User   = AutoMapper.Mapper.Map <UserDTO>(user);
            reservationCreationDTO.Status = "Active";
            var reservation = AutoMapper.Mapper.Map <Reservation>(reservationCreationDTO);

            _reservationRepository.Add(reservation);
            _unitOfWork.Commit();
        }
예제 #2
0
        public ActionResult Create(ReservationCreationDTO reservationDTO)
        {
            if (ModelState.IsValid)
            {
                _reservationService.AddReservation(reservationDTO);
                return(RedirectToAction("Index"));
            }

            return(View());
        }