Beispiel #1
0
        public bool UpdateFriendsTags()
        {
            using (flickr_tablesEntities1 db = new flickr_tablesEntities1())
            {
                try
                {
                    List <friend> friends = db.friends.ToList();
                    Flickr        f       = GetInstance();
                    List <string> tags    = new List <string>();
                    var           users   = db.users.ToList();
                    foreach (var user in users)
                    {
                        Console.WriteLine("Looking for tags for users");
                        var tagsUser = f.TagsGetListUser(user.UserId).GroupBy(x => x.TagName).OrderByDescending(g => g.Count()).Take(10).Select(gg => gg.Key).ToList();
                        foreach (string tagU in tagsUser)
                        {
                            usertag utag = new usertag()
                            {
                                UserId = user.UserId,
                                Tag    = tagU
                            };
                            db.usertags.Add(utag);
                        }
                        ;
                        db.SaveChanges();
                        foreach (var tagItem in tagsUser)    //select all tags from the list for iterated friend
                        {
                            var result = (from wordP in db.words
                                          where wordP.Word1 == tagItem
                                          select wordP).ToList(); //check if for the iterated friend we have some tags like in ourdatabase
                            foreach (var itemRes in result)       //check the above result...if Yes then we add the friend to the specific category of tags
                            {
                                linkusercategory catLink = new linkusercategory()
                                {
                                    Category = itemRes.CategoryId,
                                    UserId   = user.UserId,
                                    Tag      = itemRes.Word1
                                };
                                db.linkusercategories.Add(catLink);
                            }
                        }
                        db.SaveChanges();
                    }
                    Dictionary <string, List <string> > tagsList = new System.Collections.Generic.Dictionary <string, System.Collections.Generic.List <string> >();
                    foreach (var user in friends)
                    {
                        Console.WriteLine("Foreach friend it will be retreived all the tags...");
                        tags = f.TagsGetListUser(user.IdFriend).GroupBy(x => x.TagName).OrderByDescending(g => g.Count()).Take(10).Select(gg => gg.Key).ToList();
                        tagsList.Add(user.IdFriend, tags);
                        foreach (string tafg in tags)
                        {
                            friendtag ftag = new friendtag()
                            {
                                FriendId = user.IdFriend,
                                Tag      = tafg,
                            };
                            db.friendtags.Add(ftag);
                        }
                        user.Tags = 1;
                    }

                    db.SaveChanges();
                    try
                    {
                        foreach (var item in tagsList)          //select list of tags foreach firends
                        {
                            foreach (var tagItem in item.Value) //select all tags from the list for iterated friend
                            {
                                var result = (from wordP in db.words
                                              where wordP.Word1 == tagItem
                                              select wordP).ToList(); //check if for the iterated friend we have some tags like in ourdatabase
                                foreach (var itemRes in result)       //check the above result...if Yes then we add the friend to the specific category of tags
                                {
                                    linkfriendcategory catLink = new linkfriendcategory()
                                    {
                                        IdCategory = itemRes.CategoryId,
                                        IdFriend   = item.Key,
                                        Tag        = itemRes.Word1
                                    };
                                    db.linkfriendcategories.Add(catLink);
                                }
                            }
                        }
                        db.SaveChanges();
                        Console.WriteLine("Tagging proccess complete...");
                    }
                    catch (Exception)
                    {
                    }
                    return(true);
                }
                catch (Exception)
                {
                    db.Dispose();
                    return(false);
                }
            }
        }