public ActionResult AddFeaturedArticle(int?id) { // Check ID and blog if (id == null) { return(Json(new { success = false, msg = "This blog doesn't exist." })); } Blog blog = db.Blogs.Find(id); if (blog == null) { return(Json(new { success = false, msg = "This blog doesn't exist." })); } // Check max feature count var userID = User.Identity.GetUserId(); int userFeaturedBlogsCount = db.FeaturedBlogs.Where(b => b.UserID == userID).Count(); if (userFeaturedBlogsCount >= MAX_USER_FEATURE_COUNT) { return(Json(new { success = false, msg = "You cannot feature more then " + MAX_USER_FEATURE_COUNT + " posts." })); } // Make sure blog isnt already featured FeaturedBlog checkFBlog = db.FeaturedBlogs.SingleOrDefault(b => b.BlogID == id); if (checkFBlog != null) { return(Json(new { success = false, msg = "This post is already featured!" })); } // Add featured blog to DB FeaturedBlog fBlog = new FeaturedBlog { FeaturedBlogID = Guid.NewGuid(), UserID = userID, BlogID = blog.BlogID, FeatureDate = DateTime.Now }; try { db.FeaturedBlogs.Add(fBlog); db.SaveChanges(); return(Json(new { success = true, msg = "Post successfully featured!" })); } catch (Exception) { return(Json(new { success = false, msg = "An unknown error occured." })); } }
public ActionResult RemoveFeaturedArticle(int id) { var userID = User.Identity.GetUserId(); FeaturedBlog fBlog = db.FeaturedBlogs.SingleOrDefault(b => b.BlogID == id); if (fBlog.UserID == userID && fBlog != null) { db.FeaturedBlogs.Remove(fBlog); db.SaveChanges(); return(Json(new { success = true, blogId = id })); } return(Json(new { success = false })); }