Ejemplo n.º 1
0
        public async Task <string> CreateAsync(TripCreateInputModel inputModel, ApplicationUser user)
        {
            var townsDistance = this.townsDistancesRepository
                                .AllAsNoTracking()
                                .Where(td => (td.Origin == inputModel.Origin && td.Destination == inputModel.Destination) ||
                                       (td.Origin == inputModel.Destination && td.Destination == inputModel.Origin))
                                .FirstOrDefault();

            if (townsDistance == null)
            {
                return(null);
            }

            var trip = new Trip
            {
                DriverId              = user.Id,
                CarId                 = user.CarId,
                TownsDistanceId       = townsDistance.Id,
                DateOfDeparture       = inputModel.DateOfDeparture,
                TimeOfDeparture       = inputModel.TimeOfDeparture,
                TotalSeats            = user.Car.PassengerSeats,
                FreeSeats             = inputModel.FreeSeats,
                ExpensePerPerson      = inputModel.ExpensePerPerson,
                AdditionalInformation = inputModel.AdditionalInformation,
            };

            await this.tripsRepository.AddAsync(trip);

            await this.tripsRepository.SaveChangesAsync();

            return(trip.Id);
        }
Ejemplo n.º 2
0
        public async Task CreateAsync(TripCreateInputModel input)
        {
            var trip = new Trip
            {
                FromDestinationName = input.FromDestinationName,
                ToDestinationName   = input.ToDestinationName,
                User                  = input.ApplicationUser,
                UserId                = input.UserId,
                Car                   = input.Car,
                CarId                 = input.CarId,
                PricePerPassenger     = input.PricePerPassenger,
                DateOfDeparture       = input.DateOfDeparture,
                TimeOfDeparture       = input.TimeOfDeparture,
                AdditionalInformation = input.AdditionalInformation,
                GroupName             = Guid.NewGuid().ToString(),
            };

            var userTrip = new UserTrip
            {
                User   = trip.User,
                UserId = trip.UserId,
                Trip   = trip,
                TripId = trip.Id,
            };

            var user = input.ApplicationUser;

            user.UserTrips.Add(userTrip);
            this.unitOfWork.Users.Update(user);
            await this.unitOfWork.CompleteAsync();
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create(TripCreateInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View());
            }

            var user = await this.userManager
                       .Users
                       .Include(u => u.Car)
                       .SingleAsync(u => u.Email == this.User.Identity.Name);

            if (user.CarId == null)
            {
                return(this.BadRequest("User does not have a car."));
            }

            if (inputModel.FreeSeats > user.Car.PassengerSeats)
            {
                return(this.BadRequest("Free seats are more than the available car seats."));
            }

            var tripId = await this.tripsService.CreateAsync(inputModel, user);

            if (tripId == null)
            {
                return(this.View());
            }

            return(this.Redirect("/Home/Index"));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create(TripCreateInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View());
            }

            if (inputModel.DateOfDeparture.Date.CompareTo(DateTime.Now.Date) < 0 ||
                (inputModel.DateOfDeparture.Date.CompareTo(DateTime.Now.Date) == 0 &&
                 inputModel.TimeOfDeparture.TimeOfDay.TotalMinutes < DateTime.Now.TimeOfDay.TotalMinutes))
            {
                return(this.View());
            }

            var user = await this.userManager
                       .Users
                       .Include(u => u.Car)
                       .SingleAsync(u => u.Email == this.User.Identity.Name);

            if (user.CarId == null)
            {
                return(this.RedirectToAction("Index", "Cars"));
            }

            var tripId = await this.tripsService.CreateAsync(inputModel, user);

            if (tripId == null)
            {
                return(this.RedirectToAction("BadRequest", "Errors"));
            }

            this.TempData["Notification"] = "Trip was successfully created.";

            return(this.RedirectToAction("Details", new { id = tripId }));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create(string id)
        {
            var car = await this.carsService.GetByIdAsync <CarServiceDetailsModel>(id);

            var viewModel = new TripCreateInputModel()
            {
                CarId = car.Id,
                Car   = car.To <Car>(),
            };

            return(this.View(viewModel));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Create(TripCreateInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            input.ApplicationUser = user;
            await this.tripsService.CreateAsync(input);

            return(this.RedirectToAction("Index", "Trips"));
        }
Ejemplo n.º 7
0
        public async Task <string> CreateAsync(TripCreateInputModel inputModel, ApplicationUser user)
        {
            var townsDistanceId = await this.GetOriginAndDestination(inputModel.Origin, inputModel.Destination);

            if (townsDistanceId == null)
            {
                return(null);
            }

            if (inputModel.FreeSeats > user.Car.PassengerSeats)
            {
                return(null);
            }

            var origin      = (Town)Enum.Parse(typeof(Town), inputModel.Origin.Replace(" ", string.Empty));
            var destination = (Town)Enum.Parse(typeof(Town), inputModel.Destination.Replace(" ", string.Empty));

            var trip = new Trip
            {
                DriverId              = user.Id,
                CarId                 = user.CarId,
                TownsDistanceId       = townsDistanceId,
                Origin                = origin,
                Destination           = destination,
                DateOfDeparture       = inputModel.DateOfDeparture,
                TimeOfDeparture       = inputModel.TimeOfDeparture,
                TotalSeats            = user.Car.PassengerSeats,
                FreeSeats             = inputModel.FreeSeats,
                ExpensePerPerson      = inputModel.ExpensePerPerson,
                AdditionalInformation = inputModel.AdditionalInformation,
            };

            await this.tripsRepository.AddAsync(trip);

            await this.tripsRepository.SaveChangesAsync();

            var userTrip = new UserTrip
            {
                UserId = user.Id,
                TripId = trip.Id,
            };

            await this.userTripsRepository.AddAsync(userTrip);

            await this.userTripsRepository.SaveChangesAsync();

            return(trip.Id);
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Create()
        {
            var currentUser = await this.userManager.GetUserAsync(this.User);

            var cars = this.carsService.GetAllUserCarsByUserId <CarDropDownViewModel>(currentUser.Id);

            var viewModel = new TripCreateInputModel
            {
                ApplicationUser = currentUser,
                Cars            = cars,
                Destinations    = await this.tripsService.GetAllDestinationsAsync <DestinationViewModel>(),
                DateOfDeparture = DateTime.Now,
            };

            return(this.View(viewModel));
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> Create(TripCreateInputModel tripCreateInputModel, string id)
        {
            if (tripCreateInputModel.CarId == null)
            {
                tripCreateInputModel.CarId = id;
                await this.reservationsService.SettingUpReservationStatusToAccomplished(id);
            }

            var currentUser = await this.userManager.GetUserAsync(this.User);

            tripCreateInputModel.ClientId = currentUser.Id;

            var isCarAvailableByDate = await this.carsService.IsCarAvailableByDate(DateTime.UtcNow, id);

            if (!this.ModelState.IsValid ||
                !isCarAvailableByDate)
            {
                var car = await this.carsService.GetByIdAsync <CarServiceDetailsModel>(id);

                tripCreateInputModel.Car      = car.To <Car>();
                tripCreateInputModel.ClientId = currentUser.Id;

                if (!isCarAvailableByDate)
                {
                    this.TempData["Error"] = UnavailableCarErrorMessage;
                }

                return(this.View(tripCreateInputModel));
            }

            var tripServiceCreateModel = tripCreateInputModel.To <TripServiceInputModel>();

            await this.tripsService.CreateAsync(tripServiceCreateModel);

            return(this.Redirect("/Trips/MyTrips"));
        }