Ejemplo n.º 1
0
 public bool UpdateProduct(ProductEntity product)
 {
     // TODO: call data access layer to update product
     // first check to see if it is a valid price
     if (product.UnitPrice <= 0)
         return false;
     // ProductName can't be empty
     else if (product.ProductName == null
     || product.ProductName.Length == 0)
         return false;
     // QuantityPerUnit can't be empty
     else if (product.QuantityPerUnit == null
     || product.QuantityPerUnit.Length == 0)
         return false;
     // then validate other properties
     else
     {
         ProductEntity productInDB = GetProduct(product.ProductID);
         // invalid product to update
         if (productInDB == null)
             return false;
         // a product can't be discontinued if there are
         // non-fulfilled orders
         if (product.Discontinued == true
         && productInDB.UnitsOnOrder > 0)
             return false;
         else
             return productDAO.UpdateProduct(product);
     }
 }
Ejemplo n.º 2
0
 public ProductEntity GetProduct(int id)
 {
     /*
     // TODO: connect to DB to retrieve product
     ProductEntity p = new ProductEntity();
     p.ProductID = id;
     p.ProductName = "fake product name from data access layer";
     p.UnitPrice = (decimal)30.00;
     return p;
      */
     ProductEntity p = null;
     using (SqlConnection conn = new SqlConnection(connectionString))
     {
         SqlCommand comm = new SqlCommand();
         comm.CommandText = "select * from Products where ProductID=" + id;
         comm.Connection = conn;
         conn.Open();
         SqlDataReader reader = comm.ExecuteReader();
         if (reader.HasRows)
         {
             reader.Read();
             p = new ProductEntity();
             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;
     }
 }
Ejemplo n.º 3
0
 public ProductEntity GetProduct(int id)
 {
     // TODO: call data access layer to retrieve product
     ProductEntity p = new ProductEntity();
     p.ProductID = id;
     p.ProductName = "fake product name from business logic layer";
     p.UnitPrice = (decimal)20.00;
     if (id > 50) p.UnitsOnOrder = 30;
     return p;
 }
Ejemplo n.º 4
0
 public bool UpdateProduct(ProductEntity product)
 {
     // TODO: connect to DB to update product
     using (SqlConnection conn = new SqlConnection(connectionString))
     {
         SqlCommand cmd = new SqlCommand("UPDATE products SET ProductName=@name,QuantityPerUnit=@unit,UnitPrice=@price,Discontinued=@discontinued WHERE ProductID=@id", 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();
         int numRows = cmd.ExecuteNonQuery();
         if (numRows != 1)
             return false;
     }
     return true;
 }