Exemple #1
0
 public bool IsDirect(SuperTrip supertrip)
 {
     if (supertrip.Trip.Count > 1)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
Exemple #2
0
        private static void AddSuperTrip(SuperTrip superTrip)
        {
            TrainCommanderEntities db = new TrainCommanderEntities();

            if (!db.SuperTrip.Any(o =>
                                  o.id_departure_station == superTrip.id_departure_station &&
                                  o.id_arrival_station == superTrip.id_arrival_station &&
                                  o.departure_date == superTrip.departure_date))
            {
                db.SuperTrip.Add(superTrip);
            }
            db.SaveChanges();
        }
Exemple #3
0
        public SuperTripViewModel ConvertToSuperTripViewModel(Orders order)
        {
            SuperTripViewModel superTripVM = new SuperTripViewModel();
            SuperTrip          supertrip   = order.SuperTrip;

            superTripVM.SupertripId      = supertrip.id;
            superTripVM.DepartureStation = StationProvider.GetStationNameById(supertrip.id_departure_station);
            superTripVM.DepartureDate    = supertrip.departure_date;
            superTripVM.ArrivalStation   = StationProvider.GetStationNameById(supertrip.id_arrival_station);
            superTripVM.ArrivalDate      = supertrip.arrival_date;
            superTripVM.IsDirect         = IsDirect(supertrip);
            superTripVM.Price            = Convert.ToDouble(supertrip.price);
            superTripVM.Quantity         = order.quantity;

            return(superTripVM);
        }
Exemple #4
0
        private static void AddTrips(List <Trip> trips)
        {
            TrainCommanderEntities db = new TrainCommanderEntities();
            DateTime departure_date   = trips.First().departure_date;
            DateTime arrival_date     = trips.Last().arrival_date;

            foreach (Trip trip in trips)
            {
                if (!db.Trip.Any(o =>
                                 o.id_departure_station == trip.id_departure_station &&
                                 o.id_arrival_station == trip.id_arrival_station &&
                                 o.departure_date == trip.departure_date))
                {
                    SuperTrip supertrip = (from o in db.SuperTrip
                                           where o.departure_date == departure_date && o.arrival_date == arrival_date
                                           select o).FirstOrDefault();
                    trip.SuperTrip.Add(supertrip);
                    trip.Train = AddTrain();
                    db.Trip.Add(trip);
                    db.SaveChanges();
                }
            }
        }
Exemple #5
0
        public static List <SuperTrip> GetMultiplesJourneys(string departure, string arrival, string date, bool representarrival)
        {
            List <SuperTrip> listOfJourneys = new List <SuperTrip>();

            string departureId = GetStationSNCFId(departure);
            string arrivalId   = GetStationSNCFId(arrival);

            for (int i = 0; i < 4; i++)
            {
                SuperTrip superTrip = GetJourney(departureId, arrivalId, date, representarrival);
                listOfJourneys.Add(superTrip);
                if (!representarrival)
                {
                    date = listOfJourneys[i].departure_date.ToString("yyyyMMddThhmmss").Remove(listOfJourneys[i].departure_date.ToString("yyyyMMddThhmmss").Length - 1) + 1;
                }
                else
                {
                    date = listOfJourneys[i].arrival_date.ToString("yyyyMMddThhmmss").Remove(listOfJourneys[i].arrival_date.ToString("yyyyMMddThhmmss").Length - 1) + 1;
                }
            }

            return(listOfJourneys);
        }
Exemple #6
0
        public static SuperTrip GetJourney(string departureId, string arrivalId, string datetime, bool representarrival)
        {
            string sURL = "https://api.sncf.com/v1/coverage/sncf/journeys?from=" + departureId + "&to=" + arrivalId + "&datetime=" + datetime;

            if (representarrival)
            {
                sURL = sURL + "&datetime_represents=arrival";
            }


            WebRequest wrGETURL = WebRequest.Create(sURL);

            wrGETURL.Headers.Add("Authorization", "4a24c48a-ba0f-4b70-aead-5137ede74ffd");

            HttpWebRequest request = wrGETURL as HttpWebRequest;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            WebHeaderCollection header = response.Headers;

            var encoding = ASCIIEncoding.ASCII;

            using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
            {
                int i = 0;

                SNCFTrip        trip     = new SNCFTrip();
                List <SNCFTrip> tripList = new List <SNCFTrip>();

                Place        stop     = new Place();
                List <Place> stoplist = new List <Place>();

                string jsonText = reader.ReadToEnd();

                JObject journeyData = JObject.Parse(jsonText);

                List <JToken> journeys = journeyData["journeys"].ToList();

                foreach (JToken journey in journeys)
                {
                    if (i != 0)
                    {
                        break;
                    }
                    journeyData = JObject.Parse(journey.ToString());

                    List <JToken> sections = journeyData["sections"].ToList();

                    foreach (JToken section in sections)
                    {
                        trip = JsonConvert.DeserializeObject <SNCFTrip>(section.ToString());

                        if ((JObject)section["from"] != null)
                        {
                            journeyData = JObject.Parse(section.ToString());

                            JObject deps = section["from"].Value <JObject>();

                            foreach (JProperty prop in deps.Properties())
                            {
                                if (prop.Name == "name")
                                {
                                    trip.departure = prop.First.ToString().Split(' ')[0];
                                }
                            }

                            JObject arivs = section["to"].Value <JObject>();

                            foreach (JProperty prop in arivs.Properties())
                            {
                                if (prop.Name == "name")
                                {
                                    trip.arrival = prop.First.ToString().Split(' ')[0];
                                }
                            }

                            trip.price = trip.duration / 300;

                            tripList.Add(trip);
                        }
                    }
                    i++;
                }


                tripList.RemoveAt(0);
                tripList.RemoveAt(tripList.Count - 1);
                for (int count = tripList.Count - 1; count >= 0; count--)
                {
                    if (tripList[count].arrival == tripList[count].departure)
                    {
                        tripList.RemoveAt(count);
                    }
                }
                List <Trip> trips     = ConvertToListTrip(tripList);
                SuperTrip   superTrip = GetSuperTrip(trips);
                AddSuperTrip(superTrip);
                AddTrips(trips);
                return(superTrip);
            }
        }