Пример #1
0
        public HttpResponse AddUserToTrip(string tripId)
        {
            var userId = this.User.Id;
            var user   = this.data.Users.First(x => x.Id == userId);

            if (this.data.UserTrips.Any(x => x.TripId == tripId && x.UserId == userId))
            {
                return(Redirect($"/Trips/Details?tripId={tripId}"));
            }

            if (this.userTripService.FreeSeatTrip(userId, tripId) <= 0)
            {
                return(Redirect("/Trips/All"));
            }
            var trip = this.data.Trips.First(x => x.Id == tripId);

            var userTrip = new UserTrip {
                Trip = trip, User = user
            };

            var userAsQueary = this.data
                               .Users
                               .Single(x => x.Id == this.User.Id);

            userAsQueary.UserTrips.Add(userTrip);

            var tripAsQueary = this.data
                               .Trips
                               .Single(x => x.Id == tripId);

            this.data.SaveChanges();

            return(Redirect("/Trips/All"));
        }
Пример #2
0
        public bool AddUserToTrip(string userId, string tripId)
        {
            var trip = this.context.Trip.FirstOrDefault(x => x.Id == tripId);

            var userTripExist = this.context.UserTrips.FirstOrDefault(x => x.UserId == userId && x.TripId == trip.Id);

            if (userTripExist != null)
            {
                return(false);
            }

            if (userTripExist == null && trip.Seats - trip.UserTrips.Count > 0)
            {
                var userTrip = new UserTrip
                {
                    TripId = tripId,
                    UserId = userId
                };

                trip.UserTrips.Add(userTrip);
                this.context.UserTrips.Add(userTrip);

                this.context.SaveChanges();

                return(true);
            }

            return(false);
        }
        public async Task <bool> JoinAsync(string userId, int tripId)
        {
            var tripInfo = await this.GetTripInfoAsync(userId, tripId);

            if (tripInfo == null)
            {
                return(false);
            }

            if (userId == tripInfo.DriverId || tripInfo.UserIsSignedIn)
            {
                return(false);
            }

            var passagerInTrip = new UserTrip()
            {
                UserId = userId,
                TripId = tripId
            };

            this.db.Add(passagerInTrip);

            await this.db.SaveChangesAsync();

            return(true);
        }
Пример #4
0
        public static async Task CreateTrip(string customerEmail, string customerName, string driverEmail, string startLocation, string destinationLocation, DateTime timeOfBooking, DateTime lastEdited, double basePrice, string driverMessage, string vehicleClass)
        {
            UserTrip newTrip = new UserTrip
            {
                UserEmail           = customerEmail,
                UserName            = customerName,
                DriverEmail         = driverEmail,
                StartLocation       = startLocation,
                DestinationLocation = destinationLocation,
                BookingTime         = timeOfBooking,
                LastEditedTime      = lastEdited,
                TripBasePrice       = basePrice,
                DriverMessage       = driverMessage,
                TripTipAmount       = 0,
                VehicleClass        = vehicleClass
            };

            await client.CreateDocumentAsync(tripsCollectionLink, newTrip);

            /*
             * dbClient = LocationTrackingHelper.dbClient;
             * var table = Table.LoadTable(dbClient, "Trips");
             * var item = new Document();
             * item["UserEmail"] = customerEmail;
             * item["CustomerName"] = customerName;
             * item["StartLocation"] = startLocation;
             * item["DestinationLocation"] = destinationLocation;
             * item["BookingTime"] = timeOfBooking;
             * item["Time"] = lastEdited;
             * item["TripBasePrice"] = basePrice;
             * item["MessageForDriver"] = driverMessage;
             * await table.PutItemAsync(item);
             */
        }
Пример #5
0
        public HttpResponse AddUserToTrip(string tripId)
        {
            var userTrip = this.db.UserTrips
                           .Where(x => x.TripId == tripId && x.UserId == User.Id)
                           .FirstOrDefault();

            if (userTrip != null)
            {
                return(Redirect($"/Trips/Details?tripId={tripId}"));
            }

            var newUserTrip = new UserTrip
            {
                UserId = User.Id,
                TripId = tripId,
            };

            var trip = this.db.Trips
                       .Find(tripId);

            trip.Seats--;

            this.db.UserTrips.Add(newUserTrip);

            this.db.SaveChanges();

            return(Redirect("/Trips/All"));
        }
