public async Task <IActionResult> Create(CreateProductCommentInputModel input) { var parentId = input.ParentId == 0 ? (int?)null : input.ParentId; if (parentId.HasValue) { if (!await this.productCommentsService.IsInProductId(parentId.Value, input.ProductId)) { return(this.BadRequest()); } } var userId = this.userManager.GetUserId(this.User); try { await this.productCommentsService.CreateAsync(input.ProductId, userId, input.Content, parentId); } catch (ArgumentException aex) { return(this.BadRequest(aex.Message)); } return(this.Redirect(this.Url.Action("Details", "Products", new { id = input.ProductId }) + "#nav-comments")); }
public async Task TestAddingProductComment() { this.SeedDatabase(); var productComment = new CreateProductCommentInputModel { ProductId = this.firstProduct.Id, Content = "I like this product.", }; await this.productCommentsService.CreateAsync(productComment.ProductId, this.user.Id, productComment.Content); var count = await this.productCommentsRepository.All().CountAsync(); Assert.Equal(1, count); }
public async Task CheckSettingOfProductCommentProperties() { this.SeedDatabase(); var model = new CreateProductCommentInputModel { ProductId = this.firstProduct.Id, Content = "What's your opinion for the product?", }; await this.productCommentsService.CreateAsync(model.ProductId, this.user.Id, model.Content); var productComment = await this.productCommentsRepository.All().FirstOrDefaultAsync(); Assert.Equal(model.ProductId, productComment.ProductId); Assert.Equal("What's your opinion for the product?", productComment.Content); }
public async Task CheckIfAddingProductCommentThrowsArgumentException() { this.SeedDatabase(); await this.SeedProductComments(); var productComment = new CreateProductCommentInputModel { ProductId = this.firstProduct.Id, Content = this.firstProductComment.Content, }; var exception = await Assert .ThrowsAsync <ArgumentException>(async() => await this.productCommentsService .CreateAsync(productComment.ProductId, this.user.Id, productComment.Content)); Assert.Equal( string.Format( ExceptionMessages.ProductCommentAlreadyExists, productComment.ProductId, productComment.Content), exception.Message); }