public async Task <IActionResult> PostProduct([FromBody] ProductDto product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_iCategoryRepository.CategoryExist(product.CatogaryID)) { return(StatusCode(500, "不存在这个分类!")); } if (_iCategoryRepository.ProductIdExist(product.ID)) { return(StatusCode(500, "这个产品编号已经存在")); } var newProduct = Mapper.Map <Product>(product); _iCategoryRepository.AddProduct(newProduct); if (!await _iCategoryRepository.SaveAsync()) { return(StatusCode(500, "保存时出现错误!")); } return(CreatedAtAction("GetProduct", new { cid = product.CatogaryID, id = product.ID, }, product)); }
public void addProduct(int id, int productId) { CategoryEntity category = _categoryRepository.Get(id); ProductEntity product = _productRepository.Get(productId); if (category == null || product == null) { return; } _categoryRepository.AddProduct(category, product); }
public void addProduct(int id, int productId) { if (_categoryRepository.Get(id) == null || _productRepository.Get(productId) == null ) { return; } Category category = _categoryRepository.Get(id); Product product = _productRepository.Get(productId); _categoryRepository.IncludeProductCategorie(category); _categoryRepository.AddProduct(category, product); }