示例#1
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));
        }
        public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log)
        {
            double lat = double.Parse(req.Query["lat"]);
            double lng = double.Parse(req.Query["lng"]);

            var origin = new GeoCoordinate(lat, lng);
            var jump   = 0.0025;

            // +1,-1 +1,0 +1,+1
            //  0,-1  oo   0,+1
            // -1,-1 -1,0 -1,+1

            var surroundingArea = new List <GeoCoordinate>
            {
                new GeoCoordinate(origin.Latitude + jump, origin.Longitude - jump),
                new GeoCoordinate(origin.Latitude + jump, origin.Longitude),
                new GeoCoordinate(origin.Latitude + jump, origin.Longitude + jump),
                new GeoCoordinate(origin.Latitude, origin.Longitude - jump),
                origin,
                new GeoCoordinate(origin.Latitude, origin.Longitude + jump),
                new GeoCoordinate(origin.Latitude - jump, origin.Longitude - jump),
                new GeoCoordinate(origin.Latitude - jump, origin.Longitude),
                new GeoCoordinate(origin.Latitude - jump, origin.Longitude + jump)
            };

            List <NearByResult> results = new List <NearByResult>();

            try
            {
                foreach (GeoCoordinate pos in surroundingArea)
                {
                    List <NearByResult> places = await GetNearByPlaces(pos.Latitude, pos.Longitude);

                    foreach (NearByResult place in places)
                    {
                        if (!results.Any(x => x.PlaceId == place.PlaceId))
                        {
                            results.Add(place);
                        }
                    }
                }

                foreach (NearByResult nearByResult in results)
                {
                    GooglePlacesPto googlePlacesPto = Context.GooglePlaces.Find(nearByResult.PlaceId);
                    if (googlePlacesPto != null)
                    {
                        // update
                        log.LogInformation($"Updating Google Places {googlePlacesPto.Id}");
                        nearByResult.Adapt(googlePlacesPto);
                        googlePlacesPto.Updated = DateTime.Now;
                        Context.GooglePlaces.Update(googlePlacesPto);
                    }
                    else
                    {
                        // insert
                        log.LogInformation($"Inserting new Google Places {nearByResult.PlaceId}");
                        googlePlacesPto = new GooglePlacesPto();
                        nearByResult.Adapt(googlePlacesPto);
                        googlePlacesPto.Updated = DateTime.Now;
                        PlacePto placePto = FindExistingPlace(log, lat, lng, googlePlacesPto.Name);
                        if (placePto == null)
                        {
                            placePto = new PlacePto
                            {
                                Location = new Point(googlePlacesPto.Longitude, googlePlacesPto.Latitude)
                                {
                                    SRID = 4326
                                },
                                Name         = googlePlacesPto.Name,
                                GooglePlaces = googlePlacesPto
                            };
                            Context.Places.Add(placePto);
                        }
                        googlePlacesPto.Place = placePto;
                        Context.GooglePlaces.Add(googlePlacesPto);
                    }
                    Context.SaveChanges();
                    await RemoveCachedPlace(log, googlePlacesPto.PlaceId.ToString());
                }

                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw;
            }

            return(new OkResult());
        }