Пример #1
0
 public RaveDetails(Rave aRave, Owner aOwner, Venue aVenue)
 {
     RaveID = aRave.RaveID;
     Rave = aRave;
     Owner = aOwner;
     Venue = aVenue;
 }
        public static IList<RaveDetails> GetRaveDetailsFromEventIDs(IEnumerable<Int64> eventIds)
        {
            _fb.AccessToken = _GenericAccessToken;

            string fields = "eid, name, pic_square, start_time, end_time, is_date_only, creator, update_time, location, venue";
            string eidList = string.Join(",", eventIds.ToArray());
            dynamic fqlResult = _fb.Get("fql", new { q = String.Format("SELECT {0} FROM event WHERE eid IN ({1})", fields, eidList) });
            JsonArray fqlEventResults = fqlResult.data;

            IList<RaveDetails> eventResults = new List<RaveDetails>();
            HashSet<Venue> venueResults = new HashSet<Venue>();
            HashSet<Rave> raveResults = new HashSet<Rave>();
            foreach (dynamic fqlEvent in fqlEventResults)
            {
                Int64? creatorId = fqlEvent.creator ?? 0;
                Int64? eventId = fqlEvent.eid;
                Int64? venueId = 0;
                if (fqlEvent.venue.Count > 0)
                {
                     dynamic jsonVenue = (JsonObject)fqlEvent.venue;
                     venueId = jsonVenue.id ?? 0;
                }

                Rave aRave = null;
                Venue aVenue = null;
                Owner aOwner = null;
                if (eventId != null)
                {
                    // Get the owner; no chance for duplications
                    aOwner = GetRaveOwnerFromUserID(creatorId);

                    // Before querying for the Venue, make sure we haven't already queried it
                    if (venueResults.Any(v => v.VenueID == venueId))
                    {
                        aVenue = venueResults.First(v => v.VenueID == venueId);
                    }
                    else if (fqlEvent.venue.Count > 0)
                    {
                        aVenue = GetVenueFromJsonObject((JsonObject)fqlEvent.venue);
                        if (aVenue == null)
                        {
                          // If no facebook page exists for this Venue, create a new negative ID
                          // All negative ID's therefore show that the Venue does not exist on Facebook
                                                    aVenue = new Venue();
                          using (RaveRadarContext _db = new RaveRadarContext())
                          {
                              long dbMin = _db.Venues.Min(v => v.VenueID).GetValueOrDefault(0);
                              long syncMin = venueResults.Min(v => v.VenueID).GetValueOrDefault(0);
                              aVenue.VenueID = (dbMin <= syncMin ? dbMin : syncMin) - 1;
                                                            aVenue.Name = fqlEvent.location ?? string.Empty;
                                                            aVenue.GetLocationFromGoogle(fqlEvent.location ?? string.Empty);

                                                            // If no location found, reset venue to null
                                                            if (aVenue.GetLocation() == null)
                                                            {
                                                                aVenue = null;
                                                            }
                          }
                        }

                                                if (aVenue != null)
                                                {
                                                    venueResults.Add(aVenue);
                                                }
                    }

                    // Before querying for the Rave, make sure we haven't already queried it
                    if (raveResults.Any(r => r.RaveID == eventId))
                    {
                        aRave = raveResults.First(r => r.RaveID == eventId);
                    }
                    else
                    {
                        aRave = new Rave();
                        aRave.RaveID = eventId ?? 0; // Should never happen
                        aRave.OwnerID = aOwner.OwnerID;
                        if (aVenue == null) { aRave.VenueID = null; } else { aRave.VenueID = aVenue.VenueID; }
                        aRave.Name = fqlEvent.name ?? string.Empty;
                        aRave.PicURL = fqlEvent.pic_square ?? ConfigurationManager.AppSettings["DefaultRaveIcon"].ToString();

                        aRave.StartTime = ConvertFacebookDateToLocal(fqlEvent.start_time, fqlEvent.is_date_only);
                        if (fqlEvent.end_time == null) { aRave.EndTime = null; }
                        else { aRave.EndTime = ConvertFacebookDateToLocal(fqlEvent.end_time, fqlEvent.is_date_only); }
                        aRave.IsDateOnly = fqlEvent.is_date_only;
                        aRave.Location = fqlEvent.location ?? string.Empty;
                        aRave.IsApproved = (aOwner == null ? false : aOwner.IsTrusted);
                        aRave.SubmitterID = null;
                        aRave.UpdateTime = DateTimeConvertor.FromUnixTime((double)fqlEvent.update_time);
                        raveResults.Add(aRave);
                    }
                }

                // Put all the details together to create one object
                eventResults.Add(new RaveDetails(aRave, aOwner, aVenue));
            }

            return eventResults;
        }