Example #1
0
 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);
     }
 }
Example #2
0
 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;
 }
Example #3
0
 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(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;
        }
 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;
 }