Ejemplo n.º 1
0
        /// <summary>
        /// Create a new booking in the database
        /// </summary>
        /// <param name="createBooking">New booking</param>
        /// <returns></returns>
        public async Task <int> CreateBookingAsync(CreateBookingDTO createBooking)
        {
            if (createBooking == null)
            {
                throw new ArgumentNullException(nameof(createBooking));
            }

            var booking = _bookingFactory.CreateBooking(createBooking);

            Validate(booking);

            var result = await _bookingRepository.CreateAsync(booking);

            return(result);
        }
Ejemplo n.º 2
0
        public IView Book(int id, DateTime startDate, DateTime endDate, string comments)
        {
            this.Authorize(Roles.User, Roles.VenueAdmin);
            var room = this.Data.RepositoryWithRooms.Get(id);

            if (room == null)
            {
                return(NotFound(string.Format("The room with ID {0} does not exist.", id)));
            }

            if (endDate < startDate)
            {
                throw new ArgumentException("The date range is invalid.");
            }

            var availablePeriod = room.AvailableDates.FirstOrDefault(d => d.StartDate <= startDate || endDate <= d.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    = BookingFactory.CreateBooking(CurrentUser, startDate, endDate, totalPrice, comments);

            room.Bookings.Add(booking);
            this.CurrentUser.Bookings.Add(booking);
            this.UpdateRoomAvailability(startDate, endDate, room, availablePeriod);

            return(GetView(booking));
        }
        public async Task <ActionResult> Handle(CreateBookingCommand request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // gets the available car
            var availableCar = availableCarRepository.Get(request.AvailableCarId);

            if (availableCar == null)
            {
                var result = new ActionResult
                {
                    Status  = ActionResultCode.Failed,
                    Message = $"Cannot find an available car with the {request.AvailableCarId} id",
                    Errors  = new List <string> {
                        $"Cannot find an available car with the {request.AvailableCarId} id"
                    }
                };
                return(await Task.FromResult(result));
            }

            // gets the user
            var user = userRepository.GetUser(request.UserId);

            if (user == null)
            {
                var result = new ActionResult
                {
                    Status  = ActionResultCode.Failed,
                    Message = $"Cannot find an user with the {request.UserId} id",
                    Errors  = new List <string> {
                        $"Cannot find an user with the {request.UserId} id"
                    }
                };
                return(await Task.FromResult(result));
            }

            // creates the booking
            var booking = BookingFactory.CreateBooking(availableCar, user, request.From, request.To);

            // validates the booking
            var validationContext = new ValidationContext(booking);

            Validator.ValidateObject(booking, validationContext, validateAllProperties: true);

            bookingRepository.Add(booking);

            // saves changes
            await bookingRepository.UnitOfWork.SaveChangesAsync();

            var res = new ActionResult
            {
                Status = ActionResultCode.Success
            };

            return(await Task.FromResult(res));
        }