Пример #6
0
        public bool AddUserToTrip(string tripId, string userId)
        {
            var trip = this.db.Trips.FirstOrDefault(x => x.Id == tripId);

            var userTrip = new UserTrip
            {
                TripId = tripId,
                UserId = userId
            };

            if (!this.db.UserTrips.Any(x => x.TripId == userTrip.TripId && x.UserId == userTrip.UserId))
            {
                if (trip.Seats <= 0)
                {
                    return(false);
                }
                else
                {
                    trip.UserTrips.Add(userTrip);
                    trip.Seats--;
                }

                this.db.SaveChanges();

                return(true);
            }

            return(false);
        }
        public HttpResponse AddUserToTrip(string tripId)
        {
            var tripQuery = data.Trips.FirstOrDefault(t => t.Id == tripId);

            if (tripQuery == null)
            {
                return(Error("Selected trip doesn't exist."));
            }

            var userTripQuery = data.UserTrips.FirstOrDefault(ut => ut.TripId == tripId && ut.UserId == User.Id);

            if (userTripQuery != null)
            {
                return(Redirect($"/Trips/Details?tripId={tripId}"));
            }

            if (tripQuery.Seats == 0)
            {
                return(Error("Not enough seats on the trip!")); // Not sure if i should redirect to details or throw error.
            }

            var userTrips = new UserTrip()
            {
                TripId = tripId,
                UserId = User.Id
            };

            tripQuery.Seats--;

            data.UserTrips.Add(userTrips);
            data.SaveChanges();

            return(Redirect("/"));
        }
Пример #8
0
        public bool AddUserToTrip(string tripId, string userId)
        {
            if (dbContext.UserTrips.Any(x => x.TripId == tripId && x.UserId == userId))
            {
                return(false);
            }
            var trip = dbContext.Trips.Where(x => x.Id == tripId)
                       .Select(x => new
            {
                UserTrips = x.UserTrips,
                Seats     = x.Seats
            }).FirstOrDefault();

            if (trip == null || trip.Seats - trip.UserTrips.Count() <= 0)
            {
                return(false);
            }

            if (!dbContext.Trips.Any(x => x.Id == tripId))
            {
                return(false);
            }

            var userTrip = new UserTrip
            {
                UserId = userId,
                TripId = tripId,
            };

            dbContext.UserTrips.Add(userTrip);

            dbContext.SaveChanges();

            return(true);
        }
Пример #9
0
        public void JoinUserAndTrip(string tripId, string userId)
        {
            var trip = this.db.Trips.FirstOrDefault(x => x.Id == tripId);

            bool isTrue = this.db.UserTrips.Any(x => x.TripId == tripId && x.UserId == userId);

            if (isTrue)
            {
                return;
            }

            if (trip.Seats > 1)
            {
                trip.Seats -= 1;

                var userTrip = new UserTrip()
                {
                    TripId = tripId,
                    UserId = userId,
                };

                this.db.UserTrips.Add(userTrip);
                this.db.SaveChanges();
            }
        }
Пример #10
0
        public ICollection <string> AddTrip(AddTripFormModel model, string userId)
        {
            var modelErrors = this.validator.ValidateTrip(model);

            if (modelErrors.Any())
            {
                return(modelErrors);
            }

            var trip = new Trip
            {
                StartPoint    = model.StartPoint,
                EndPoint      = model.EndPoint,
                DepartureTime = DateTime.ParseExact(model.DepartureTime, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture),
                ImagePath     = model.ImagePath,
                Seats         = int.Parse(model.Seats),
                Description   = model.Description,
            };

            dbContext.Trips.Add(trip);

            dbContext.SaveChanges();

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

            dbContext.UserTrips.Add(userTrip);

            dbContext.SaveChanges();

            return(modelErrors);
        }
Пример #11
0
        public HttpResponse Add(AddTripForm model)
        {
            var errors = this.validator.ValidateTrip(model);

            if (errors.Any())
            {
                return(Redirect("/Trips/Add"));
            }

            var departure = DateTime.ParseExact(model.DepartureTime, "dd.MM.yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            var trip      = new Trip
            {
                StartPoint    = model.StartPoint,
                EndPoint      = model.EndPoint,
                DepartureTime = departure,
                ImagePath     = model.ImagePath,
                Seats         = model.Seats,
                Description   = model.Description,
            };

            ;
            var user = this.data.Users.First(x => x.Id == this.User.Id);

            var userTrip = new UserTrip {
                Trip = trip, User = user
            };

            trip.UserTrips.Add(userTrip);

            this.data.Trips.Add(trip);

            this.data.SaveChanges();

            return(Redirect("/Trips/All"));
        }
Пример #12
0
        public void Remove(int tripId, string userId)
        {
            UserTrip trip = this.db.UserTrips.FirstOrDefault(ut => ut.UserId == userId && ut.TripId == tripId);

            this.db.UserTrips.Remove(trip);
            this.db.SaveChanges();
        }
Пример #13
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();
        }
