Exemple #1
0
        public async Task <JObject> GetDataAsJObjectAsync(string url, JsonType type)
        {
            var json = await _publicTransportRepository.DownloadData(url);

            try
            {
                if (!string.IsNullOrEmpty(json))
                {
                    var oldJson = _documentStoreRepository.GetDbJson(type);

                    if (oldJson != null && !string.IsNullOrEmpty(oldJson.Id))
                    {
                        _documentStoreRepository.Delete(oldJson.Id);
                    }

                    _documentStoreRepository.Save(new DbJson {
                        Json = json, Type = type
                    });
                }
                else
                {
                    json = _documentStoreRepository.GetDbJson(type).Json;
                }
            }
            catch (OutOfMemoryException e)
            {
                json = _documentStoreRepository.GetDbJson(type).Json;
            }

            return(JsonConvert.DeserializeObject <JObject>(json));
        }
 void DeleteAndStoreTripsInDb(List <TripsWithBusStops> tripsWithBusStops)
 {
     if (tripsWithBusStops.Count > 0)
     {
         _documentStoreRepository.DeleteTripsWithBusStops();
     }
     _documentStoreRepository.Save(tripsWithBusStops);
 }
        public async Task <List <TimeTableDateTime> > MassDownloadAndSaveToDb(List <StopTimeUrl> convertedStopTimes)
        {
            var entitiesThatWerentDownloaded = new List <TimeTableDateTime>();

            try
            {
                foreach (var stopTime in convertedStopTimes)
                {
                    var objectsToSaveInDb = new List <TimeTableJson>();

                    using (var client = new HttpClient())
                    {
                        foreach (var url in stopTime.Urls)
                        {
                            var json = await _publicTransportRepository.DownloadData(url, client);

                            if (!string.IsNullOrEmpty(json))
                            {
                                objectsToSaveInDb.Add(new TimeTableJson()
                                {
                                    RouteId = stopTime.RouteId,
                                    Json    = json
                                });
                            }
                            else
                            {
                                entitiesThatWerentDownloaded.Add(new TimeTableDateTime()
                                {
                                    RouteId = stopTime.RouteId,
                                    Date    = _timeService.GetDateFromUrl(url)
                                });
                            }
                        }
                    }

                    _documentStoreRepository.Save(objectsToSaveInDb);
                }
            }
            catch (Exception e)
            {
                var message = e.Message;
            }

            return(entitiesThatWerentDownloaded);
        }
        public void ChangeTimeTableJsonsToObjectsAndSaveToDb(List <StopTimeUrl> convertedStopTimes, List <TimeTableDateTime> entitiesThatWerentDownloaded)
        {
            foreach (var stopTime in convertedStopTimes)
            {
                try
                {
                    var notDownloadedEntitiesByRouteId =
                        entitiesThatWerentDownloaded.Where(x => x.RouteId == stopTime.RouteId).ToList();
                    var entitiesToDelete = _documentStoreRepository.GetTimeTableDataByRouteId(stopTime.RouteId);

                    if (notDownloadedEntitiesByRouteId.Count > 0)
                    {
                        entitiesToDelete = _filterHelper.Filter(entitiesToDelete, notDownloadedEntitiesByRouteId);
                    }

                    var jsonsToConvert    = _documentStoreRepository.GetJsonsByRouteId(stopTime.RouteId);
                    var timeTableDataList = new List <TimeTableData>();

                    if (jsonsToConvert.Count <= 0)
                    {
                        continue;
                    }

                    foreach (var item in jsonsToConvert)
                    {
                        var jsonAsJObject = JsonConvert.DeserializeObject <JObject>(item.Json);
                        timeTableDataList.Add(_converter.Deserialize(jsonAsJObject));
                    }

                    timeTableDataList = timeTableDataList.Where(x => x.StopTimes.Count > 0).ToList();

                    _documentStoreRepository.Save(timeTableDataList);
                    _documentStoreRepository.Delete(jsonsToConvert.Select(x => x.Id).ToList());
                    _documentStoreRepository.Delete(entitiesToDelete.Select(x => x.Id).ToList());
                }
                catch (Exception e)
                {
                    var mes = e.Message;
                }
            }
        }
        public void SetMinuteTimeTables()
        {
            var groupedJoinedTrips = _documentStoreRepository.GetGroupedJoinedModels();

            foreach (var group in groupedJoinedTrips)
            {
                foreach (var joinedTripModel in group.JoinedTripModels)
                {
                    var routeIds                    = joinedTripModel.JoinedTrips.SelectMany(x => x.Stops.Select(y => y.RouteId)).Distinct().ToList();
                    var minuteTimeTableList         = _documentStoreRepository.GetMinuteTimeTableListByBusLineName(joinedTripModel.BusLineName);
                    var existingStopIds             = joinedTripModel.JoinedTrips.SelectMany(x => x.Stops.Select(y => y.StopId).ToList()).Distinct().OrderBy(x => x).ToList();
                    var minuteTimeTableToDeleteList = minuteTimeTableList.Where(x => existingStopIds.All(y => y != x.StopId)).Select(x => x.Id).ToList();
                    minuteTimeTableList = minuteTimeTableList.Where(x => minuteTimeTableToDeleteList.All(y => !y.Equals(x.Id))).ToList();

                    _documentStoreRepository.Delete(minuteTimeTableToDeleteList);

                    minuteTimeTableList = _minuteTimeTableBuilder.BuildList(minuteTimeTableList, routeIds, joinedTripModel.JoinedTrips);

                    _documentStoreRepository.Delete(minuteTimeTableList.Select(x => x.Id).ToList());
                    _documentStoreRepository.Save(minuteTimeTableList);
                }
            }
        }