Beispiel #1
0
        public async Task <IActionResult> Create(BookingFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(HttpContext.User);

            var listing = await _unitOfWork.Listings.GetByIdWithImagesAndAccessories(model.Listing.Id);

            var available = await _unitOfWork.Bookings.IsAvailable(listing.Id,
                                                                   model.Listing.From.Value, model.Listing.To.Value);

            if (!available)
            {
                return(NotFound());
            }

            var stripeCustomerId = await _stripe.AddCustomerToStripeAsync(model.StripeToken, user.Email);

            var booking = _mapper.Map <Booking>(model);

            _mapper.Map(listing, booking);

            booking.User = user;
            booking.StripeCustomerIdToken = stripeCustomerId;
            booking.CalculateTotalPrice();
            booking.NewlyCreated();

            await _unitOfWork.Bookings.AddAsync(booking);

            _unitOfWork.Complete();

            var bookingUrl = Url.NewBookingCallbackLink(listing.Id, Request.Scheme);
            var imageUrl   = _imageUploader.GetBikeImageById(listing.Images.FirstOrDefault()?.ImagePublicId, 690, 690);
            await _emailSender.SendNewBookingEmailAsync(listing.User.Email, bookingUrl, imageUrl, booking);

            return(RedirectToAction("Details", new { id = booking.Id }));
        }