public Task SetAsync(StoryRating rating) => _database.GetCollection <StoryRatingDocument>("ratings") .ReplaceOneAsync(x => x.StoryId == rating.Id.StoryId && x.UserId == rating.Id.UserId, new StoryRatingDocument(rating), new ReplaceOptions { IsUpsert = true });
public void AddRating(int storyId, double rate, string username) { var user = this.UserManager.FindByNameAsync(username).GetAwaiter().GetResult(); var story = this.Context.FictionStories.Find(storyId); bool rated = AlreadyRated(story.Id, user.UserName); if (rated) { throw new InvalidOperationException(GlobalConstants.AlreadyRated); } var rating = new StoryRating { Rating = rate, UserId = user.Id }; this.Context.StoryRatings.Add(rating); var storyRating = new FanFictionRating { FanFictionId = storyId, RatingId = rating.Id }; this.Context.FictionRatings.Add(storyRating); story.Ratings.Add(storyRating); this.Context.Update(story); this.Context.SaveChanges(); }
public void Add_Rating_Should_Throw_Exception_If_AlreadyRated_Is_True() { //arrange var story = new FanFictionStory { Id = 1, Author = null, Summary = "some summary", Title = "Story To test" }; var user = new FanFictionUser { Id = "UserId", Nickname = "TestStory", UserName = "******", }; var rating = new StoryRating { Rating = 8, UserId = user.Id, Id = 1, FanFictionUser = user }; var storyRating = new FanFictionRating { FanFictionId = story.Id, RatingId = rating.Id, FanFictionStory = story, StoryRating = rating }; var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >(); usermanager.CreateAsync(user).GetAwaiter(); this.Context.FictionStories.Add(story); this.Context.StoryRatings.Add(rating); this.Context.FictionRatings.Add(storyRating); this.Context.SaveChanges(); //act var storyService = GetService(); Action act = () => storyService.AddRating(story.Id, 1, user.UserName); //assert act.Should().Throw <InvalidOperationException>().WithMessage(GlobalConstants.AlreadyRated); }
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); }
public StoryRatingChanged(StoryRating rating, int totalRate) { Rating = rating; TotalRate = totalRate; }
public StoryRatingDocument(StoryRating rating) { StoryId = rating.Id.StoryId; UserId = rating.Id.UserId; Rate = rating.Rate; }