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 Add_Rating_Should_Add_Rating_To_A_Story() { //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 usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >(); await usermanager.CreateAsync(user); this.Context.FictionStories.Add(story); this.Context.SaveChanges(); //act var storyService = GetService(); storyService.AddRating(story.Id, 10, user.UserName); //assert var storyRating = new FanFictionRating { FanFictionId = 1, RatingId = 1 }; var result = this.Context.FictionRatings.FirstOrDefault(); result.Should().NotBeNull().And. Subject.Should() .BeOfType <FanFictionRating>().And.Should() .BeEquivalentTo(storyRating, opt => opt.ExcludingMissingMembers()); }