public int UpdateProductsCategory(int categoryID, ProductCategory newCategory) { Debug.Write(">>> Updating a products category ... "); DBStockCTX stockDB = new DBStockCTX(); /* * // 1st method: with a query * // init a query * var query = from prodCat in stockDB.productscategories * where prodCat.id == categoryID * select prodCat; * * // get the searched element * ProductCategory category = query.FirstOrDefault<ProductCategory>(); * * // 2nd method: without a query, use find method * ProductCategory prodCat = stockDB.productscategories.Find(categoryID); * * prodCat.name = newCategory.name; * prodCat.description = newCategory.description; * prodCat.products = newCategory.products; */ // 3rd method: using attach method stockDB.productscategories.Attach(newCategory); stockDB.Entry(newCategory).State = EntityState.Modified; stockDB.SaveChanges(); Debug.WriteLine("Done."); return(0); }
static void Main(string[] args) { // init a new instance of stock service Console.WriteLine("> Staring Stock Service ..."); #region DB connection init (EF6) Debug.WriteLine(">> Init StockDB ... "); // create DB if does not exists DBStockCTX stockDB = new DBStockCTX(); Console.Write(">>> Checking Stock DB existance: "); if (!stockDB.Database.Exists()) { Console.Write("does not exists!\n>>>> Creating a new one ... "); stockDB.Database.Create(); Console.WriteLine("Done."); } else { Console.WriteLine("already exists."); } //Debug.WriteLine("Done."); #endregion #region Service init (WCF) // init the service here Debug.Write(">> Init Service ... "); Uri baseAddr = new Uri("http://127.0.0.1:9999/CRUDServer"); // create the service host ServiceHost hoster = new ServiceHost(typeof(StockService), baseAddr); // add service EP hoster.AddServiceEndpoint(typeof(IStockService), new WSHttpBinding(), "Stock"); // enable metadata exchange ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; hoster.Description.Behaviors.Add(smb); // add EP to host hoster.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); // open the host hoster.Open(); Debug.WriteLine("Done."); #endregion Console.WriteLine("> Service is UP ..."); Console.WriteLine("!!! Press ENTER to quit !!!"); Console.ReadLine(); }
public int DeleteProduct(Product product) { // PS: there is some exceptions to catch ie: null, ... Debug.Write(">>> Deleting a product ... "); try { DBStockCTX stockDB = new DBStockCTX(); stockDB.products.Remove(product); stockDB.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Error:"); Console.WriteLine(ex.Message); return(1); } Debug.WriteLine("Done."); return(0); }
//private DBStockCTX stockDB = null; // stock database context handler //private ServiceHost hoster = null; // service handler #region product OPS public int AddProduct(Product product) { // PS: there is some exceptions to catch ie: duplicates, null ... Debug.Write(">>> Adding a new product ... "); try { DBStockCTX stockDB = new DBStockCTX(); stockDB.products.Add(product); stockDB.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Error:"); Console.WriteLine(ex.Message); return(1); } Debug.WriteLine("Done."); return(0); }
public List <ProductCategory> GetAllProductsCategories() { Debug.Write(">>> Requesting all product ..."); List <ProductCategory> productsCat = null; try { DBStockCTX stockDB = new DBStockCTX(); productsCat = stockDB.productscategories.ToList <ProductCategory>(); } catch (Exception ex) { Debug.WriteLine("Error:"); Console.WriteLine(ex.Message); return(productsCat); } Debug.WriteLine("Done."); return(productsCat); }
public int DeleteProductsCategory(ProductCategory category) { // PS: there is some exceptions to catch ie: duplicates, null ... Debug.Write(">>> Deleting a products category ... "); try { DBStockCTX stockDB = new DBStockCTX(); stockDB.productscategories.Remove(category); stockDB.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Error:"); Console.WriteLine(ex.Message); return(1); } Debug.WriteLine("Done."); return(0); }
public int UpdateProduct(int productID, Product newProduct) { // PS: there is some exceptions to catch ie: null, ... Debug.Write(">>> Updating a new product ... "); try { DBStockCTX stockDB = new DBStockCTX(); /* * // 1st method: with a query * // init a query * var query = from prod in stockDB.products * where prod.id == productID * select prod; * * // get the searched element * Product product = query.FirstOrDefault<Product>(); * * // 2nd method: without a query, use find method * Product prod = stockDB.products.Find(productID); * prod.name = newProduct.name; * prod.description = newProduct.description; * prod.price = newProduct.price; * prod.category = newProduct.category; */ // 3rd method: using attach method stockDB.products.Attach(newProduct); stockDB.Entry(newProduct).State = EntityState.Modified; stockDB.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Error:"); Console.WriteLine(ex.Message); return(1); } Debug.WriteLine("Done."); return(0); }