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

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

            var finalCategory = _mapper
                                .Map <ProductCategory>(payload);

            _categoryService.CreateCategory(finalCategory);

            var createdCategoryToReturn = _mapper
                                          .Map <ProductCategoryDto>(finalCategory);

            return(CreatedAtRoute(
                       "GetCategory",
                       new { id = finalCategory.Id },
                       createdCategoryToReturn));
        }
        public IActionResult CreateProductCategory([FromBody] ProductCategoryForCreationDto prodCategory)
        {
            try
            {
                if (prodCategory == null)
                {
                    _logger.LogError("prodCategory object sent from client is null.");
                    return(BadRequest("prodCategory object is null"));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid prodCategory object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var prodCategoryEntity = _mapper.Map <ProductCategory>(prodCategory);

                _repository.Product_Category.CreateProdCategory(prodCategoryEntity);
                _repository.Save();

                var createdProdCategory = _mapper.Map <ProductCategoryDto>(prodCategoryEntity);

                return(CreatedAtRoute("ProductCategoryById", new { id = createdProdCategory.ProductCategoryId }, createdProdCategory));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateProdCategory action: {ex.InnerException.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Esempio n. 3
0
        public async Task <ActionResult> CreateProductCategory(ProductCategoryForCreationDto productCategoryForCreation)
        {
            var productCategory = _mapper.Map <ProductCategoryForCreationDto, ProductCategory>(productCategoryForCreation);

            await _unitOfWork.Repository <ProductCategory>().AddAsync(productCategory);

            await _unitOfWork.Complete();

            var response = _mapper.Map <ProductCategory, ProductCategoryToReturnDto>(productCategory);

            return(CreatedAtRoute("GetProductCategoryById", new { id = productCategory.Id }, response));
        }