public async Task <IActionResult> Put(PostRatingDto postRatingDto) { var userName = User.Identity.Name; var avgRating = await ratingService.RatePost(postRatingDto, userName); await notificationService.RatingNotify(postRatingDto, userName); return(Ok(new { AvgRating = avgRating })); }
public async Task <bool> RatingNotify(PostRatingDto postRatingDto, string whoRated) { var post = await repository.GetById <Post>(postRatingDto.Id); var userName = post.User.UserName; if (whoRated == userName) { return(false); } string title = "Rating"; string text = $"{whoRated} Rated your post by {postRatingDto.Value}"; return(await NotifyUser(title, text, userName)); }
public async Task <double> RatePost(PostRatingDto postRatingDto, string userName) { var value = postRatingDto.Value; var postId = postRatingDto.Id; if (value > 10 || value < 1) { throw new BadRequestException("invalid value"); } var user = await userManager.FindByNameAsync(userName); var post = await repository.GetById <Post>(postId); var rating = post.Ratings.SingleOrDefault(x => x.UserId == user.Id); if (rating == null) { rating = new Rating(); rating.PostId = postId; rating.User = user; rating.Value = value; post.Ratings.Add(rating); } else { rating.Value = value; } await repository.SaveAll(); return(post.Ratings.Select(x => x.Value).Average()); }