public async Task <IActionResult> Update([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { string lat = req.Query["lat"]; string lng = req.Query["lng"]; if (string.IsNullOrWhiteSpace(lat) || string.IsNullOrWhiteSpace(lng)) { return(new NotFoundResult()); } try { int page = 1; log.LogInformation($"Getting page {page} for {lat}, {lng}"); List <Establishment> establishments = await GetBadFoodHygieneRatings(lat, lng, page); while (establishments != null && establishments.Count() > 0) { foreach (Establishment establishment in establishments) { FoodHygieneRatingPto foodHygieneRatingPto = Context.FoodHygieneRatings.Find(establishment.FHRSID); if (foodHygieneRatingPto != null) { // update log.LogInformation($"Updating {establishment.FHRSID}"); establishment.Adapt(foodHygieneRatingPto); foodHygieneRatingPto.Updated = DateTime.Now; Context.FoodHygieneRatings.Update(foodHygieneRatingPto); } else { // insert log.LogInformation($"Inserting {establishment.FHRSID}"); foodHygieneRatingPto = new FoodHygieneRatingPto(); establishment.Adapt(foodHygieneRatingPto); foodHygieneRatingPto.Updated = DateTime.Now; // find generic place var place = FindExistingPlace(log, foodHygieneRatingPto.Latitude, foodHygieneRatingPto.Longitude, foodHygieneRatingPto.BusinessName); if (place == null) { place = new PlacePto { Location = new Point(foodHygieneRatingPto.Longitude, foodHygieneRatingPto.Latitude) { SRID = 4326 }, Name = foodHygieneRatingPto.BusinessName, FoodHygieneRating = foodHygieneRatingPto }; Context.Places.Add(place); } foodHygieneRatingPto.Place = place; Context.FoodHygieneRatings.Add(foodHygieneRatingPto); } Context.SaveChanges(); await RemoveCachedPlace(log, foodHygieneRatingPto.PlaceId.ToString()); } page++; establishments = await GetBadFoodHygieneRatings(lat, lng, page); } Context.SaveChanges(); } catch (Exception ex) { throw; } return(new OkResult()); }
public async Task <IActionResult> Get([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { string lat = req.Query["lat"]; string lng = req.Query["lng"]; int.TryParse(req.Query["distance"], out int distance); if (distance == 0) { distance = 5000; } var location = new Point(double.Parse(lng), double.Parse(lat)) { SRID = 4326 }; var requestPto = new GetShitRequestPto { Requested = DateTime.Now, Location = location, Distance = distance, ClientIpAddress = req.HttpContext.Connection.RemoteIpAddress.ToString() }; _context.GetShitRequests.Add(requestPto); _context.SaveChanges(); IQueryable <PlacePto> ptos = _context.Places.Where(x => x.Location.Distance(location) <= distance && (x.FoodHygieneRating.RatingValue == "0" || x.FoodHygieneRating.RatingValue == "1" || x.FoodHygieneRating.RatingValue == "2" || (x.GooglePlaces.Rating > 0 && x.GooglePlaces.Rating < 3.5) || (x.TripAdvisorLocation.Rating > 0 && x.TripAdvisorLocation.Rating < 3.5))) .OrderBy(x => x.Location.Distance(location)) .Take(50); if (ptos.Count() == 0) { return(new NotFoundResult()); } var places = new List <PlaceDto>(); foreach (PlacePto pto in ptos) { string cachedPlace = await _cache.GetStringAsync(pto.Id.ToString()); if (cachedPlace != null) { places.Add(JsonConvert.DeserializeObject <PlaceDto>(cachedPlace)); } else { var placeDto = new PlaceDto { Id = pto.Id, Lat = pto.Location.Y, Lng = pto.Location.X, Name = pto.Name }; FoodHygieneRatingPto foodHygieneRatingPto = _context.FoodHygieneRatings.Where(x => x.PlaceId == pto.Id).SingleOrDefault(); if (foodHygieneRatingPto != null) { placeDto.FoodHygieneRating = foodHygieneRatingPto.RatingValue; placeDto.FoodHygieneRatingId = foodHygieneRatingPto.FHRSID; } GooglePlacesPto googlePlacesPto = _context.GooglePlaces.Where(x => x.PlaceId == pto.Id).SingleOrDefault(); if (googlePlacesPto != null) { placeDto.GooglePlacesId = googlePlacesPto.Id; placeDto.GooglePlacesRating = googlePlacesPto.Rating; placeDto.GooglePlacesRatings = googlePlacesPto.UserRatingsTotal; } TripAdvisorPto tripAdvisorLocation = _context.TripAdvisorLocations.Where(x => x.PlaceId == pto.Id).SingleOrDefault(); if (tripAdvisorLocation != null) { placeDto.TripAdvisorUrl = $"https://www.tripadvisor.co.uk{tripAdvisorLocation.SummaryObject.detailPageUrl}"; placeDto.TripAdvisorRating = tripAdvisorLocation.SummaryObject.averageRating; placeDto.TripAdvisorRatings = tripAdvisorLocation.SummaryObject.userReviewCount; } if (placeDto.FoodHygieneRatingId != null || placeDto.GooglePlacesId != null) { await _cache.SetStringAsync(pto.Id.ToString(), JsonConvert.SerializeObject(placeDto)); places.Add(placeDto); } } } return(new OkObjectResult(places)); }