public ActionResult <CommentReadDto> GetCommentById(int id) { var comment = _repository.GetCommentById(id); if (comment != null) { return(Ok(_mapper.Map <CommentReadDto>(comment))); } return(NotFound()); }
public async Task <IActionResult> DeleteComment(int id) { var comment = await _repo.GetCommentById(id); if (comment == null) { return(NotFound()); } if (int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value) != comment.UserId) { return(StatusCode(403)); } _repo.Delete(comment); await _repo.SaveAll(); return(NoContent()); }
public ActionResult <PostReadDto> GetCategoryByPostId(int id) { var postItem = _repository.GetCommentById(id); if (postItem != null) { return(Ok(_mapper.Map <IEnumerable <CommentReadDto> >(postItem))); } return(NotFound()); }
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)); } }