Ejemplo n.º 1
0
        /*
         * Method that adds and updates any venues to our database using Google Places API
         */
        public void UpdateVenues()
        {
            // Only want to update Venues database once a week
            var   venues           = _venueRepository.GetAllVenues();
            Venue mostRecentUpdate = venues.OrderByDescending(v => v.DateUpdated).FirstOrDefault();

            // If there is no update or if most recent update was more than two weeks ago
            // search places and update Venues database
            if (mostRecentUpdate == null || mostRecentUpdate.DateUpdated < DateTime.Now.AddDays(-14))
            {
                // Get list of places
                List <PlaceSearchResult> places = _placesApi.GetVenues();

                foreach (var place in places)
                {
                    // Check if we already have this venue in database
                    Venue existingVenue = venues.FirstOrDefault(v => v.GooglePlaceId == place.PlaceId);

                    // If not in database, add it
                    if (existingVenue == null)
                    {
                        Venue venue = new Venue
                        {
                            GooglePlaceId = place.PlaceId,
                            Name          = place.Name,
                            DateUpdated   = DateTime.Now
                        };

                        _venueRepository.AddVenue(venue);
                    }
                }

                // Update most recent update
                mostRecentUpdate.DateUpdated = DateTime.Now;
                _venueRepository.Edit(mostRecentUpdate);

                UpdateVenueDetails();
            }
        }