コード例 #1
0
        public IActionResult CreateProductFeature(int productId, [FromBody] ProductFeatureForCreationDto productFeature)
        {
            if (null == productFeature)
            {
                return(BadRequest());
            }

            //Description and name can not be the same (for some reason)
            if (productFeature.Name == productFeature.Description)
            {
                ModelState.AddModelError("Description", "The description must be different to name");
            }

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

            if (!_productInfoRepository.ProductExists(productId))
            {
                return(NotFound());
            }

            var newProductFeature = Mapper.Map <Entities.ProductFeature>(productFeature);

            _productInfoRepository.AddFeatureForProduct(productId, newProductFeature);

            if (!_productInfoRepository.Save())
            {
                return(StatusCode(500, "Failed to save"));
            }

            var newProductFeatureReturn = Mapper.Map <Models.ProductFeatureDto>(newProductFeature);

            //Note the returned productId is the same as the one that was passed in
            return(CreatedAtRoute("GetProductFeature", new { productid = productId, id = newProductFeature.Id }, newProductFeatureReturn));
        }