Example #1
0
        public override void Execute()
        {
            var facebookClient = new FacebookClient(AccessToken);
            string userTag = DocumentSession.Advanced.DocumentStore.Conventions.FindTypeTagName(typeof(User));

            dynamic friendsData = facebookClient.Get("/fql", new { q = "SELECT uid,name,sex FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = me())" });

            List<dynamic> friendsList = JsonConvert.DeserializeObject<List<dynamic>>(friendsData.data.ToString());
            string[] friendsIds = friendsList.Select(x => (string)string.Format("{0}/{1}", userTag, x.uid)).ToArray();
            var friendsDocs = DocumentSession.Load<User>(friendsIds);

            var userFriendsDocId = DocumentSession.BuildRavenId<UserFriends>(UserId);

            var userFriendsDoc = DocumentSession.Load<UserFriends>(userFriendsDocId);

            if (userFriendsDoc == null)
            {
                userFriendsDoc = new UserFriends()
                {
                    Id = DocumentSession.BuildRavenId<UserFriends>(UserId)
                };

                DocumentSession.Store(userFriendsDoc);
            }

            userFriendsDoc.FriendsIds = friendsIds.ToList();

            for (var i = 0; i < friendsList.Count; i++)
            {
                if (friendsDocs[i] == null)
                {
                    dynamic friendData = friendsList[i];

                    var userToAdd = new User()
                    {
                        Id = string.Format("{0}/{1}", userTag, friendData.uid),
                        Name = friendData.name,
                        IsActive = false,
                        GenderPreference = UserGender.Unknown,
                        Gender = (friendData.sex == "male") ? UserGender.Male : (friendData.sex == "female") ? UserGender.Female : UserGender.Unknown
                    };

                    DocumentSession.Store(userToAdd);

                    if (i % 500 == 0)
                    {
                        DocumentSession.SaveChanges();
                    }
                }
            }

            DocumentSession.SaveChanges();
        }
Example #2
0
            public ValidCrushes()
            {
                var existingUsers = Builder<User>.CreateListOfSize(10)
                                        .All().With(x => x.Id, null)
                                        .Build().ToList();

                existingUsers.ForEach(x => {
                    Session.Store(x);
                    var friends = new UserFriends()
                    {
                        Id = Session.BuildRavenId<UserFriends>(x.Id.ToLongId()),
                        FriendsIds = existingUsers.Where(y => y.Id != x.Id).ToList().GetRange(0,5).Select(u => u.Id).ToList()
                    };

                    Session.Store(friends);
                });

                Session.SaveChanges();

                service = new CrushService(Session);
            }