Example #1
0
 public Book(Booking booking)
     : base(booking)
 {
 }
        public IView Book(int roomId, DateTime startDate, DateTime endDate, string comments)
        {
            Authorize(Roles.User, Roles.VenueAdmin);
            var room = Data.RoomsRepository.Get(roomId);
            if (room == null)
            {
                return NotFound(string.Format("The room with ID {0} does not exist.", roomId));
            }

            //if (endDate < startDate) throw new ArgumentException("The date range is invalid.");
            var availablePeriod = room.AvailableDates.FirstOrDefault(d => d.StartDate <= startDate || d.EndDate >= endDate);
            if (availablePeriod == null)
            {
                throw new ArgumentException(string.Format("The room is not available to book in the period {0:dd.MM.yyyy} - {1:dd.MM.yyyy}.", startDate, endDate));
            }

            decimal totalPrice = (endDate - startDate).Days * room.PricePerDay;
            var booking = new Booking(CurrentUser, startDate, endDate, totalPrice, comments);
            room.Bookings.Add(booking);
            CurrentUser.Bookings.Add(booking);
            UpdateRoomAvailability(startDate, endDate, room, availablePeriod);
            return View(booking);
        }