public async Task <BarReview> AddBarReview(BarReview review, int barId, string userId) { var bar = await this.context.Bars.Where(x => x.Id == barId).SingleOrDefaultAsync(); bar.Rating = await this.barService.CalculateAverageRating(bar.ToContract(), review.Rating); var reviewEntity = review.ToEntity(); reviewEntity.BarEntityId = barId; reviewEntity.UserEntityId = userId; await this.context.BarReviews.AddAsync(reviewEntity); this.context.Bars.Update(bar); await this.context.SaveChangesAsync(); return(reviewEntity.ToContract()); }
public async Task AddReviewToTheCorrectBar() { var testBarId = 2; var testUserId = "TestUserId"; var testBarRating = 5; var testBarReview = new BarReview() { User = new User() { Id = testUserId, Name = "Pesho", Email = "*****@*****.**" }, Rating = testBarRating, }; var options = TestUtilities.GetOptions(nameof(AddReviewToTheCorrectBar)); using (var arrangeContext = new AppDBContext(options)) { await arrangeContext.BarReviews.AddAsync(testBarReview.ToEntity()); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new AppDBContext(options)) { var cocktailService = new CocktailService(assertContext); var barService = new BarService(assertContext); var sut = new UserService(assertContext, barService, cocktailService); var testReview = await sut.AddBarReview(testBarReview, testBarId, testUserId); Assert.AreEqual(testBarRating, testReview.Rating); } }