public async Task HandleAsync(RateStory command) { var user = await _userRepository.GetAsync(command.UserId); if (user is null) { throw new UserNotFoundException(command.UserId); } if (user.Locked) { throw new UserLockedException(command.UserId); } var story = await _storyRepository.GetAsync(command.StoryId); if (story is null) { throw new StoryNotFoundException(command.StoryId); } await _storyRatingRepository.SetAsync(new StoryRating(new StoryRatingId(command.StoryId, command.UserId), command.Rate)); var totalRating = await _storyRatingRepository.GetTotalRatingAsync(story.Id); await _messageBroker.PublishAsync(new StoryRated(command.StoryId, command.UserId, command.Rate, totalRating)); }
public async Task <StoryRating> RateAsync(Story story, User user, int rate) { if (user.Locked) { throw new UserLockedException(user.Id); } var totalRating = await _storyRatingRepository.GetTotalRatingAsync(story.Id); var rating = StoryRating.Create(story.Id, user.Id, rate, totalRating); return(rating); }