Ejemplo n.º 1
0
        public void AddLink(LinkEditViewModel linkVm)
        {
            var linkDb = new Links();

            linkDb.Title       = linkVm.Title;
            linkDb.Description = linkVm.Description;
            linkDb.Url         = linkVm.Url;

            int linkId = -1;

            using (var ctx = new DbModel())
            {
                ctx.Links.Add(linkDb);

                ctx.SaveChanges();
                linkId = linkDb.ID;


                var linksKeywordsDb = new List <LinksKeywords>();
                var keywordsDb      = new List <Keywords>();

                if (linkVm.SelectedKeywords != null && linkId != -1)
                {
                    linkVm.SelectedKeywords.ForEach(k =>
                    {
                        var intId         = Convert.ToInt32(k);
                        var keywordDb     = new Keywords();
                        keywordDb.Keyword = ctx.Keywords.Single(kwd => kwd.ID == intId).Keyword;
                        //keywordDb.Keyword = k.Text;
                        keywordsDb.Add(keywordDb);

                        var linkKeywordDb       = new LinksKeywords();
                        linkKeywordDb.KeywordID = Convert.ToInt32(k);
                        linkKeywordDb.LinkID    = linkId;
                        linksKeywordsDb.Add(linkKeywordDb);
                    });

                    ctx.Keywords.AddRange(keywordsDb);
                    ctx.LinksKeywords.AddRange(linksKeywordsDb);

                    ctx.SaveChanges();
                }
            }
        }
Ejemplo n.º 2
0
        public List <LinkViewModel> GetLinks(int count)
        {
            using (var ctx = new DbModel())
            {
                var resultVm = new List <LinkViewModel>();

                var links = ctx.Links.OrderByDescending(l => l.ID).Take(count).ToList();
                links.ForEach(l =>
                {
                    var link = new LinkViewModel
                    {
                        Id          = l.ID,
                        Title       = l.Title,
                        Description = l.Description,
                        Url         = l.Url
                    };
                    resultVm.Add(link);
                });

                var linkIds = links.Select(l => l.ID);

                var x = from k in ctx.Keywords
                        join lk in ctx.LinksKeywords on k.ID equals lk.KeywordID
                        where linkIds.Contains(lk.LinkID)
                        select new { KeywordID = k.ID, LinkID = lk.LinkID, Keyword = k.Keyword };

                var linksKeywords   = new List <LinksKeywords>();
                var keywordsPerLink = new List <KeywordViewModel>();
                x.ToList().ForEach(y =>
                {
                    var linkKeyword = new LinksKeywords
                    {
                        LinkID    = y.LinkID,
                        KeywordID = y.KeywordID
                    };
                    var keyword = new KeywordViewModel
                    {
                        Id      = y.KeywordID,
                        Keyword = y.Keyword
                    };

                    linksKeywords.Add(linkKeyword);
                    keywordsPerLink.Add(keyword);
                });

                //var linksKeywords = from k in ctx.Keywords
                //                    join lk in ctx.LinksKeywords on k.ID equals lk.KeywordID
                //                    where linkIds.Contains(lk.LinkID)
                //                    select new LinksKeywords { KeywordID = k.ID, LinkID = lk.LinkID };

                //var keywordsPerLink = from k in ctx.Keywords
                //                      join lk in ctx.LinksKeywords on k.ID equals lk.KeywordID
                //                      where linkIds.Contains(lk.LinkID)
                //                      select new KeywordViewModel { Id = k.ID, Keyword = k.Keyword };


                resultVm.ForEach(rvm =>
                {
                    var lk = linksKeywords.Where(linksKey => linksKey.LinkID == rvm.Id);
                    if (lk != null && lk.Any())
                    {
                        var lkIds    = lk.Select(xy => xy.KeywordID);
                        rvm.Keywords = keywordsPerLink.Where(k => lkIds.Contains(k.Id))
                                       .Select(k => new KeywordViewModel {
                            Id = k.Id, Keyword = k.Keyword
                        })
                                       .ToList();
                    }
                    else
                    {
                        rvm.Keywords = new List <KeywordViewModel>();
                    }
                });

                //resultVm.ForEach(rvm =>
                //{
                //    rvm.Keywords = keywordsPerLink.Where(keywords => keywords.LinkId == rvm.Id).ToList();
                //});

                return(resultVm);
            }
        }