public void UpdateProductWithOverPriceSuccessTest() { var repository = new Mock <IProductRepository>(); var errorCollector = new ErrorCollector(); var exportService = new Mock <IExportService>().Object; repository.Setup(r => r.GetProductById(It.IsAny <int>())).Returns(new Product { Id = 1, Code = "T1", Name = "Test 1", Price = 10 }); var service = new ProductService(repository.Object, errorCollector, exportService); var inputModel = new UpdateProductInputModel { Id = 1, Code = "T1", Name = "Test 1", Price = Constants.ProductMaxPrice + 1, ConfirmPrice = true }; var outputModel = service.UpdateProduct(inputModel); Assert.IsNotNull(outputModel); Assert.IsFalse(errorCollector.Errors.Any()); Assert.IsTrue(outputModel.Success); }
public GenericResponse <IUpdateProductOutputModel> Update([FromBody] UpdateProductInputModel inputModel) { return(new GenericResponse <IUpdateProductOutputModel> { Code = 200, Result = _service.UpdateProduct(inputModel) }); }
public void UpdateProductWithInvalidInputModelTest() { var repository = new Mock <IProductRepository>().Object; var errorCollector = new ErrorCollector(); var exportService = new Mock <IExportService>().Object; var service = new ProductService(repository, errorCollector, exportService); var inputModel = new UpdateProductInputModel { }; var outputModel = service.UpdateProduct(inputModel); Assert.IsNull(outputModel); Assert.IsTrue(errorCollector.Errors.Any()); }
public async Task <IActionResult> UpdateProduct([FromBody] UpdateProductInputModel inputModel) { var product = await _catalogContext.Products .SingleOrDefaultAsync(i => i.Id == inputModel.Id); if (product == null) { return(NotFound(new { Message = $"Product {inputModel.Id} is not found." })); } // Update current product _mapper.Map(inputModel, product); _catalogContext.Products.Update(product); await _catalogContext.SaveChangesAsync(); return(CreatedAtAction(nameof(GetById), new { id = product.Id }, null)); }
public async Task <IActionResult> Update(UpdateProductInputModel input) { if (!this.ModelState.IsValid) { input.Categories = await this.categoriesService.GetAllAsSelectListItemAsync(); input.Brands = await this.brandsService.GetAllAsSelectListItemAsync(); input.Picture = await this.productsService.GetPictureUrlAsync(input.Id); return(this.View(input)); } await this.productsService.UpdateAsync(input.Id, input.Name, input.Description, input.Price, input.NewPicture, input.BrandId, input.CategoryId); this.TempData["InfoMessage"] = GlobalMessages.SuccessUpdateMessage; return(this.RedirectToAction(nameof(this.GetAll))); }
public async Task SetUp() { _fixture = new Fixture(); _products = _fixture.CreateMany <Product>(10).ToList(); var options = new DbContextOptionsBuilder <CatalogContext>() .UseInMemoryDatabase(databaseName: $"Catalog_{Guid.NewGuid()}") .Options; _dbContext = new CatalogContext(options); await _dbContext.Products.AddRangeAsync(_products); await _dbContext.SaveChangesAsync(); _model = _fixture.Create <UpdateProductInputModel>(); _validator = new UpdateProductModelValidator(_dbContext); }
public void UpdateProductWithPriceOverMaxLimitTest() { var repository = new Mock <IProductRepository>().Object; var errorCollector = new ErrorCollector(); var exportService = new Mock <IExportService>().Object; var service = new ProductService(repository, errorCollector, exportService); var inputModel = new UpdateProductInputModel { Id = 1, Code = "T1", Name = "Test 1", Price = Constants.ProductMaxPrice + 1 }; var outputModel = service.UpdateProduct(inputModel); Assert.IsNull(outputModel); Assert.IsTrue(errorCollector.Errors.Any()); }
public void UpdateProductButProductDoesNotExistTest() { var repository = new Mock <IProductRepository>().Object; var errorCollector = new ErrorCollector(); var exportService = new Mock <IExportService>().Object; var service = new ProductService(repository, errorCollector, exportService); var inputModel = new UpdateProductInputModel { Id = 1, Code = "T1", Name = "Test 1", Price = 10 }; var outputModel = service.UpdateProduct(inputModel); Assert.IsNull(outputModel); Assert.IsTrue(errorCollector.Errors.Any()); }
public void UpdateProductWithAlreadyExistingCodeTest() { var repository = new Mock <IProductRepository>(); var errorCollector = new ErrorCollector(); var exportService = new Mock <IExportService>().Object; repository.Setup(r => r.GetProductById(It.IsAny <int>())).Returns(new Product { Id = 1, Code = "T1", Name = "Test 1", Price = 10 }); repository.Setup(r => r.GetProducts(It.IsAny <string>())).Returns(new[] { new Product { Id = 2, Code = "T1", Name = "Test One", Price = 15 } }); var service = new ProductService(repository.Object, errorCollector, exportService); var inputModel = new UpdateProductInputModel { Id = 1, Code = "T1", Name = "Test 1", Price = 10 }; var outputModel = service.UpdateProduct(inputModel); Assert.IsNull(outputModel); Assert.IsTrue(errorCollector.Errors.Any()); }
public GenericResponse <UpdateProductOutputModel> UpdateProduct(UpdateProductInputModel input) { return(CallWebClient <UpdateProductInputModel, UpdateProductOutputModel>(CreateServiceCallParameter(input, (request) => _webClient.Put(request).Result, "Products/", ServiceUrl))); }