public bool UpdateProduct( ref ProductBDO productBDO, ref string message) { var productInDB = GetProduct(productBDO.ProductID); // invalid product to update if (productInDB == null) { message = "cannot get product for this ID"; return(false); } // a product cannot be discontinued // if there are non-fulfilled orders if (productBDO.Discontinued == true && productInDB.UnitsOnOrder > 0) { message = "cannot discontinue this product"; return(false); } else { return(productDAO.UpdateProduct(ref productBDO, ref message)); } }
public Product GetProduct(int id) { ProductBDO productBDO = null; try { productBDO = productLogic.GetProduct(id); } catch (Exception e) { string msg = e.Message; string reason = "GetProduct Exception"; throw new FaultException <ProductFault> (new ProductFault(msg), reason); } if (productBDO == null) { string msg = string.Format("No product found for id {0}", id); string reason = "GetProduct Empty Product"; throw new FaultException <ProductFault> (new ProductFault(msg), reason); } Product product = new Product(); TranslateProductBDOToProductDTO(productBDO, product); return(product); }
public bool UpdateProduct(ProductBDO product, ref string message) { ProductBDO productInDB = GetProduct(product.ProductID); // invalid product to update if (productInDB == null) { message = "cannot get product for this ID"; return(false); } // a product can't be discontinued // if there are non-fulfilled orders if (product.Discontinued == true && productInDB.UnitsOnOrder > 0) { message = "cannot discontinue this product"; return(false); } else { /* * // TODO: call data access layer to update product * message = "Product updated successfully"; * return true; */ return(productDAO.UpdateProduct(product, ref message)); } }
public ProductBDO GetProduct(int id) { ProductBDO productBDO = null; using (var NWEntities = new NorthwindEntities()) { Product product = (from p in NWEntities.Products where p.ProductID == id select p).FirstOrDefault(); if (product != null) { productBDO = new ProductBDO() { ProductID = product.ProductID, ProductName = product.ProductName, QuantityPerUnit = product.QuantityPerUnit, UnitPrice = (decimal)product.UnitPrice, UnitsInStock = (int)product.UnitsInStock, ReorderLevel = (int)product.ReorderLevel, UnitsOnOrder = (int)product.UnitsOnOrder, Discontinued = product.Discontinued, RowVersion = product.RowVersion } } ; } return(productBDO); }
private void TranslateProductDTOToProductBDO( Product product, ProductBDO productBDO) { productBDO.ProductID = product.ProductID; productBDO.ProductName = product.ProductName; productBDO.QuantityPerUnit = product.QuantityPerUnit; productBDO.UnitPrice = product.UnitPrice; productBDO.Discontinued = product.Discontinued; }
private void TranslateProductBDOToProductDTO( ProductBDO productBDO, Product product) { product.ProductID = productBDO.ProductID; product.ProductName = productBDO.ProductName; product.QuantityPerUnit = productBDO.QuantityPerUnit; product.UnitPrice = productBDO.UnitPrice; product.Discontinued = productBDO.Discontinued; product.RowVersion = productBDO.RowVersion; }
public ProductBDO GetProduct(int id) { /* * // TODO: connect to DB to retrieve product * ProductBDO p = new ProductBDO(); * p.ProductID = id; * p.ProductName = * "fake product name from data access layer"; * p.UnitPrice = 30.00m; * p.QuantityPerUnit = "fake QPU"; * return p; */ ProductBDO p = null; using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select * from Products where ProductID=@id"; cmd.Parameters.AddWithValue("@id", id); cmd.Connection = conn; conn.Open(); using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.HasRows) { reader.Read(); p = new ProductBDO(); p.ProductID = id; p.ProductName = (string)reader["ProductName"]; p.QuantityPerUnit = (string)reader["QuantityPerUnit"]; p.UnitPrice = (decimal)reader["UnitPrice"]; p.UnitsInStock = (short)reader["UnitsInStock"]; p.UnitsOnOrder = (short)reader["UnitsOnOrder"]; p.ReorderLevel = (short)reader["ReorderLevel"]; p.Discontinued = (bool)reader["Discontinued"]; } } } } return(p); }
public Product GetProduct(int id) { /* * // TODO: call business logic layer to retrieve product * Product product = new Product(); * product.ProductID = id; * product.ProductName = * "fake product name from service layer"; * product.UnitPrice = 10.0m; * product.QuantityPerUnit = "fake QPU"; * return product; */ ProductBDO productBDO = null; try { productBDO = productLogic.GetProduct(id); } catch (Exception e) { string msg = e.Message; string reason = "GetProduct Exception"; throw new FaultException <ProductFault> (new ProductFault(msg), reason); } if (productBDO == null) { string msg = string.Format("No product found for id {0}", id); string reason = "GetProduct Empty Product"; if (id == 999) { throw new Exception(msg); } else { throw new FaultException <ProductFault> (new ProductFault(msg), reason); } } Product product = new Product(); TranslateProductBDOToProductDTO(productBDO, product); return(product); }
public bool UpdateProduct( ref ProductBDO productBDO, ref string message) { message = "product updated successfully"; bool ret = true; using (var NWEntities = new NorthwindEntities()) { var productID = productBDO.ProductID; Product productInDB = (from p in NWEntities.Products where p.ProductID == productID select p).FirstOrDefault(); // check product if (productInDB == null) { throw new Exception("No product with ID " + productBDO.ProductID); } NWEntities.Products.Remove(productInDB); // update product productInDB.ProductName = productBDO.ProductName; productInDB.QuantityPerUnit = productBDO.QuantityPerUnit; productInDB.UnitPrice = productBDO.UnitPrice; productInDB.Discontinued = productBDO.Discontinued; productInDB.RowVersion = productBDO.RowVersion; NWEntities.Products.Attach(productInDB); NWEntities.Entry(productInDB).State = System.Data.EntityState.Modified; int num = NWEntities.SaveChanges(); productBDO.RowVersion = productInDB.RowVersion; if (num != 1) { ret = false; message = "no product is updated"; } } return(ret); }
public bool UpdateProduct(Product product, ref string message) { bool result = true; // first check to see if it is a valid price if (product.UnitPrice <= 0) { message = "Price cannot be <= 0"; result = false; } // ProductName can't be empty else if (string.IsNullOrEmpty(product.ProductName)) { message = "Product name cannot be empty"; result = false; } // QuantityPerUnit can't be empty else if (string.IsNullOrEmpty(product.QuantityPerUnit)) { message = "Quantity cannot be empty"; result = false; } else { ProductBDO productBDO = new ProductBDO(); TranslateProductDTOToProductBDO(product, productBDO); try { result = productLogic.UpdateProduct( productBDO, ref message); } catch (Exception e) { string msg = e.Message; string reason = "UpdateProduct Exception"; throw new FaultException <ProductFault> (new ProductFault(msg), reason); } } return(result); }
public bool UpdateProduct(ProductBDO product, ref string message) { /* * // TODO: connect to DB to update product * message = "product updated successfully"; * return true; */ message = "product updated successfully"; bool ret = true; using (SqlConnection conn = new SqlConnection(connectionString)) { string cmdStr = @"UPDATE products SET ProductName=@name, QuantityPerUnit=@unit, UnitPrice=@price, Discontinued=@discontinued WHERE ProductID=@id"; using (SqlCommand cmd = new SqlCommand(cmdStr, conn)) { cmd.Parameters.AddWithValue("@name", product.ProductName); cmd.Parameters.AddWithValue("@unit", product.QuantityPerUnit); cmd.Parameters.AddWithValue("@price", product.UnitPrice); cmd.Parameters.AddWithValue("@discontinued", product.Discontinued); cmd.Parameters.AddWithValue("@id", product.ProductID); conn.Open(); if (cmd.ExecuteNonQuery() != 1) { message = "no product is updated"; ret = false; } } } return(ret); }
public bool UpdateProduct(ref Product product, ref string message) { bool result = true; // ProductName can't be empty if (string.IsNullOrEmpty(product.ProductName)) { message = "Product name cannot be empty"; result = false; } // QuantityPerUnit can't be empty else if (string.IsNullOrEmpty(product.QuantityPerUnit)) { message = "Quantity cannot be empty"; result = false; } else { try { var productBDO = new ProductBDO(); TranslateProductDTOToProductBDO(product, productBDO); result = productLogic.UpdateProduct( ref productBDO, ref message); product.RowVersion = productBDO.RowVersion; } catch (Exception e) { string msg = e.Message; throw new FaultException <ProductFault> (new ProductFault(msg), msg); } } return(result); }