/// <summary> /// Add a new entry to the IngredientSet table /// </summary> public static IngredientSet Add(int extendedIngredientId, int ingredientId, double amount, MeasurementUnit measurementUnit) { IngredientSet result = null; SqlConnection cn = GetConnection(); string cmd = "AddIngredientSet"; using (SqlCommand sqlCmd = new SqlCommand(cmd, cn)) { sqlCmd.CommandType = CommandType.StoredProcedure; BuildSqlParameter(sqlCmd, "@IngredientSetExtendedIngredientId", SqlDbType.Int, extendedIngredientId); BuildSqlParameter(sqlCmd, "@IngredientSetIngredientId", SqlDbType.Int, ingredientId); BuildSqlParameter(sqlCmd, "@IngredientSetAmount", SqlDbType.Float, amount); BuildSqlParameter(sqlCmd, "@IngredientSetMeasurementUnit", SqlDbType.SmallInt, measurementUnit); BuildSqlParameter(sqlCmd, "@IngredientSetId", SqlDbType.Int, ParameterDirection.ReturnValue); if (sqlCmd.ExecuteNonQuery() > 0) { result = new IngredientSet(Convert.ToInt32(sqlCmd.Parameters["@IngredientSetId"].Value), extendedIngredientId, ingredientId, amount, measurementUnit); } } FinishedWithConnection(cn); return(result); }
/// <summary> /// Update an entry in the IngredientSet table /// </summary> public static bool Update(IngredientSet ingredientSet) { bool result = false; SqlConnection cn = GetConnection(); result = Update(cn, ingredientSet); FinishedWithConnection(cn); return(result); }
/// <summary> /// Get an entry from the IngredientSet table /// </summary> public static IngredientSet Get(int id) { IngredientSet result = null; SqlConnection cn = GetConnection(); result = Get(cn, id); FinishedWithConnection(cn); return(result); }
private static IngredientSet Get(SqlConnection cn, int id) { IngredientSet result = null; using (SqlCommand cmd = new SqlCommand("SELECT * FROM IngredientSet WHERE IngredientSetId=" + id, cn)) { using (SqlDataReader rdr = cmd.ExecuteReader()) { if (rdr.Read()) { result = BuildIngredientSet(rdr); } } } return(result); }
/// <summary> /// Checks to see if this ingredient contains another ingredient /// </summary> /// <param name="ingredientId"></param> /// <returns></returns> public bool ContainsIngredient(int ingredientId) { if (!IngredientSet.HasEntries(Id)) { return(false); } foreach (IngredientSet ingredientSet in IngredientSet.GetAll(Id)) { Ingredient ingredient = Ingredient.Get(ingredientSet.IngredientId); if ((ingredientId == ingredient.Id) || (ingredient.ContainsIngredient(ingredientId))) { return(true); } } return(false); }
/// <summary> /// Delete an entry from the IngredientSet table /// </summary> public static bool Delete(int id) { Int32 rowsAffected = 0; SqlConnection cn = GetConnection(); IngredientSet ingredientSet = Get(cn, id); if (ingredientSet != null) { using (SqlCommand sqlCmd = cn.CreateCommand()) { sqlCmd.CommandText = "DELETE FROM IngredientSet WHERE IngredientSetId=" + id; rowsAffected = sqlCmd.ExecuteNonQuery(); } } FinishedWithConnection(cn); return(rowsAffected != 0); }
/// <summary> /// Will either use the cost of its ingredients, if this is /// a prepared ingredient, else returns this.CostPerUnit /// </summary> /// <returns></returns> public double GetActualCostPerUnit() { if (!IngredientSet.HasEntries(Id)) { return(CostPerUnit); } double result = 0; foreach (IngredientSet ingredientSet in IngredientSet.GetAll(Id)) { Ingredient ingredient = Ingredient.Get(ingredientSet.IngredientId); double amountInInventoryUnits = UnitConversion.Convert(ingredientSet.Amount, ingredientSet.MeasurementUnit, ingredient.MeasurementUnit); result += (amountInInventoryUnits * ingredient.GetActualCostPerUnit()); } return(result); }
private static bool Update(SqlConnection cn, IngredientSet ingredientSet) { Int32 rowsAffected = 0; using (SqlCommand sqlCmd = cn.CreateCommand()) { sqlCmd.CommandText = "UPDATE IngredientSet SET IngredientSetExtendedIngredientId=@IngredientSetExtendedIngredientId,IngredientSetIngredientId=@IngredientSetIngredientId,IngredientSetAmount=@IngredientSetAmount,IngredientSetMeasurementUnit=@IngredientSetMeasurementUnit WHERE IngredientSetId=@IngredientSetId"; BuildSqlParameter(sqlCmd, "@IngredientSetId", SqlDbType.Int, ingredientSet.Id); BuildSqlParameter(sqlCmd, "@IngredientSetExtendedIngredientId", SqlDbType.Int, ingredientSet.ExtendedIngredientId); BuildSqlParameter(sqlCmd, "@IngredientSetIngredientId", SqlDbType.Int, ingredientSet.IngredientId); BuildSqlParameter(sqlCmd, "@IngredientSetMeasurementUnit", SqlDbType.SmallInt, ingredientSet.MeasurementUnit); BuildSqlParameter(sqlCmd, "@IngredientSetAmount", SqlDbType.Float, ingredientSet.Amount); rowsAffected = sqlCmd.ExecuteNonQuery(); } return(rowsAffected != 0); }
public static IngredientSet Get(int extendedIngredientId, int ingredientId) { IngredientSet result = null; SqlConnection cn = GetConnection(); using (SqlCommand cmd = new SqlCommand("SELECT * FROM IngredientSet WHERE IngredientSetExtendedIngredientId=" + extendedIngredientId + " AND IngredientSetIngredientId=" + ingredientId, cn)) { using (SqlDataReader rdr = cmd.ExecuteReader()) { if (rdr.Read()) { result = BuildIngredientSet(rdr); } } } FinishedWithConnection(cn); return(result); }
public bool Update() { return(IngredientSet.Update(this)); }