public async Task <IActionResult> GetStations(CombustibleType type, int region = 0, int comuna = 0, string distributor = "") { try { var result = await GetStationsWithCache(type, Request); if (result == null) { return(new NotFoundResult()); } var filteredResult = result.Where(r => (region == 0 || (r.id_region != null && r.id_region.ToString() == region.ToString().PadLeft(2, '0'))) && (comuna == 0 || (r.id_comuna != null && r.id_comuna.ToString() == comuna.ToString().PadLeft(2, '0'))) && (distributor == string.Empty || (r.distribuidor != null && r.distribuidor.nombre != null && r.distribuidor.nombre == distributor)) ); //add distance var lat = Request.Query["lat"].ToString(); var lng = Request.Query["lng"].ToString(); if (lat != string.Empty && lng != string.Empty) { AddDistance(filteredResult, double.Parse(lat), double.Parse(lng)); } //filter before add ranking filteredResult = Filters.BaseResultFilter.FilterResult(filteredResult, HttpContext.Request); //add prices ranking AddRanking(filteredResult); return(new OkObjectResult(filteredResult)); } catch (WebException wex) { return(HandleWebException(wex)); } catch (Exception ex) { return(HandleException(ex)); } }
private static async Task <List <Models.CombustibleStation> > GetStationsWithCache(CombustibleType type, HttpRequest context) { const string name = "estaciones"; var typeName = Enum.GetName(typeof(CombustibleType), type) + "/" + name; var data = memStore.SingleOrDefault(s => s.Key == typeName); if (data.Value == null || data.Value.Item1 < DateTime.Now) //check expiration { var tempUrl = string.Format(url, Enum.GetName(typeof(CombustibleType), type).ToLower(), name); using (var result = await Helpers.Net.GetResponse(tempUrl)) { if (result.IsSuccessStatusCode) { var newData = JsonConvert.DeserializeObject <JObject>(await result.Content.ReadAsStringAsync(), Startup.jsonSettings); var stations = JsonConvert.DeserializeObject <List <Models.CombustibleStation> >(newData["data"].ToString(), Startup.jsonSettings); // filter stations var filteredStations = stations.Where(s => s.fecha_hora_actualizacion != null && DateTime.Parse(s.fecha_hora_actualizacion) > DateTime.Now.AddMonths(skipDataBeforeInMonths * -1)) .ToList(); //replace broken links with default image and remove schema to avoid mixed content warnings await RemoveBrokenLinks(filteredStations, "//api.cne.cl/brands/sin%20bandera-horizontal.svg", "http:"); SaveResultsInMongoDB(filteredStations); var newValue = new Tuple <DateTime, List <Models.CombustibleStation> >(DateTime.Now.AddHours(cacheDurationInHours), filteredStations); if (data.Value == null) { memStore.Add(typeName, newValue); } else { memStore[typeName] = newValue; } return(newValue.Item2); } } } else { context.HttpContext.Response.Headers.Add("Cache-Expiration", data.Value.Item1.ToString()); return(data.Value.Item2); } return(null); }