private void SeedIfPlus(BlogContext context) { var ifplus = new IsPostUpvd { Post_Id = 2, User_Id = 3, IsPostUpvoted = false }; context.Set <IsPostUpvd>().AddOrUpdate(ifplus); context.SaveChanges(); }
public void UpdateIsVotedState(IsPostUpvd isUpvoted) { _db.IsPostUpvd.Attach(isUpvoted); _db.Entry(isUpvoted).Property("IsPostUpvoted").IsModified = true; }
public void Add(IsPostUpvd isUpvoted) { _db.IsPostUpvd.Add(isUpvoted); }
public ActionResult AddPlus(Post post) { var currentlyLoggedUserID = _user.GetIDOfCurrentlyLoggedUser().Value; var postToUpvote = _post.GetPostByID(post.Post_Id); bool checkPostState = true; var postIsUpvoted = _post.GetUpvPost(postToUpvote.Post_Id, currentlyLoggedUserID); //prevent user from double voting if (postToUpvote != null) { if (postIsUpvoted != null) { checkPostState = postIsUpvoted.IsPostUpvoted; } else { var postIsUpvtd = new IsPostUpvd() { User_Id = currentlyLoggedUserID, Post_Id = postToUpvote.Post_Id, IsPostUpvoted = false }; _post.Add(postIsUpvtd); _post.SaveChanges(); checkPostState = postIsUpvtd.IsPostUpvoted; } } if (postToUpvote != null && User.Identity.Name.ToLower() != postToUpvote.User.NickName.ToLower() && !checkPostState) { postToUpvote.Votes = postToUpvote.Votes + 1; postIsUpvoted = _post.GetUpvPost(postToUpvote.Post_Id, currentlyLoggedUserID); postIsUpvoted.IsPostUpvoted = true; _post.UpdateOnlyVotes(postToUpvote); _post.UpdateIsVotedState(postIsUpvoted); _post.SaveChanges(); var result = new { result = true, votes = postToUpvote.Votes }; return(Json(result, JsonRequestBehavior.AllowGet)); } else if (postToUpvote != null && User.Identity.Name.ToLower() != postToUpvote.User.NickName.ToLower() && checkPostState) { postToUpvote.Votes = postToUpvote.Votes - 1; postIsUpvoted = _post.GetUpvPost(postToUpvote.Post_Id, currentlyLoggedUserID); postIsUpvoted.IsPostUpvoted = false; _post.UpdateOnlyVotes(postToUpvote); _post.UpdateIsVotedState(postIsUpvoted); _post.SaveChanges(); var result = new { result = false, votes = postToUpvote.Votes }; return(Json(result, JsonRequestBehavior.AllowGet)); } else { var result = new { result = false, votes = postToUpvote.Votes }; return(Json(result, JsonRequestBehavior.AllowGet)); } }