コード例 #1
0
        public async Task Upsert(string table, VastTrafikModelTrafficSituation record)
        {
            var collection = db.GetCollection <VastTrafikModelTrafficSituation>(table);


            collection.ReplaceOne(filter: (u => u.SituationNumber == record.SituationNumber), options: new UpdateOptions {
                IsUpsert = true
            }, replacement: record);
        }
コード例 #2
0
        public static async void GetTrafficSituation()
        {
            string url = "https://api.vasttrafik.se/ts/v1/traffic-situations";

            using (HttpResponseMessage response = await ApiHelperVasttrafik.ApiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    string fromVastTrafik = await response.Content.ReadAsStringAsync();

                    JArray trafficSituation = JArray.Parse(fromVastTrafik);

                    MongoCRUD db = new MongoCRUD("admin");

                    foreach (var traffic in trafficSituation)
                    {
                        VastTrafikModelTrafficSituation model = new VastTrafikModelTrafficSituation();

                        model.SituationNumber    = traffic[key : "situationNumber"].ToString();
                        model.CreationTime       = traffic[key : "creationTime"].ToObject <DateTime>();
                        model.StartTime          = traffic[key : "startTime"].ToObject <DateTime>();
                        model.EndTime            = traffic[key : "endTime"].ToObject <DateTime>();
                        model.Severity           = traffic[key : "severity"].ToString();
                        model.Title              = traffic[key : "title"].ToString();
                        model.Description        = traffic[key : "description"].ToString();
                        model.AffectedStopPoints = new List <AffectedStopPointsModel>();



                        try
                        {
                            JArray affectedStopPoints = JArray.Parse(traffic.SelectToken("affectedStopPoints").ToString());

                            foreach (var stops in affectedStopPoints)
                            {
                                AffectedStopPointsModel modelaffected = new AffectedStopPointsModel();
                                modelaffected.Name = stops[key : "name"].ToString();

                                modelaffected.StopPointGid     = stops[key : "gid"].ToString();
                                modelaffected.MunicipalityName = stops[key : "municipalityName"].ToString();

                                model.AffectedStopPoints.Add(modelaffected);

                                StopPointNameMunicipalityModel stopName = new StopPointNameMunicipalityModel();
                                stopName.Name             = stops[key : "name"].ToString();
                                stopName.MunicipalityName = stops[key : "municipalityName"].ToString();
                                stopName.SituationNumber  = model.SituationNumber.ToString();

                                try
                                {
                                    JArray affectedLines = JArray.Parse(traffic.SelectToken("affectedLines").ToString());


                                    foreach (var stop in affectedLines)
                                    {
                                        stopName.TransportAuthorityName   = stop[key : "transportAuthorityName"].ToString();
                                        stopName.DefaultTransportModeCode = stop[key : "defaultTransportModeCode"].ToString();
                                    }
                                }
                                catch
                                {
                                    Console.WriteLine("affectedLines Error");
                                    break;
                                }


                                GetGeo(stopName, stopName.Name, stopName.MunicipalityName);
                                await db.Upsert(Vasttrafik.affectedLocation, stopName);
                            }
                        }
                        catch
                        {
                            Console.WriteLine("Catchen");
                            break;
                        }



                        /*
                         * try
                         * {
                         *  JArray affectedLines = JArray.Parse(traffic.SelectToken("affectedLines").ToString());
                         *  Console.WriteLine(affectedLines);
                         *
                         *  foreach (var stop in affectedLines)
                         *  {
                         *
                         *      StopPointNameMunicipalityModel modelaffected = new StopPointNameMunicipalityModel();
                         *      modelaffected.TransportAuthorityName = stop[key: "transportAuthorityName"].ToString();
                         *      modelaffected.DefaultTransportModeCode = stop[key: "defaultTransportModeCode"].ToString();
                         *      modelaffected.SituationNumber = model.SituationNumber.ToString();
                         *
                         *      await db.Upsert(Vasttrafik.affectedLocation, modelaffected);
                         *  }
                         * }
                         * catch
                         * {
                         *  Console.WriteLine("affectedLines Error");
                         *  break;
                         * }
                         */



                        await db.Upsert(Vasttrafik.traficSituation, model);
                    }
                }
                else
                {
                    Console.Write("Fel vid kontakt med API");
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }