public JokeRatingBindingModel GetTestJokeRatedDownBM(int id, string username) { var bm = new JokeRatingBindingModel() { JokeId = id, Rating = -1, Username = username }; return(bm); }
public ActionResult <JokeRatingBindingModel> Post(JokeRatingBindingModel bindingModel) { var user = userMgr.FindByNameAsync(bindingModel.Username).Result; var joke = jokesService.RateJoke(bindingModel, user); if (joke == null) { return(NotFound()); } return(bindingModel); }
//Tested public Joke RateJoke(JokeRatingBindingModel ratingModel, CustomUser user) { var joke = this.context.Jokes.FirstOrDefault(x => x.Id == ratingModel.JokeId); if (ratingModel.Rating == 1) { if (IsUserLikedJoke(ratingModel.JokeId, user.Id)) { return(null); } if (IsUserDisLikedJoke(ratingModel.JokeId, user.Id)) { joke.Dislikes++; var dislikedJoke = this.context.UsersJokeDislikes.FirstOrDefault(x => x.User.UserName == ratingModel.Username && x.JokeId == ratingModel.JokeId); this.context.UsersJokeDislikes.Remove(dislikedJoke); this.context.SaveChanges(); } joke.Likes++; var jokeToRate = new UserJokeLikes(ratingModel.JokeId, user); this.context.UsersJokeLikes.Add(jokeToRate); } else { if (IsUserDisLikedJoke(ratingModel.JokeId, user.Id)) { return(null); } if (IsUserLikedJoke(ratingModel.JokeId, user.Id)) { joke.Likes--; var likedJoke = this.context.UsersJokeLikes.FirstOrDefault(x => x.User.UserName == ratingModel.Username && x.JokeId == ratingModel.JokeId); this.context.UsersJokeLikes.Remove(likedJoke); this.context.SaveChanges(); } joke.Dislikes--; var jokeToRate = new UserJokeDislikes(ratingModel.JokeId, user); this.context.UsersJokeDislikes.Add(jokeToRate); } this.context.SaveChanges(); return(joke); }