Esempio n. 1
0
        public IActionResult UpdateCategory(int categoryId, [FromBody] ProductCategoryForUpdateDto payload)
        {
            if (payload.Label == payload.Description)
            {
                ModelState.AddModelError("Description", "Label category must be different from description.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var categoryEntity = _categoryService.GetCategoryById(categoryId);

            if (categoryEntity == null)
            {
                return(NotFound());
            }

            _mapper.Map(payload, categoryEntity);

            _categoryService.UpdateCategory(categoryEntity);

            var updatedCategoryToReturn = _mapper
                                          .Map <ProductCategoryDto>(categoryEntity);

            return(CreatedAtRoute(
                       "GetCategory",
                       new { id = categoryId },
                       updatedCategoryToReturn));
        }
        public IActionResult UpdateProductCategory(int id, [FromBody] ProductCategoryForUpdateDto prodCat)
        {
            try
            {
                if (prodCat == null)
                {
                    _logger.LogError("prodCat object sent from client is null.");
                    return(BadRequest("prodCat object is null"));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid prodCat object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var prodCatEntity = _repository.Product_Category.GetProductCategoryById(id);
                if (prodCatEntity == null)
                {
                    _logger.LogError($"prodCat with id: {id}, hasn't been found in db.");
                    return(NotFound());
                }

                _mapper.Map(prodCat, prodCatEntity);

                _repository.Product_Category.UpdateProdCategory(prodCatEntity);
                _repository.Save();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside UpdateProductType action: {ex.InnerException.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Esempio n. 3
0
        public async Task <ActionResult> UpdateProductCategory(int id, ProductCategoryForUpdateDto productCategoryForUpdate)
        {
            var productCategory = await _unitOfWork.Repository <ProductCategory>().GetByIdAsync(id);

            if (productCategory == null)
            {
                return(NotFound(new ApiResponse(404)));
            }

            _mapper.Map(productCategoryForUpdate, productCategory);

            _unitOfWork.Repository <ProductCategory>().Update(productCategory);
            await _unitOfWork.Complete();

            return(NoContent());
        }