public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log)
        {
            var geoId    = 186356;
            var page     = 1;
            var lastPage = false;

            do
            {
                var r = await GetRestaurantsOnPage(geoId, page, log);

                if (r.Item1 != null)
                {
                    foreach (Summary summary in r.Item1)
                    {
                        Details details = await GetRestaurantDetails(geoId, summary.locationId, log);

                        try
                        {
                            if (details != null)
                            {
                                TripAdvisorPto tripAdvisorPto = Context.TripAdvisorLocations.Find(summary.locationId);
                                if (tripAdvisorPto != null)
                                {
                                    // update
                                    log.LogInformation($"Updating TripAdvisor location {summary.locationId}");
                                    tripAdvisorPto.Rating        = summary.averageRating;
                                    tripAdvisorPto.SummaryObject = summary;
                                    tripAdvisorPto.DetailsObject = details;
                                    tripAdvisorPto.Updated       = DateTime.Now;
                                    Context.Update(tripAdvisorPto);
                                }
                                else
                                {
                                    // insert
                                    log.LogInformation($"Inserting new TripAdvisor location {summary.locationId}");
                                    tripAdvisorPto = new TripAdvisorPto
                                    {
                                        LocationId    = summary.locationId,
                                        Rating        = summary.averageRating,
                                        SummaryObject = summary,
                                        DetailsObject = details,
                                        Updated       = DateTime.Now
                                    };
                                    PlacePto placePto = FindExistingPlace(log, details.location.latitude, details.location.longitude, summary.name);
                                    if (placePto == null)
                                    {
                                        placePto = new PlacePto
                                        {
                                            Location = new Point(details.location.latitude, details.location.longitude)
                                            {
                                                SRID = 4326
                                            },
                                            Name = summary.name,
                                            TripAdvisorLocation = tripAdvisorPto
                                        };
                                        Context.Places.Add(placePto);
                                    }
                                    tripAdvisorPto.Place = placePto;
                                    Context.TripAdvisorLocations.Add(tripAdvisorPto);
                                }
                                Context.SaveChanges();
                                await RemoveCachedPlace(log, tripAdvisorPto.PlaceId.ToString());
                            }
                        }
                        catch (Exception ex)
                        {
                            log.LogError(ex.Message);
                        }
                    }
                    page++;
                }
                lastPage = r.Item2;
            } while (!lastPage);

            Context.SaveChanges();
            return(new OkResult());
        }
Пример #2
0
        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));
        }