Пример #14
0
        public void AddUserToTrip(string userId, string tripId)
        {
            var userTrip = new UserTrip
            {
                TripId = tripId,
                UserId = userId,
            };

            this.db.UsersTrips.Add(userTrip);
            this.db.SaveChanges();
        }
        public void JoinTrip(string userId, string tripId)
        {
            var userTrip = new UserTrip()
            {
                UserId = userId,
                TripId = tripId
            };

            this.DecreaseFreeSeat(tripId);

            this.db.UserTrips.Add(userTrip);
            this.db.SaveChanges();
        }
Пример #16
0
        public void AddUserToTrip(string tripId, string username, string password)
        {
            var userId = this.GetUserId(username, password);

            var userTrip = new UserTrip
            {
                UserId = userId,
                TripId = tripId
            };

            this.db.UserTrips.Add(userTrip);
            this.db.SaveChanges();
        }
Пример #17
0
        public async Task SeedAsync(ApplicationDbContext dbContext, IServiceProvider serviceProvider)
        {
            var userManager = serviceProvider
                              .GetRequiredService <UserManager <ApplicationUser> >();

            var random     = new Random();
            var random2    = new Random();
            var dateRandom = new Random();
            var trips      = new List <Trip>();

            for (int i = 1; i <= 36; i++)
            {
                var user            = dbContext.Users.FirstOrDefault(x => x.UserName == $"FooUser{i}");
                var car             = dbContext.Cars.FirstOrDefault(x => x.UserId == user.Id);
                var randomFromId    = random.Next(1, 257);
                var randomToId      = random2.Next(1, 257);
                var destinationFrom = dbContext.Destinations.Where(x => x.Id == randomFromId).FirstOrDefault();
                var destinationTo   = dbContext.Destinations.Where(x => x.Id == randomToId).FirstOrDefault();
                var dateOfDeparture = new DateTime(2020, 12, dateRandom.Next(1, 31));

                var trip = new Trip
                {
                    FromDestination     = destinationFrom,
                    ToDestination       = destinationTo,
                    FromDestinationName = destinationFrom.Name,
                    ToDestinationName   = destinationTo.Name,
                    DateOfDeparture     = dateOfDeparture,
                    PricePerPassenger   = i + 10,
                    Car       = car,
                    User      = user,
                    GroupName = Guid.NewGuid().ToString(),
                };
                trips.Add(trip);

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

                user.UserTrips.Add(userTrip);
            }

            await dbContext.Trips.AddRangeAsync(trips);

            await dbContext.SaveChangesAsync();
        }
        public void AddUserToTrip(string userId, string tripId)
        {
            var trip = this.data.Trips.FirstOrDefault(t => t.Id == tripId);

            trip.Seats -= 1;

            var userTrip = new UserTrip
            {
                UserId = userId,
                Trip   = trip
            };

            this.data.UsersTrips.Add(userTrip);
            this.data.SaveChanges();
        }
Пример #19
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);
        }
Пример #20
0
        public void AddUserToTrip(string tripId, string userId)
        {
            var trips = this.db.Trips.FirstOrDefault(t => t.Id == tripId);

            if (trips.Seats > 0)
            {
                var userTrips = new UserTrip()
                {
                    UserId = userId,
                    TripId = tripId,
                };

                this.db.Usertrips.Add(userTrips);
                trips.Seats--;
                this.db.SaveChanges();
            }
        }
Пример #21
0
        public void AddUserToTrip(string tripId, string userId)
        {
            var trip = this.db.Trips.Find(tripId);
            var user = this.db.Users.Find(userId);

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

            trip.Seats = trip.Seats - 1;
            trip.UserTrips.Add(userTrip);
            this.db.SaveChanges();
        }
