private void SeedIfPlusWpis(BlogContext context) { var ifplus = new IsCommUpvoted { Comment_Id = context.Comments.FirstOrDefault().Comment_Id, User_Id = context.Users.FirstOrDefault().User_Id, IsCommentUpvoted = false }; context.Set <IsCommUpvoted>().AddOrUpdate(ifplus); context.SaveChanges(); }
public void Add(IsCommUpvoted voteComm) { _db.IsCommUpvoted.Add(voteComm); }
public ActionResult AddPlus(Comment comment) { var currentlyLoggedUserID = _user.GetIDOfCurrentlyLoggedUser().Value; var commToVote = _comm.GetCommentById(comment.Comment_Id); bool checkPostState = true; var commToVoteItem = _comm.GetPlusComment(commToVote.Comment_Id, currentlyLoggedUserID); // prevent user from double voting if (commToVote != null) { if (commToVoteItem != null) { checkPostState = commToVoteItem.IsCommentUpvoted; } else { var commVoteEntity = new IsCommUpvoted() { User_Id = currentlyLoggedUserID, Comment_Id = commToVote.Comment_Id, IsCommentUpvoted = false }; _comm.Add(commVoteEntity); _comm.SaveChanges(); checkPostState = commVoteEntity.IsCommentUpvoted; } } if (commToVote != null && User.Identity.Name.ToLower() != commToVote.User.NickName.ToLower() && !checkPostState) { commToVote.Votes = commToVote.Votes + 1; commToVoteItem = _comm.GetPlusComment(commToVote.Comment_Id, currentlyLoggedUserID); commToVoteItem.IsCommentUpvoted = true; _comm.UpdateOnlyVotes(commToVote); _comm.UpdateIfCommState(commToVoteItem); _comm.SaveChanges(); var result = new { result = true, votes = commToVote.Votes }; return(Json(result, JsonRequestBehavior.AllowGet));; } else if (commToVote != null && User.Identity.Name.ToLower() != commToVote.User.NickName.ToLower() && checkPostState) { commToVote.Votes = commToVote.Votes - 1; commToVoteItem = _comm.GetPlusComment(commToVote.Comment_Id, currentlyLoggedUserID); commToVoteItem.IsCommentUpvoted = false; _comm.UpdateOnlyVotes(commToVote); _comm.UpdateIfCommState(commToVoteItem); _comm.SaveChanges(); var result = new { result = false, votes = commToVote.Votes }; return(Json(result, JsonRequestBehavior.AllowGet)); } else { var result = new { result = false, votes = commToVote.Votes }; return(Json(result, JsonRequestBehavior.AllowGet)); } }
public void UpdateIfCommState(IsCommUpvoted isUpvoted) { _db.IsCommUpvoted.Attach(isUpvoted); _db.Entry(isUpvoted).Property("IsCommentUpvoted").IsModified = true; }