Beispiel #1
0
        public async Task <IActionResult> Put(int id, [FromBody] ProductViewModel productViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var product = _mapper.Map <Product>(productViewModel);

                    var validationResult = await _productHandler.CanUpdate(id, product);

                    if (validationResult == null)
                    {
                        product.Id = id;
                        var result = await _productService.Update(product);

                        if (result.ValidationResults.Count == 0)
                        {
                            return(Ok("Product modified successfully!"));
                        }

                        ModelState.AddModelErrors(result.ValidationResults);
                    }

                    ModelState.AddModelError(validationResult);
                }
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            return(BadRequest(ModelState.GetErrors()));
        }