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));
        }
Пример #3
0
 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);
        }
Пример #6
0
        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));
            }
        }
Пример #7
0
        /*
         * 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());
        }
Пример #9
0
        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);
        }
Пример #10
0
 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);
 }
Пример #12
0
        public ActionResult <Product> Put([FromBody] ProductUpdateInputModel model)
        {
            var product = _productRepository.Update(model);

            if (product == null)
            {
                return(NoContent());
            }

            return(product);
        }
Пример #13
0
        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);
        }
Пример #14
0
        public Product MapProductUpdateInputModel(ProductUpdateInputModel productUpdateInputModel)
        {
            Product product = new Product()
            {
                Id    = productUpdateInputModel.Id,
                Name  = productUpdateInputModel.Name,
                Price = productUpdateInputModel.Price
            };

            return(product);
        }
Пример #15
0
        public void Put(ProductUpdateInputModel model)
        {
            var product = new ProductSummary
            {
                Id    = model.Id,
                Name  = model.Name,
                Price = model.Price
            };

            _productDao.UpdateProduct(product);
        }
Пример #16
0
 public async Task <IActionResult> Put(ProductUpdateInputModel model)
 {
     try
     {
         return(Ok(await _repo.UpdateProduct(model)));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Пример #17
0
 public async Task <IActionResult> Put([FromBody] ProductUpdateInputModel productUpdate)
 {
     if (await _productService.UpsertProduct(_productBucket, productUpdate))
     {
         return(Ok());
     }
     else
     {
         return(NotFound());
     }
 }
Пример #18
0
 public IActionResult Put(ProductUpdateInputModel model)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest("Not a valid model"));
     }
     else
     {
         productService.EditProduct(model);
         return(Ok(model));
     }
 }
Пример #19
0
        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);
        }
Пример #20
0
 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();
         }
     }
 }
Пример #21
0
        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());
        }
Пример #22
0
        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));
            }
        }
Пример #24
0
        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();
            }
        }
Пример #25
0
 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());
 }
Пример #26
0
        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());
        }
Пример #27
0
        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());
        }
Пример #28
0
        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;
        }
Пример #29
0
        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());
        }
Пример #30
0
        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())));
            }
        }