/// <summary>
        /// Jared Greenfield
        /// Created: 2018/01/24
        ///
        /// <remarks>
        /// Jared Greenfield
        /// Updated: 2018/02/14
        /// Added TransactionScope to make sure either all recipe, item, and offering creations
        /// happen, or they rollback to ensure consistency.
        /// </remarks>
        ///
        /// <remarks>
        /// Jared Greenfield
        /// Created: 2019/02/20
        /// Added RecipeLines to object and removed lines parameter
        /// </remarks>
        ///
        /// Adds a Recipe and its lines to the database.
        /// </summary>
        /// <param name="recipe">A recipe object.</param>
        /// <param name="item">The Item that the recipe corresponds to.</param>
        /// <param name="offering">The Offering that may or may not exist based on whether it is decided upon creation if it is a public recipe.</param>
        /// <exception cref="SQLException">Insert Fails (example of exception tag)</exception>
        /// <returns>Rows affected</returns>
        public int InsertRecipe(Recipe recipe, Item item, Offering offering)
        {
            int    returnedID = 0;
            string cmdText    = @"sp_insert_recipe";
            string cmdText2   = @"sp_insert_recipe_item_line";

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    using (SqlConnection conn = DBConnection.GetDbConnection())
                    {
                        conn.Open();
                        SqlCommand cmd1 = new SqlCommand(cmdText, conn);
                        cmd1.CommandType = CommandType.StoredProcedure;
                        cmd1.Parameters.AddWithValue("@Name", recipe.Name);
                        cmd1.Parameters.AddWithValue("@Description", recipe.Description);
                        var temp = cmd1.ExecuteScalar();
                        returnedID = Convert.ToInt32(temp);

                        foreach (var line in recipe.RecipeLines)
                        {
                            int        result = 0;
                            SqlCommand cmd2   = new SqlCommand(cmdText2, conn);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            cmd2.Parameters.AddWithValue("@RecipeID", returnedID);
                            cmd2.Parameters.AddWithValue("@ItemID", line.ItemID);
                            cmd2.Parameters.AddWithValue("@Quantity", line.Quantity);
                            cmd2.Parameters.AddWithValue("@UnitOfMeasure", line.UnitOfMeasure);
                            result = cmd2.ExecuteNonQuery();
                            if (result != 1)
                            {
                                throw new Exception("Line failed to add");
                            }
                        }
                    }
                    int offeringID = 0;
                    if (offering != null)
                    {
                        offeringID = _offeringAccessor.InsertOffering(offering);
                    }
                    if (offeringID != 0)
                    {
                        item.OfferingID = offeringID;
                    }
                    item.RecipeID = returnedID;
                    _itemAccessor.InsertItem(item);
                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(returnedID);
        }
예제 #2
0
        /// <summary author="Jared Greenfield" created="2019/02/06">
        /// Creates an Item.
        /// </summary>
        /// <exception cref="SQLException">Insert Fails (example of exception tag)</exception>
        /// <returns>List of all Items</returns>
        public int CreateItem(Item item)
        {
            int id = 0;

            try
            {
                if (item.IsValid())
                {
                    id = _itemAccessor.InsertItem(item);
                }
                else
                {
                    throw new ArgumentException("Data for this Item is not valid.");
                }
            }
            catch (Exception ex)
            {
                ExceptionLogManager.getInstance().LogException(ex);
                throw ex;
            }
            return(id);
        }