public void InsertProduct(Product product) { try { ValidateProduct(product); productRepository.InsertProduct(product); } catch (Exception ex) { throw ex; } }
//public IEnumerable<Product> GetSingleProductByID(long productID) //{ // return productRepository.GetSingleProductByID(productID); //} private void ValidateProduct(Product product) { if(product.ProductID!=null) { var duplicateProduct = productRepository.GetProduct(product).FirstOrDefault(); if (duplicateProduct != null && duplicateProduct.ProductID != product.ProductID) { if (duplicateProduct.SKU == product.SKU) { throw new ProductException(String.Format("Product {0} already exists", duplicateProduct.Name, duplicateProduct.SKU)); } //if (duplicateProduct.Name == product.Name) //{ // throw new ProductException(String.Format("Product {0} already exists", duplicateProduct.Name, duplicateProduct.SKU)); //} } } }
public void UpdateProduct(Product product) { try { var currentProduct = context.Products.Find(product.ProductID); context.Entry(currentProduct).CurrentValues.SetValues(product); SaveChanges(); } catch (Exception ex) { throw ex; } }
public List<Product> GetProduct(Product product) { var products = (context.Products.Where(p => p.SKU == product.SKU && p.CompanyID==product.CompanyID)).ToList(); return products; }
public void InsertProduct(Product product) { try { context.Entry(product).State = EntityState.Added; SaveChanges(); } catch (Exception ex) { throw ex; } }