public async Task CreateAsync_WithIncorrectData_ShouldThrowArgumentNullException() { // Arrange MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); await this.SeedRecipeAndUser(context); var reviewRepository = new EfDeletableEntityRepository <Review>(context); var reviewService = this.GetReviewService(reviewRepository); var reviewServiceModel = new ReviewServiceModel { Comment = "Comment", Rating = 5, RecipeId = null, ReviewerId = null, }; // Act // Assert await Assert.ThrowsAsync <ArgumentNullException>(async() => { await reviewService.CreateAsync(reviewServiceModel); }); }
public void Create_WithNonExistentUser_ShouldReturnFalse() { string errorMessagePrefix = "ReviewsService Create() method does not work properly."; var context = UniShopDbContextInMemoryFactory.InitializeContext(); this.SeedData(context); this.reviewsService = new ReviewsService(context, new UniShopUsersService(context)); string userId = context.Users.First().Id + new Guid(); int productId = context.Products.Last().Id + 1; ReviewServiceModel testReview = new ReviewServiceModel { Raiting = 2, ProductId = productId, Comment = "Test Comment Test" }; int expectedCount = context.Reviews.Count(); bool actualResult = this.reviewsService.Create(testReview, userId); int actualCount = context.Reviews.Count(); Assert.False(actualResult, errorMessagePrefix); Assert.Equal(expectedCount, actualCount); }
public async Task CreateAsync_WithCorrectData_ShouldReturnCorrectResult() { var errorMessagePrefix = "ReviewService CreateAsync() method does not work properly."; // Arrange MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); await this.SeedRecipeAndUser(context); var reviewRepository = new EfDeletableEntityRepository <Review>(context); var reviewService = this.GetReviewService(reviewRepository); var reviewServiceModel = new ReviewServiceModel { Comment = "Comment", Rating = 5, Recipe = context.Recipes.First().To <RecipeServiceModel>(), ReviewerId = context.Recipes.First().Id, }; // Act var result = await reviewService.CreateAsync(reviewServiceModel); // Assert Assert.True(result, errorMessagePrefix + " " + "Returns false."); }
public async Task CreateAsync_WithCorrectData_ShouldSuccessfullyCreate() { var errorMessagePrefix = "ReviewService CreateAsync() method does not work properly."; // Arrange MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); await this.SeedRecipeAndUser(context); var reviewRepository = new EfDeletableEntityRepository <Review>(context); var reviewService = this.GetReviewService(reviewRepository); var reviewServiceModel = new ReviewServiceModel { Comment = "Comment", Rating = 5, Recipe = context.Recipes.First().To <RecipeServiceModel>(), ReviewerId = context.Recipes.First().Id, }; // Act await reviewService.CreateAsync(reviewServiceModel); var actualResult = reviewRepository.All().First().To <ReviewServiceModel>(); var expectedResult = reviewServiceModel; // Assert Assert.True(expectedResult.Comment == actualResult.Comment, errorMessagePrefix + " " + "Comment is not returned properly."); Assert.True(expectedResult.Rating == actualResult.Rating, errorMessagePrefix + " " + "Rating is not returned properly."); Assert.True(expectedResult.Recipe.Id == actualResult.Recipe.Id, errorMessagePrefix + " " + "Recipe Id is not returned properly."); Assert.True(expectedResult.ReviewerId == actualResult.ReviewerId, errorMessagePrefix + " " + "ReviewerId is not returned properly."); }
public bool Create(ReviewServiceModel reviewServiceModel, string userId) { var product = this.context.Products.FirstOrDefault(p => p.Id == reviewServiceModel.ProductId); if (product == null) { return(false); } var user = this.uniShopUsersService.GetUserById(userId); if (user == null) { return(false); } var review = new Review { UniShopUserId = userId, ProductId = reviewServiceModel.ProductId, Comment = reviewServiceModel.Comment, Raiting = reviewServiceModel.Raiting }; this.context.Reviews.Add(review); int result = this.context.SaveChanges(); return(result > 0); }
public async Task <bool> Create(ReviewServiceModel reviewServiceModel) { Review review = AutoMapper.Mapper.Map <Review>(reviewServiceModel); ValidateReview(review); await dbContext.Reviews.AddAsync(review); var result = await dbContext.SaveChangesAsync(); return(result > 0); }
public async Task Create_WithNoComment_ShouldThrowArgumentException() { var context = ReserveTableDbContextInMemoryFactory.InitializeContext(); this.reviewService = new ReviewService(context); ReviewServiceModel review = new ReviewServiceModel { Date = DateTime.Now, Comment = string.Empty, Rate = 9, }; await Assert.ThrowsAsync <ArgumentException>(() => this.reviewService.Create(review)); }
public async Task Create_WithInvalidRate_ShouldThrowArgumentException(double rate) { var context = ReserveTableDbContextInMemoryFactory.InitializeContext(); this.reviewService = new ReviewService(context); ReviewServiceModel review = new ReviewServiceModel { Date = DateTime.Now, Comment = "Good", Rate = rate, }; await Assert.ThrowsAsync <ArgumentException>(() => this.reviewService.Create(review)); }
public async Task Create_WithCorrectData_ShouldCreateSucessfully() { string errorMessage = "ReviewService Create() method does not work properly."; var context = ReserveTableDbContextInMemoryFactory.InitializeContext(); this.reviewService = new ReviewService(context); ReviewServiceModel review = new ReviewServiceModel { Date = DateTime.Now, Comment = "Good", Rate = 9, }; bool actualResult = await this.reviewService.Create(review); Assert.True(actualResult, errorMessage); }
public async Task <IActionResult> Create(CreateReviewBindingModel model, string city, string restaurant) { var restaurantFromDbServiceModel = await restaurantService.GetRestaurantByNameAndCity(city, restaurant); var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; ReviewServiceModel reviewServiceModel = AutoMapper.Mapper.Map <ReviewServiceModel>(model); reviewServiceModel.RestaurantId = restaurantFromDbServiceModel.Id; reviewServiceModel.UserId = userId; reviewServiceModel.Date = DateTime.Now; await reviewsService.Create(reviewServiceModel); var newRestaurantAverageRating = await restaurantService.GetAverageRate(restaurantFromDbServiceModel); await restaurantService.SetNewRating(restaurantFromDbServiceModel.Id, newRestaurantAverageRating); return(this.Redirect($"/Restaurants/{city}/{restaurant}")); }
public async Task <bool> CreateAsync(ReviewServiceModel reviewServiceModel) { var review = reviewServiceModel.To <Review>(); if (review.Comment == null || review.Rating == 0 || review.ReviewerId == null || review.Recipe.Id == null) { throw new ArgumentNullException(InvalidReviewPropertyErrorMessage); } await this.userService.SetUserToReviewAsync(reviewServiceModel.ReviewerId, review); await this.recipeService.SetRecipeToReviewAsync(reviewServiceModel.Recipe.Id, review); await this.reviewRepository.AddAsync(review); var result = await this.reviewRepository.SaveChangesAsync(); return(result > 0); }
public async Task <bool> EditAsync(string id, ReviewServiceModel reviewServiceModel) { var reviewFromDb = await this.reviewRepository.GetByIdWithDeletedAsync(id); if (reviewFromDb == null) { throw new ArgumentNullException( string.Format(InvalidReviewIdErrorMessage, id)); } if (reviewServiceModel.Comment == null || reviewServiceModel.Rating == 0) { throw new ArgumentNullException(InvalidReviewPropertyErrorMessage); } reviewFromDb.Rating = reviewServiceModel.Rating; reviewFromDb.Comment = reviewServiceModel.Comment; this.reviewRepository.Update(reviewFromDb); var result = await this.reviewRepository.SaveChangesAsync(); return(result > 0); }