Пример #22
0
        public async Task <string> AddUserToTripAsync(string requestorId, string tripCreatorId, string tripId)
        {
            var trip = await this.GetByIdAsync(tripId);

            var requestor = await this.usersService.GetByIdAsync(requestorId);

            var tripCreator = await this.usersService.GetByIdAsync(tripCreatorId);

            if (requestor == null || tripCreator == null || trip == null)
            {
                return(null);
            }

            if (trip.DriverId != tripCreator.Id || trip.FreeSeats == 0)
            {
                return(null);
            }

            var userTrip = await this.userTripsRepository
                           .All()
                           .Where(ut => ut.UserId == requestorId && ut.TripId == tripId)
                           .FirstOrDefaultAsync();

            if (userTrip != null)
            {
                return(null);
            }

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

            await this.userTripsRepository.AddAsync(userTrip);

            await this.userTripsRepository.SaveChangesAsync();

            trip.FreeSeats--;

            this.tripsRepository.Update(trip);
            await this.tripsRepository.SaveChangesAsync();

            return(trip.Id);
        }
Пример #23
0
        public bool AddUserToTrip(string tripId, string userId)
        {
            var existUser = dbContext.UserTrips.FirstOrDefault(x => x.TripId == tripId && x.UserId == userId);

            if (existUser == null)
            {
                var userTrip = new UserTrip()
                {
                    TripId = tripId,
                    UserId = userId
                };
                this.dbContext.UserTrips.Add(userTrip);
                dbContext.SaveChanges();
                return(true);
            }

            return(false);
        }
Пример #24
0
        public bool AddUserToTrip(string userId, string tripId)
        {
            if (this.db.Trips.Find(tripId).UserTrips.Any(x => x.UserId == userId))
            {
                return(false);
            }

            var userTrip = new UserTrip()
            {
                UserId = userId,
                TripId = tripId
            };

            this.db.UserTrips.Add(userTrip);
            this.db.SaveChanges();

            return(true);
        }
Пример #25
0
        public void AddUser(string userId, string tripId)
        {
            var tripFromDb = this.db.Trips.FirstOrDefault(t => t.Id == tripId);
            var userFromDb = this.db.Users.FirstOrDefault(u => u.Id == userId);

            var ut = new UserTrip
            {
                UserId = userId,
                User   = userFromDb,
                TripId = tripId,
                Trip   = tripFromDb
            };

            tripFromDb.Seats--;

            this.db.UserTrips.Add(ut);
            this.db.SaveChanges();
        }
Пример #26
0
        public bool AddUserToTrip(string userId, string tripId)
        {
            var userInTrip = this.db.UserTrips.Any(x => x.UserId == userId && x.TripId == tripId);

            if (userInTrip)
            {
                return(false);
            }
            var userTrip = new UserTrip()
            {
                TripId = tripId,
                UserId = userId
            };

            this.db.UserTrips.Add(userTrip);
            this.db.SaveChanges();
            return(true);
        }
Пример #27
0
 public Trip InsertTrip(Trip trip)
 {
     try
     {
         _dbContext.Trips.Add(trip);
         _dbContext.SaveChanges();
         UserTrip userTrip = new UserTrip {
             User_Id = trip.PrimaryUser_Id.Value, Trip_Id = trip.Id, RequestTime = trip.RequestTime, isScheduled = trip.isScheduled
         };
         _dbContext.UserTrips.Add(userTrip);
         _dbContext.SaveChanges();
         return(trip);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Пример #28
0
        public void AddUserToTrip(string userId, string tripId)
        {
            var userIsInTrip = this.data.UserTrips
                               .Any(x => x.UserId == userId && x.TripId == tripId);

            if (userIsInTrip)
            {
                return;
            }

            var userInTrip = new UserTrip
            {
                UserId = userId,
                TripId = tripId,
            };

            this.data.UserTrips.Add(userInTrip);
            this.data.SaveChanges();
        }
        public bool AddUserToTrip(string tripId, string userId)
        {
            var targetTrip = this.db.Trips.FirstOrDefault(x => x.Id == tripId);
            var userTrip   = new UserTrip
            {
                TripId = tripId,
                UserId = userId
            };

            if (!this.db.UserTrips.Any(x => x.TripId == userTrip.TripId && x.UserId == userTrip.UserId) && targetTrip.Seats > 0)
            {
                targetTrip.Seats -= 1;
                targetTrip.UserTrips.Add(userTrip);
                db.SaveChanges();
                return(true);
            }

            return(false);
        }
Пример #30
0
        public bool AddUserToTrip(string userId, string tripId)
        {
            var userTrip = this.db.UserTrips.Where(x => x.UserId == userId && x.TripId == tripId).FirstOrDefault();

            if (userTrip == null)
            {
                userTrip = new UserTrip
                {
                    UserId = userId,
                    TripId = tripId
                };

                this.db.UserTrips.Add(userTrip);
                this.db.SaveChanges();
                return(true);
            }

            return(false);
        }