public async Task <IActionResult> UpdateProduct(ProductUpdateInputModel inputModel) { var productId = await _productService.UpdateProductAsync(inputModel.Id, inputModel.Name, inputModel.Description, inputModel.Barcode, inputModel.Price, inputModel.Stock, inputModel.Images.Select(x => x.ImageUrl).ToList()); return(RedirectToAction("UpdateProduct", productId)); }
public async Task UpdateProductWithMnusPrice() { Product[] dbProducts; var controller = GetController(out dbProducts); Assert.Greater(dbProducts.Length, 0); Random random = new Random(); int randomIndex = random.Next(0, dbProducts.Length); var sourceProduct = dbProducts[randomIndex]; ProductUpdateInputModel newProductUpdateModel = new ProductUpdateInputModel() { Id = sourceProduct.Id, Name = "minus price", Price = -8 }; await controller.Put(newProductUpdateModel); Product updatedProduct = new Product(newProductUpdateModel); Product responseProduct = await controller.Get((Guid)updatedProduct.Id); Assert.IsFalse(AreProductsSame(responseProduct, updatedProduct)); }
public void ValidateProductUpdateInput(ProductUpdateInputModel productUpdateInput) { if (productUpdateInput == null) { throw new InputNullException(Resource.Exception.ExceptionMessage.InputIsNull); } }
public bool Put(Guid id, string name, string price) { decimal price_decimal = decimal.Parse(price.Replace('.', ',')); ProductUpdateInputModel product_update_input = new ProductUpdateInputModel(id, name, price_decimal); return(product_actions.UpdateProduct(product_update_input)); }
public async Task UpdateProductWithBadId() { Product[] dbProducts; var controller = GetController(out dbProducts); Assert.Greater(dbProducts.Length, 0); Random random = new Random(); int randomIndex = random.Next(0, dbProducts.Length); var sourceProduct = dbProducts[randomIndex]; ProductUpdateInputModel newProductUpdateModel = new ProductUpdateInputModel() { Id = Guid.NewGuid(), Name = "new id", Price = 9 }; await controller.Put(newProductUpdateModel); Product updatedProduct = new Product(newProductUpdateModel); Product responseProduct = await controller.Get((Guid)updatedProduct.Id); Assert.IsNull(responseProduct); }
public async Task <IActionResult> Put([FromBody] ProductUpdateInputModel model) { try { var data = await _context.Products.FindAsync(model.Id); if (data == null) { throw new Exception("Product with this ID does not exist."); } else if (!ModelState.IsValid) { throw new Exception("Name is required and has to be between 1 and 100 characters. Price is required and needs to be positive."); } else if (model.Price <= 0) { throw new Exception("Price value needs to be positive."); } else { data.Name = model.Name; data.Price = model.Price; _context.Products.Update(data); await _context.SaveChangesAsync(); return(Ok(data)); } } catch (Exception exception) { return(StatusCode(500, "Error: " + exception.Message)); } }
/* * Overwrite the Product with the specified Id. */ public async Task <bool> Update(ProductUpdateInputModel model) { Product product = await GetById(model.Id); if (product == null) { return(false); } product = ProductUpdateInputModel.Update(product, model); _context.Set <Product>().Update(product); try { _ = await SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DoesProductExist(product.Id)) { return(false); } else { throw; } } return(true); }
public ActionResult EditProduct(ProductUpdateInputModel updateInputModel) { var p = _productsController.Update(updateInputModel); if (p == null) { return(BadRequest("bad request")); } return(AllProducts()); }
public async Task <bool> UpdateProduct(ProductUpdateInputModel model) { Product product = await _context.Products.FirstOrDefaultAsync(p => p.Id == model.Id); product.Name = model.Name; product.Price = (decimal)model.Price; await _context.SaveChangesAsync(); return(true); }
public void Put([FromBody] ProductUpdateInputModel model) { if (ModelState.IsValid) { _productService.UpdateProduct(new ProductModel() { Id = model.Id, Name = model.Name, Price = model.Price }); } }
// Updates product with the provided ProductUpdateInputModel public Product Update(ProductUpdateInputModel model) { if (model.IsValid()) { string sql = $@"UPDATE {productTableName} SET Name = '{model.Name}', Price = {model.Price} WHERE Id = '{model.Id}';"; _databaseAccess.Execute(sql); return(Get(model.Id)); } return(null); }
public ActionResult <Product> Put([FromBody] ProductUpdateInputModel model) { var product = _productRepository.Update(model); if (product == null) { return(NoContent()); } return(product); }
public static bool ValidateData(ProductUpdateInputModel product, MainDbContext context, out List <string> errorList) { errorList = new List <string>(); bool isIdOk = CheckId(product.Id, ref errorList); bool idExistInDb = isIdOk && context.Products.FirstOrDefault((x) => x.Id == product.Id) != null; bool isNameOk = CheckName(product.Name, ref errorList); bool isPriceOk = CheckPrice(product.Price, ref errorList); return(isIdOk && idExistInDb && isNameOk && isPriceOk); }
public Product MapProductUpdateInputModel(ProductUpdateInputModel productUpdateInputModel) { Product product = new Product() { Id = productUpdateInputModel.Id, Name = productUpdateInputModel.Name, Price = productUpdateInputModel.Price }; return(product); }
public void Put(ProductUpdateInputModel model) { var product = new ProductSummary { Id = model.Id, Name = model.Name, Price = model.Price }; _productDao.UpdateProduct(product); }
public async Task <IActionResult> Put(ProductUpdateInputModel model) { try { return(Ok(await _repo.UpdateProduct(model))); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> Put([FromBody] ProductUpdateInputModel productUpdate) { if (await _productService.UpsertProduct(_productBucket, productUpdate)) { return(Ok()); } else { return(NotFound()); } }
public IActionResult Put(ProductUpdateInputModel model) { if (!ModelState.IsValid) { return(BadRequest("Not a valid model")); } else { productService.EditProduct(model); return(Ok(model)); } }
public bool UpdateProduct(ProductUpdateInputModel updateProduct) { if (_context.Products.Find(updateProduct.Id) == null) { return(false); } Product product = _productMapper.MapProductUpdateInputModel(updateProduct); _context.Entry(product).State = EntityState.Modified; _context.SaveChanges(); return(true); }
public void Put(ProductUpdateInputModel model) { if (ModelState.IsValid) { var product = _dbContext.Products.SingleOrDefault(p => p.Id == model.Id); if (product != null) { product.Name = model.Name; product.Price = model.Price; _dbContext.Update(product); _dbContext.SaveChanges(); } } }
public async Task <ActionResult <ProductUpdateInputModel> > Put(ProductUpdateInputModel model) { if (model.Id == Guid.Empty) { return(BadRequest()); } bool result = await _repository.Update(model); if (!result) { return(NotFound()); } return(NoContent()); }
public void Update(Guid id, ProductUpdateInputModel productUpdateInputModel) { _logger.LogInformation($"Update {nameof(Product)} entity with {id}: start"); Product product = _repository.GetById(id); //overwrite id - id from path more important than passed in model productUpdateInputModel.Id = id; _mapper.Map(productUpdateInputModel, product); _repository.Update(product); if (!_repository.Save()) { throw new Exception($"Updating {nameof(Product)} {productUpdateInputModel.Name} failed on server"); } _logger.LogInformation($"Update {nameof(Product)} entity with {id}: sucess"); }
public IActionResult Put([FromBody] ProductUpdateInputModel product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var productToEdit = _productFactory.GetProduct(product); _repo.EditProduct(productToEdit); return(Ok()); } catch (EntityNotFoundException e) { return(BadRequest(e.Message)); } }
public void EditProduct(ProductUpdateInputModel model) { Product product = new Product(); if (model.Id != null) { product = GetProduct(model.Id); } if (product != null) { product.Name = model.Name; product.Price = model.Price; db.Entry(product).State = Microsoft.EntityFrameworkCore.EntityState.Modified; db.SaveChanges(); } }
public async Task <IActionResult> Put([FromBody] ProductUpdateInputModel model) { if (model.Id != Guid.Empty && ModelState.IsValid) { _context.Products.Update(new Product { Id = model.Id, Name = model.Name, Price = model.Price }); await _context.SaveChangesAsync(); } else if (model.Id == Guid.Empty) { throw new Exception("Id cannot be all zeros, must be provided in Body Request"); } else if (!ModelState.IsValid) { throw new Exception("Provided informations doesn't meet requirements (name and price cannot be empty, name cannot be longer than 100 characters, price must be higher than 0)"); } return(NoContent()); }
public async Task <IActionResult> Put([FromBody] ProductUpdateInputModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var product = await _repository.GetProduct((Guid)model.Id); if (product == null) { return(NotFound()); } var result = _mapper.Map <ProductUpdateInputModel, Product>(model, product); await _unitOfWork.CompleteAsync(); return(Ok()); }
public async Task <IActionResult> PutProductItemAsync([FromBody] ProductUpdateInputModel model) { logger?.LogDebug("'{0}' has been invoked with model {1}", nameof(PostProductItemAsync), model); var entity = await context.GetProductItemsAsync(new ProductItem(model.ItemId)); if (entity == null) { return(NotFound()); } entity.ItemName = model.ItemName; entity.ItemPrice = model.ItemPrice; context.Update(entity); await context.SaveChangesAsync(); return(NoContent()); }
public void PutTest() { var item = new ProductUpdateInputModel() { Id = _testProductGuid.HasValue ? _testProductGuid.Value : new Guid(), Name = "Test" + " Edited Name", Price = Convert.ToDecimal(new Random(DateTime.Now.Millisecond).NextDouble().ToString("n2")) }; var content = JsonConvert.SerializeObject(item); var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); var response = _client.PutAsync("/api/Product", stringContent).Result; response.EnsureSuccessStatusCode(); var responseString = response.Content.ReadAsStringAsync().Result; _testProductGuid = null; }
public IActionResult Put([FromBody] ProductUpdateInputModel model) { if (model == null) { return(BadRequest("Model cannot be a null object")); } try { _repository.Update(new Product(model.Id, model.Name, model.Price)); } catch (ArgumentException exc) { return(NotFound(exc.Message)); } catch (Exception exc) { return(BadRequest(exc.Message)); } return(Ok()); }
public IActionResult UpdateProduct(Guid id, [FromBody] ProductUpdateInputModel productDTO) { try { if (productDTO == null) { return(BadRequest(Resources.Messages.ProductIsNull)); } productDTO.Id = id; var product = _mapper.Map <ProductUpdateInputModel, Product>(productDTO); if (!ModelState.IsValid) { return(BadRequest(ModelState.Select(x => x.Value.Errors).Where(y => y.Count > 0).ToList())); } _repository.Product.UpdateProduct(product); return(NoContent()); } catch (Exception ex) { return(StatusCode(500, String.Concat(Resources.Messages.InternalError, ex.ToString()))); } }