public async Task <JsonResult> DownVoteItem(string itemType, string itemId, string userId) { await Task.Run(() => { DownVote downVote = null; string originalQuestionUserId = ""; if (itemType == "question") { var question = qaLogic.HandleGetQuestion(Convert.ToInt32(itemId)); if (question != null) { downVote = new DownVote { questionId = Convert.ToInt32(itemId), userId = userId }; originalQuestionUserId = question.OwnerId; } } else { var answer = qaLogic.HandleGetAnswer(Convert.ToInt32(itemId)); if (answer != null) { downVote = new DownVote { answerId = Convert.ToInt32(itemId), userId = userId }; originalQuestionUserId = answer.OwnerId; } } qaLogic.HandleSaveDownVote(downVote); qaLogic.HandleUpdateReputation(originalQuestionUserId, -5); }); return(Json("DownVote", JsonRequestBehavior.AllowGet)); }
public async Task <string> Downvote(string postId) { try { var currentuser = Feature.CurrentUser(httpContextAccessor, userRepository); var currentPost = await postRepository.GetByIdAsync(ObjectId.Parse(postId)); var builderDownVote = Builders <DownVote> .Filter; var filterExistDownvote = builderDownVote.Eq(ObjectVoteId, postId) & builderDownVote.Eq(DownVoteBy, currentuser.OId) & builderDownVote.Eq(IsDeleted, false); var existDownVote = await downVoteRepository.FindAsync(filterExistDownvote); if (existDownVote != null) { return(UserDownvoteAlready); } var builderUpVote = Builders <UpVote> .Filter; var filterExistUpVote = builderUpVote.Eq(ObjectVoteId, postId) & builderUpVote.Eq(UpVoteBy, currentuser.OId) & builderUpVote.Eq(IsDeleted, false); var existUpVote = await upVoteRepository.FindAsync(filterExistUpVote); if (existUpVote != null) { existUpVote.IsDeleted = true; await upVoteRepository.DeleteAsync(existUpVote.Id); } var downvote = new DownVote() { ObjectVoteId = postId, DownVoteBy = currentuser.OId }; await downVoteRepository.AddAsync(downvote); var notificationDetail = new Noftication() { AuthorId = currentuser.OId, OwnerId = currentPost.AuthorId, ObjectId = currentPost.OId, ObjectThumbnail = currentPost.Title }; await fcmRepository.PushNotify(currentPost.AuthorId, notificationDetail, NotificationContent.DownvotePostNotification); await userService.AddPoint(currentPost.AuthorId, currentPost.OId, PointAdded.Downvote); return(DownvoteSuccess); } catch (Exception) { return("Có lỗi xảy ra. "); } }