Exemplo n.º 1
0
        public static void CalculateRecipeQuantity(Recipe rcp)
        {
            Dictionary<decimal, string> conversionchart = new Dictionary<decimal, string>();

            using (MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["default"].ToString()))
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("Lookup_Select_QuantityConversion", conn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    decimal keyvalue = (decimal)TruncateQuantity(Convert.ToDecimal(dr["QuantityDecimal"]));
                    string displayvalue = Convert.ToString(dr["QuantityDisplay"]);
                    conversionchart.Add(keyvalue, displayvalue);
                }
                dr.Close();
                conn.Close();
            }

            foreach(RecipeIngredient ing in rcp.IngredientList)
            {
                if (ing.Quantity == 0)
                { ing.DisplayQuantity = String.Empty; }
                else
                {
                    ing.Quantity = TruncateQuantity(ing.Quantity) * rcp.Quantity;

                    decimal qtynumber = Math.Truncate((decimal)(ing.Quantity ?? 0));
                    decimal qtydecimal = (decimal)(TruncateQuantity(ing.Quantity - qtynumber));
                    if (qtydecimal == TruncateQuantity(Convert.ToDecimal(0.9999999)))
                    {
                        qtynumber += 1;
                        qtydecimal = 0;
                    }

                    if (qtynumber > 0)
                    { ing.DisplayQuantity = qtynumber.ToString(); }
                    if (qtydecimal > 0)
                    { 
                        if (conversionchart.ContainsKey(qtydecimal))
                        { ing.DisplayQuantity += " " + conversionchart[qtydecimal]; }
                        else
                        { ing.DisplayQuantity += " " + qtydecimal.ToString(); }
                    }
                    ing.DisplayQuantity = ing.DisplayQuantity.Trim();

                }
            }
        }
Exemplo n.º 2
0
        public static List<Recipe> SearchRecipes(string _searchtext, string[] _ingredients, string[] _categories)
        {
            List<Recipe> results = new List<Recipe>();

            string ingsearchlist = String.Empty;
            foreach (string ing in _ingredients)
            { ingsearchlist += ing + "|"; }

            string ctgsearchlist = String.Empty;
            foreach (string ctg in _categories)
            { ctgsearchlist += ctg + "|"; }

            DataSet ds = new DataSet();
            using (MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["default"].ToString()))
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("Recipe_Select_RecipeSearch", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pSearchText", _searchtext);
                cmd.Parameters.AddWithValue("@pIngredients", ingsearchlist);
                cmd.Parameters.AddWithValue("@pCategories", ctgsearchlist);
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(ds, "Recipes");

                Recipe newrcp;
                string[] catlist;
                string[] catitem;

                foreach (DataRow dr in ds.Tables["Recipes"].Rows)
                {
                    newrcp = new Recipe(dr);
                    //The category list is returned in the format of: catcode::catname||catcode::catname||
                    newrcp.CategoryList = new List<Recipe.Category>();
                    catlist = dr["CategoryList"].ToString().Split(new string[] {"||"}, StringSplitOptions.RemoveEmptyEntries);
                    foreach(string category in catlist)
                    {
                        catitem = category.Split(new string[] {"::"}, StringSplitOptions.RemoveEmptyEntries);
                        newrcp.CategoryList.Add(new Recipe.Category(catitem[0], catitem[1]));
                    }

                    results.Add(newrcp);
                }

            }

            return results;
        }