Esempio n. 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                var cars = await _carService.GetCars(Reservation.PickUpTime, Reservation.DropOffTime, Reservation.VehicleModelId);

                ViewData["CarId"] = new SelectList(cars, "Id", "PlateNumber");
                return(Page());
            }

            _logger.LogInformation(LoggingEvents.GetItem, "Get Car {ID}", Reservation.CarId);
            var car = await _carService.GetCar(Reservation.CarId);

            if (car == null)
            {
                _logger.LogInformation(LoggingEvents.GetItemNotFound, "Get Car {ID} NOT FOUND", Reservation.CarId);
                return(NotFound());
            }

            _logger.LogInformation(LoggingEvents.GetItem, "Get Reservation {ID}", Reservation.Id);
            Reservation = await _reservationService.GetReservation(Reservation.Id);

            if (Reservation == null)
            {
                _logger.LogInformation(LoggingEvents.GetItemNotFound, "Get Reservation {ID} NOT FOUND", Reservation.Id);
                return(NotFound());
            }

            try
            {
                _logger.LogInformation(LoggingEvents.UpdateItem, "Update Reservation {ID} with Car {ID}", Reservation.Id, Reservation.CarId);
                await _reservationService.EditReservation(Reservation.Id, car.Id);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_reservationService.ReservationExists(Reservation.Id))
                {
                    _logger.LogInformation(LoggingEvents.UpdateItemNotFound, "Update Reservation {ID} NOT FOUND", Reservation.Id);
                    return(NotFound());
                }
                else
                {
                    return(StatusCode(409));
                }
            }

            const string view = "/Views/Emails/ReservationEmailAccepted";

            var user = await _userManager.Users
                       .Where(u => u.Id == Reservation.UserId)
                       .SingleOrDefaultAsync();

            var culture = Thread.CurrentThread.CurrentCulture.Name;

            if (!string.IsNullOrEmpty(user.Culture))
            {
                Thread.CurrentThread.CurrentCulture   = new CultureInfo(user.Culture);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(user.Culture);
            }

            var model = new EmailReservationDto
            {
                UserName    = user.Name ?? user.Email,
                Email       = user.Email,
                VehicleType = Reservation.VehicleType,
                Address     = Reservation.Address,
                PickUpTime  = Reservation.PickUpTime,
                DropOffTime = Reservation.DropOffTime,
                Price       = Reservation.Price,
                State       = Dal.Entities.Reservation.ReservationStates.Accepted
            };

            try
            {
                var message = await _render.RenderViewToStringAsync($"{view}Html.cshtml", model);

                //await _emailSender.SendEmailAsync(user.Email, "Reservation", message);
                QueueEmailMessage queueEmail = new QueueEmailMessage(user.Email, "", message, "Reservation");
                await _cloudStorageService.SendMessage(queueEmail);
            }
            catch
            (InvalidOperationException)
            {
                return(RedirectToPage("./Index"));
            }

            if (!string.IsNullOrEmpty(culture))
            {
                Thread.CurrentThread.CurrentCulture   = new CultureInfo(culture);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            }

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            User user = await _userManager.GetUserAsync(HttpContext.User);

            var vehicle = await _vehicleModelService.GetVehicle(Input.VehicleModelId);

            var address = await _addressService.GetAddress(Input.AddressId);

            int days = (Input.DropOffTime.Date - Input.PickUpTime.Date).Days;

            if (user == null || vehicle == null || address == null)
            {
                return(NotFound());
            }

            if (days < 1)
            {
                return(RedirectToPage("./List"));
            }

            if (days > 100)
            {
                return(RedirectToPage("./List"));
            }

            if (Input.PickUpTime.Date < DateTime.Now.Date.AddDays(1))
            {
                return(RedirectToPage("./List"));
            }

            var cars = await _carService.GetCars(Input.PickUpTime, Input.DropOffTime, Input.VehicleModelId);

            CarFound = cars.Count();

            if (CarFound <= 0)
            {
                return(RedirectToPage("./List"));
            }

            ReservationDto reservationDto = new ReservationDto
            {
                User           = user.UserName,
                UserId         = user.Id,
                Address        = address.ZipCode + " " + address.City + " " + address.StreetAddress,
                AddressId      = address.Id,
                PickUpTime     = Input.PickUpTime,
                DropOffTime    = Input.DropOffTime,
                VehicleModelId = vehicle.Id,
                VehicleType    = vehicle.VehicleType,
                Price          = days * vehicle.PricePerDay
            };

            Reservation = reservationDto;

            _logger.LogInformation(LoggingEvents.InsertItem, "Create Reservation");
            await _reservationService.CreateReservation(reservationDto);

            var model = new EmailReservationDto
            {
                UserName    = user.Name ?? user.Email,
                Email       = user.Email,
                VehicleType = reservationDto.VehicleType,
                Address     = reservationDto.Address,
                PickUpTime  = reservationDto.PickUpTime,
                DropOffTime = reservationDto.DropOffTime,
                Price       = reservationDto.Price,
                State       = ReservationStates.Undecieded
            };

            const string view    = "/Views/Emails/ReservationEmail";
            var          message = await _render.RenderViewToStringAsync($"{view}Html.cshtml", model);

            QueueEmailMessage queueEmail = new QueueEmailMessage(user.Email, "", message, "Reservation");
            await _cloudStorageService.SendMessage(queueEmail);

            //await _emailSender.SendEmailAsync(user.Email, "Reservation", message);

            return(RedirectToPage("./List"));
        }