예제 #1
0
        public void AddAllEventsFromFacebook()
        {
            var facebookClient = new FacebookClient();

            var getLocationsTask = facebookClient.GetLocations(FacebookSettings.AccessToken);

            //remove all existing events from database

            var exEvents = GetAllActiveEvents();

            foreach (var ev in exEvents)
            {
                DeleteEvent(ev);
            }

            //wait for locations task to finish

            Task.WaitAll(getLocationsTask);
            var locations = getLocationsTask.Result;

            //get all events from facebook

            var getEventsTask = facebookClient.GetEvents(FacebookSettings.AccessToken, locations);

            //wait for event task to finish
            Task.WaitAll(getEventsTask);

            var events = getEventsTask.Result;

            //add events to database
            foreach (var ev in events)
            {
                var eventStartTime = DateTime.Parse(ev.StartTime, null, System.Globalization.DateTimeStyles.RoundtripKind);

                DateTime eventEndTime;
                if (String.IsNullOrEmpty(ev.EndTime))
                {
                    eventEndTime = eventStartTime.AddHours(3);
                }
                else
                {
                    eventEndTime = DateTime.Parse(ev.EndTime, null, System.Globalization.DateTimeStyles.RoundtripKind);
                }

                var newEvent = Event.Create(ev.Place, ev.FacebookId, ev.Name, ev.Description, eventStartTime,
                                            eventEndTime,
                                            ev.Cover.Source);
                AddEvent(newEvent);
            }

            _databaseContext.SaveChanges();
        }