//add new record to ArticleAndTagTable public void AddTagsToArticle(Guid articleId, List <TagViewModel> tags) { if (!UnitOfWork.Articles.Exists(a => a.Id == articleId)) { throw new ObjectNotFoundException(); } var unmappedTags = Mapper.Map <List <TagViewModel>, List <Tag> >(tags); foreach (var tag in unmappedTags) { if (UnitOfWork.Tags.Exists(t => t.Title == tag.Title)) { var foundedTag = UnitOfWork.Tags.Get(t => t.Title == tag.Title); var articleAndTagEntity = new ArticleAndTag { ArticleId = articleId, TagId = foundedTag.Id }; UnitOfWork.ArticlesAndTags.Create(articleAndTagEntity); } } UnitOfWork.Save(); }
public void Create() { Article article = ctx.PostValue <Article>(); AddArticle(ref article); ArticleAndTag articleAndTag = ctx.PostValue <ArticleAndTag>(); AddTag(article, articleAndTag); }
//public void Index() //{ // // 获取[文章]循环块,根据文章找标签 // DataPage<Article> articles = db.findPage<Article>(""); // IBlock block = getBlock("list"); // foreach (Article a in articles.Results) // { // block.Set("article.Title", a.Title); // block.Set("article.ReadLink", to(new ArticleController().Read, a.Id)); // block.Set("article.Content", a.Content); // block.Set("article.Created", a.Created); // block.Set("article.ReadCount", a.ReadCount); // block.Set("article.Tag.Name", a.Tag.Name);//删 // block.Set("article.ListLink", to(new ArticleController().List, a.Tag.Id));//删 // block.Next(); // } // set("page", articles.PageBar); //} public void Index() { DataPage <Article> articles = db.findPage <Article>(""); List <Tag> tags = Tag.findAll(); List <ArticleAndTag> articleAndtags = ArticleAndTag.findAll(); bindArticleAndTag(articles.Results, tags, articleAndtags); set("page", articles.PageBar); set("IndexLink", to(new MainController().Index)); }
public List <ArticleAndTag> Angualr2() { List <ArticleAndTag> articleTags = new List <ArticleAndTag>(); foreach (Article article in db.Articles) { ArticleAndTag ang = new ArticleAndTag(article.ArticleID, article.Name, article.Tags); articleTags.Add(ang); } return(articleTags); }
/// <summary> /// 根据文章添加标签,同时将文章和标签的关系存入到ArticleAndTag关系表中 /// </summary> /// <param name="a">文章对象</param> /// <param name="articleAndTag">文章和标签关系表对象</param> private void AddTag(Article a, ArticleAndTag articleAndTag) { //分解Tag char[] splitMake = { ',' }; string[] strTags = ctx.Post("Editor$Edit$Advanced$txbTag").Split(splitMake);//也挺重要,Editor$Edit$Advanced$txbTag(name),Editor_Edit_Advanced_txbTag(id)应该是对应的 //加载标签 List <Tag> listTag = db.findAll <Tag>(); List <string> listTagName = new List <string>(); foreach (Tag tag in listTag) { listTagName.Add(tag.Name); } foreach (string s in strTags) { Tag t = new Tag(); t = ctx.PostValue(t) as Tag; if (!listTagName.Contains(s)) { listTagName.Add(s); t.Name = s; Result result = db.insert(t); if (result.HasErrors) { continue; } } else { //查找Tag t = Tag.find("Name=:n").set("n", s).first(); } //处理Tag articleAndTag.article = new Article { Id = a.Id }; articleAndTag.tag = new Tag { Id = t.Id }; if (Tag.findById(t.Id) != null) { db.insert(articleAndTag); } } }
public void Update(int id) { Article a = Article.findById(id); a = ctx.PostValue(a) as Article; UpdateArticle(ref a); //先删除对应表里文章对应的所有标签,然后重新添加文章对应的标签,不是一个好办法,先这么实现 List <ArticleAndTag> removeArticleAndTag = db.find <ArticleAndTag>("articleId = " + id).list(); foreach (ArticleAndTag aAndt in removeArticleAndTag) { db.delete(aAndt); } ArticleAndTag articleAndTag = ctx.PostValue <ArticleAndTag>(); AddTag(a, articleAndTag); }
/// <summary> /// Get a table of all articles as rows and all tags as columns /// </summary> public List <ArticleTagCounts> GetArticleTagCounts() { List <ArticleTagCounts> articleTags = new List <ArticleTagCounts>(); foreach (Article article in db.Articles) { ArticleTagCounts articleTag = new ArticleTagCounts(article.ArticleID, db.Tags.Count); ArticleAndTag ang = new ArticleAndTag(article.ArticleID, article.Name, article.Tags); for (int tag = 0; tag < db.Tags.Count; tag++) { articleTag.TagCounts[tag] = article.Tags.Any(x => x.Name == db.Tags[tag].Name) ? 1.0 : 0.0; } articleTags.Add(articleTag); } return(articleTags); }
public void Read(int id) { HideLayout(typeof(wojilu.cms.xw.Controller.ArticleController)); Article a = Article.findById(id); a.ReadCount++; db.update(a); bind("article", a); set("CommentLink", to(new ArticleController().SaveComment, id));//保存文章的评论到数据库中 //ctx.SetItem( "article", a ); //ctx.SetItem( "category", a.Category ); //设置评论到Read页面 string sqlStr = string.Format("select * from Comment where ArticleId = {0} order by OrderBy asc", id); List <Comment> commentsByArticleId = db.findBySql <Comment>(sqlStr); IBlock blockComment = getBlock("listcomment"); foreach (Comment comment in commentsByArticleId) { blockComment.Set("comment.OrderBy", comment.OrderBy); blockComment.Set("comment.Created", comment.Created); blockComment.Set("comment.BlogAddress", comment.BlogAddress); blockComment.Set("comment.UserName", comment.UserName); blockComment.Set("comment.Content", comment.Content); blockComment.Next(); } //显示标签 List <Tag> tags = Tag.findAll(); List <ArticleAndTag> articleAndtags = ArticleAndTag.findAll(); bindArticleAndTag(a, tags, articleAndtags); }
//#region OldAritcleTag方法 //private string ComputeTagCount(Tag tag) //{ // List<Article> articles = Article.findAll(); // foreach (Article article in articles) // { // if (article.Tag == null) // return "0"; // if (article.Tag.Id == tag.Id) // Tagcount++; // } // return Tagcount.ToString(); //} //#endregion private string ComputeTagCount(Tag tag) { List <ArticleAndTag> articleAndtag = ArticleAndTag.find("tagId = " + tag.Id).list(); return(articleAndtag.Count.ToString()); }
/// <summary> /// 根据文章添加标签,同时将文章和标签的关系存入到ArticleAndTag关系表中 /// </summary> /// <param name="a">文章对象</param> /// <param name="articleAndTag">文章和标签关系表对象</param> private void AddTag(Article a, ArticleAndTag articleAndTag) { //分解Tag char[] splitMake = { ',' }; string[] strTags = ctx.Post("Editor$Edit$Advanced$txbTag").Split(splitMake);//也挺重要,Editor$Edit$Advanced$txbTag(name),Editor_Edit_Advanced_txbTag(id)应该是对应的 //加载标签 List<Tag> listTag = db.findAll<Tag>(); List<string> listTagName = new List<string>(); foreach (Tag tag in listTag) { listTagName.Add(tag.Name); } foreach (string s in strTags) { Tag t = new Tag(); t = ctx.PostValue(t) as Tag; if (!listTagName.Contains(s)) { listTagName.Add(s); t.Name = s; Result result = db.insert(t); if (result.HasErrors) { continue; } } else { //查找Tag t = Tag.find("Name=:n").set("n", s).first(); } //处理Tag articleAndTag.article = new Article { Id = a.Id }; articleAndTag.tag = new Tag { Id = t.Id }; if(Tag.findById(t.Id)!=null) db.insert(articleAndTag); } }