public QuestionRatingBindingModel GetTestQuestionRatingBMRatingDown(Question question) { var model = new QuestionRatingBindingModel() { Rating = -1, QuestionId = question.Id, Username = "******" }; return(model); }
public ActionResult <QuestionRatingBindingModel> Post(QuestionRatingBindingModel bindingModel) { var user = userMgr.FindByNameAsync(bindingModel.Username).Result; var question = discussionsService.RateQuestion(bindingModel, user); if (question == null) { return(NotFound()); } return(bindingModel); }
//Tested public Question RateQuestion(QuestionRatingBindingModel model, CustomUser user) { if (model.Rating == 1) { if (!IsUserLikedQuestion(model.QuestionId, user.Id)) { var Question = new UserQuestionLikes(model.QuestionId, user); this.context.UsersQuestionLikes.Add(Question); if (IsUserDisLikedQuestion(model.QuestionId, user.Id)) { var dislikedQuestion = this.context.UsersQuestionDislikes .FirstOrDefault(x => x.User.Id == user.Id && x.QuestionId == model.QuestionId); this.context.UsersQuestionDislikes.Remove(dislikedQuestion); this.context.SaveChanges(); } } else { return(null); } } else { if (!IsUserDisLikedQuestion(model.QuestionId, user.Id)) { var Question = new UserQuestionDisLikes(model.QuestionId, user); this.context.UsersQuestionDislikes.Add(Question); if (IsUserLikedQuestion(model.QuestionId, user.Id)) { var likedQuestion = this.context.UsersQuestionLikes .FirstOrDefault(x => x.User.Id == user.Id && x.QuestionId == model.QuestionId); this.context.UsersQuestionLikes.Remove(likedQuestion); this.context.SaveChanges(); } } else { return(null); } } var question = this.context.Questions.FirstOrDefault(x => x.Id == model.QuestionId); question.Rating += model.Rating; this.context.SaveChanges(); return(question); }