public FlickrNet.ContactCollection GetFriends(string userId)
        {
            linkfriend ob = null;

            try
            {
                try
                {
                    ob = (from t in db.linkfriends
                          where t.IdUser == userId
                          select t).FirstOrDefault();
                }
                catch (Exception) { ob = null; }
                if (ob == null)//inseamna ca nu am prietenii inca adaugati in DB
                {
                    if (Models.User.FlickerObj.UserFl.ContainsKey(userId))
                    {
                        Flickr f = Models.User.FlickerObj.UserFl[userId];
                        FlickrNet.ContactCollection friends = f.ContactsGetPublicList(userId);//preiau din FLICKR
                        string realName = ""; string description = "";
                        foreach (var item in friends)
                        {
                            var person = f.PeopleGetInfo(item.UserId);
                            if (person.RealName != null)
                            {
                                realName = person.RealName;
                            }
                            else
                            {
                                realName = "";
                            }
                            if (person.Description.Length > 50)
                            {
                                description = Models.StringTool.Truncate(person.Description, 50) + "...";
                            }
                            else
                            {
                                description = person.Description;
                            }

                            item.PathAlias = item.BuddyIconUrl;
                            friend fr = new friend //adaug in DB
                            {
                                FullName    = realName,
                                IdFriend    = item.UserId,
                                UserName    = item.UserName,
                                PhotoUrl    = item.BuddyIconUrl,
                                Description = description
                            };
                            linkfriend link = new linkfriend
                            {
                                IdUser   = userId,
                                IdFriend = item.UserId
                            };
                            try
                            {
                                db.friends.Add(fr);
                                db.linkfriends.Add(link);
                            }
                            catch (Exception)
                            {
                            }
                        }
                        db.SaveChanges();
                        Models.ListOfFriends.coll = friends;
                        return(friends);
                    }
                    else //inseamna ca exista prieteni
                    {
                        ContactCollection coll = new ContactCollection();
                        Contact           con  = new Contact();
                        var friends            = db.linkfriends.Where(idx => idx.IdUser == userId).ToList();//preiau din DB
                        foreach (var item in friends)
                        {
                            var people = db.friends.Where(idx => idx.IdFriend == item.IdFriend).FirstOrDefault();
                            con.UserId     = people.IdFriend;
                            con.RealName   = people.FullName;
                            con.UserName   = people.UserName;
                            con.PathAlias  = people.PhotoUrl;
                            con.IconServer = people.Description;//we use some variable already existent so we not create another model
                            coll.Add(con);
                        }
                        return(coll);
                    }
                }
                else
                {
                    ContactCollection coll = new ContactCollection();
                    var friends            = db.linkfriends.Where(idx => idx.IdUser == userId).ToList();
                    foreach (var item in friends)
                    {
                        Contact con    = new Contact();
                        var     people = db.friends.Where(idx => idx.IdFriend == item.IdFriend).FirstOrDefault();
                        con.UserId     = people.IdFriend;
                        con.RealName   = people.FullName;
                        con.UserName   = people.UserName;
                        con.PathAlias  = people.PhotoUrl;
                        con.IconServer = people.Description;
                        coll.Add(con);
                    }
                    return(coll);
                }
            }
            catch (Exception)
            {
                db.Dispose();
                return(null);
            }
        }
Пример #2
0
        public bool UpdateFriends()
        {
            using (flickr_tablesEntities1 db = new flickr_tablesEntities1())
            {
                try
                {
                    List <user> users = db.users.ToList();
                    Console.WriteLine(users.Count + " users in database...");
                    Console.WriteLine("START UPDATE");
                    Console.WriteLine("Deleting all friends...");
                    DeleteFriends();
                    foreach (var item in users)
                    {
                        Flickr f = GetInstance();
                        FlickrNet.ContactCollection friends = f.ContactsGetPublicList(item.UserId);
                        string realName = ""; string description = "";
                        int    count = friends.Count;
                        int    contor = 0;
                        foreach (var itemFr in friends)
                        {
                            var person = f.PeopleGetInfo(itemFr.UserId);
                            if (person.RealName != null)
                            {
                                realName = person.RealName;
                            }
                            else
                            {
                                realName = "";
                            }
                            if (person.Description.Length > 50)
                            {
                                description = Truncate(person.Description, 50) + "...";
                            }
                            else
                            {
                                description = person.Description;
                            }

                            itemFr.PathAlias = itemFr.BuddyIconUrl;
                            friend fr = new friend
                            {
                                FullName    = realName,
                                IdFriend    = itemFr.UserId,
                                UserName    = itemFr.UserName,
                                PhotoUrl    = itemFr.BuddyIconUrl,
                                Description = description
                            };
                            linkfriend link = new linkfriend
                            {
                                IdUser   = item.UserId,
                                IdFriend = itemFr.UserId
                            };
                            try
                            {
                                if (db.friends.Where(a => a.IdFriend == fr.IdFriend).Count() == 0)
                                {
                                    db.friends.Add(fr);
                                    db.SaveChanges();
                                }
                                db.linkfriends.Add(link);
                                db.SaveChanges();
                                Console.WriteLine("Saving friends:");
                                if (contor >= count / 4)
                                {
                                    Console.Write("---25%---");
                                }
                                if (contor >= count / 3)
                                {
                                    Console.Write("---32%---");
                                }
                                if (contor >= count / 2)
                                {
                                    Console.Write("---50%---");
                                }
                                if (contor == count / 1.5)
                                {
                                    Console.Write("---80%---");
                                }
                                contor++;
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    Console.WriteLine("Update friends complete...");
                    return(true);
                }
                catch (Exception) { return(false); }
            }
        }