Beispiel #1
0
 public JsonResult SaveTag(TagViewModel tag)
 {
     Thread.Sleep(300);
     using (var context = new Context())
     {
         var article = context.Articles.Find(articleId);
         var dbTag = context.Tags.Where(x => x.Name == tag.name).FirstOrDefault();
         if (dbTag == null)
         {
             dbTag = context.Tags.Add(new Tag { Name = tag.name });
         }
         if (!context.ArticleTags.Any(x => x.Article.Id == articleId && x.Tag.Name == tag.name))
         {
             context.ArticleTags.Add(new ArticleTag { Article = article, Tag = dbTag });
         }
         context.SaveChanges();
     }
     return Json(this.GetTagsFromDb(), JsonRequestBehavior.AllowGet); //Content(r);
 }
Beispiel #2
0
 public JsonResult RemoveTag(TagViewModel tag)
 {
     Thread.Sleep(300);
     string r = string.Format("removed '{0}' from article", tag.name);
     using (var context = new Context())
     {
         foreach (var at in context.ArticleTags.Where(x => x.Article.Id == articleId && x.Tag.Name == tag.name))
         {
             context.ArticleTags.Remove(at);
         }
         context.SaveChanges();
         if (context.ArticleTags.Where(x => x.Tag.Name == tag.name).Count() == 0)
         {
             context.Tags.Where(x => x.Name == tag.name).ToList().ForEach(y => context.Tags.Remove(y));
             r = string.Format("removed tag '{0}' from article and tag '{0}'", tag.name);
         }
         context.SaveChanges();
     }
     return Json(this.GetTagsFromDb(), JsonRequestBehavior.AllowGet); //Content(r);
 }