public IRecipeIngredient Save(RecipeIngredient data)
        {
            if (((RecipeIngredient)data).RecipeIngredientId <= 0)
                Insert(data);
            else
                Update(data);

            return data;
        }
        public IRecipeIngredient Save(RecipeIngredient data)
        {
            Validate(data);

            var ingredient = IngredientService.LoadByTitle(data.Title);

            if (ingredient == null)
                ingredient = IngredientService.Save((Ingredient)data);

               return DataProvider.Save(data);
        }
Example #3
0
        public IIngredient LoadOrSave(RecipeIngredient ingredient)
        {
            var ingredientObject = DataProvider.Get(ingredient.Title);

            if (ingredientObject != null)
                return ingredientObject;
            else
            {
                if (String.IsNullOrEmpty(ingredient.Description))
                    ingredient.Description = ingredient.Title;

                ingredient.ComputeUrlKey();
                ingredient.ModifiedBy = 2;
                ingredient.CreatedBy = 2;
                ingredient.CreatedDate = DateTime.UtcNow;
                ingredient.ModifiedDate = DateTime.UtcNow;
                return DataProvider.Save(ingredient) as Ingredient;
            }
        }
Example #4
0
        public RecipeIngredient ParseIngredient(string ingredientText)
        {
            if (String.IsNullOrEmpty(ingredientText))
                throw new ArgumentNullException("ingredientText");

            RecipeIngredient ingredient = new RecipeIngredient();

            var ingredientParts = ingredientText.Trim().Split(' ');
            List<IngredientComponentContainer> ingredientComponents = new List<IngredientComponentContainer>();

            for(int i = 0; i < ingredientParts.Length; i++)
            {
                var ic = new IngredientComponentContainer()
                {
                    Component = ingredientParts[i],
                    ComponentType = IngredientComponentType.Unclassified
                };

                ingredientComponents.Add(ic);
            }

            ParseQuantity(ingredient, ingredientComponents);
            ParseUnit(ingredient, ingredientComponents);
            ParseItem(ingredient, ingredientComponents);
            ParseInstructions(ingredient, ingredientComponents);

            return ingredient;
        }
Example #5
0
        private void ParseUnit(RecipeIngredient ingredient, List<IngredientComponentContainer> ingredientParts)
        {
            IngredientComponentType previousPartType = IngredientComponentType.Unclassified;

            ingredientParts.ForEach((IngredientComponentContainer part) =>
            {
                if (part.ComponentType != IngredientComponentType.Unclassified)
                {
                    previousPartType = part.ComponentType;
                    return;
                }

                if (previousPartType == IngredientComponentType.Unclassified || previousPartType == IngredientComponentType.Quantity)
                {
                    if (part.Component.Length < 4)
                    {
                        part.ComponentType = IngredientComponentType.Unit;
                        ingredient.Unit = part.Component;

                        previousPartType = part.ComponentType;
                    }
                }
            });
        }
Example #6
0
        private void ParseQuantity(RecipeIngredient ingredient, List<IngredientComponentContainer> ingredientParts)
        {
            var ingredientPart = ingredientParts[0];

            if (!UnitRegexTest.IsMatch(ingredientPart.Component))
                ingredient.Quantity = 1;
            else if (ingredientParts.Count >= 2 && UnitRegexTest.IsMatch(ingredientParts[1].Component) && ingredientParts[1].Component.Contains("/"))
            {
                ingredient.QuantityText = String.Format("{0} {1}", ingredientPart.Component, ingredientParts[1].Component);
                ingredientPart.ComponentType = IngredientComponentType.Quantity;
            }
            else
            {
                ingredient.QuantityText = ingredientPart.Component;
                ingredientPart.ComponentType = IngredientComponentType.Quantity;
            }
            ingredient.Quantity = QuantityParser.Parse(ingredient.QuantityText);
        }
Example #7
0
        private void ParseItem(RecipeIngredient ingredient, List<IngredientComponentContainer> ingredientParts)
        {
            StringBuilder itemBuilder = new StringBuilder();
            bool noteIdentifierFound = false;
            ingredientParts.ForEach((IngredientComponentContainer part) =>
            {
                if (part.ComponentType != IngredientComponentType.Unclassified)
                    return;
                else
                {
                    if (part.Component.StartsWith("(") || noteIdentifierFound || part.Component.Equals("-"))
                    {
                        part.ComponentType = IngredientComponentType.Instructions;
                        return;
                    }

                    itemBuilder.AppendFormat("{0} ", part.Component.Replace(";",""));

                    if (part.Component.EndsWith(";"))
                    {
                        noteIdentifierFound = true;
                    }
                }

                part.ComponentType = IngredientComponentType.Item;
            });

            ingredient.Title = itemBuilder.ToString().Trim();
        }
Example #8
0
        private void ParseInstructions(RecipeIngredient ingredient, List<IngredientComponentContainer> ingredientComponents)
        {
            var instructionBuilder = new StringBuilder();
            var instructionParts = ingredientComponents.Where(cmp => cmp.ComponentType == IngredientComponentType.Instructions).ToList();
            instructionParts.ForEach((IngredientComponentContainer cntr) =>
            {
                instructionBuilder.AppendFormat("{0} ", cntr.Component);
            });

            ingredient.Notes = instructionBuilder.ToString().Trim();
        }
 public void Validate(RecipeIngredient data)
 {
     throw new NotImplementedException();
 }
        private void Insert(RecipeIngredient data)
        {
            using (var connection = Database.GetConnection())
            {
                var command = connection.CreateCommand();
                command.CommandText = @"insert into recipe_ingredients
            (recipeid, ingredientid, quantity, unit, notes)
            values
            ( @recipeid, @ingredientid, @quantity, @unit, @notes )
            returning *";

                command.AddParams(
                    Database.CreateParameter("@recipeid", data.RecipeID, DbType.Int64, command),
                    Database.CreateParameter("@ingredientid", data.IngredientID, DbType.Int64, command),
                    Database.CreateParameter("@unit", data.Unit, DbType.String, command),
                    Database.CreateParameter("@quantity", data.Quantity, DbType.Int32, command),
                    Database.CreateParameter("@notes", data.Notes, DbType.String, command)
                );
                command.Connection.Open();

                var reader = command.ExecuteReader(CommandBehavior.SingleRow);
                if (!reader.Read())
                    throw new ApplicationException("Error Saving Ingredient");

                data.RecipeIngredientId = (Int64)reader["id"];
            }
        }
        private void Update(RecipeIngredient data)
        {
            using(var connection = Database.GetConnection())
            {
                var command = connection.CreateCommand();

                command.CommandText = @"update recipe_ingredients
            set recipeid = @recipeid,
            quantity = @quantity,
            unit = @unit,
            notes = @notes,
            where id = @id
            returning *";

                 command.AddParams(
                    Database.CreateParameter("@recipeid", data.RecipeID, DbType.Int64, command),
                    Database.CreateParameter("@ingredientid", data.IngredientID, DbType.Int64, command),
                    Database.CreateParameter("@unit", data.Unit, DbType.String, command),
                    Database.CreateParameter("@quantity", data.Quantity, DbType.Int32, command),
                    Database.CreateParameter("@notes", data.Notes, DbType.String, command),
                    Database.CreateParameter("@id", data.RecipeIngredientId, DbType.Int64, command)
                );
                 command.Connection.Open();
                var reader = command.ExecuteReader(CommandBehavior.SingleRow);

            }
        }
        private RecipeIngredient LoadRecipeIngredient(DbDataReader reader)
        {
            var ri = new RecipeIngredient();

            ri.IngredientID = (Int64)reader["ingredientid"];
            ri.Description = (string)reader["ingredient_description"];
            ri.CreatedDate = (DateTime)reader["ingredient_created_date"];
            ri.CreatedBy = (Int64)reader["ingredient_created_by"];
            ri.ModifiedDate = (DateTime)reader["ingredient_modified_date"];
            ri.ModifiedBy = (Int64)reader["ingredient_modified_by"];
            ri.RecipeID = (Int64)reader["recipeid"];
            ri.RecipeIngredientId = (Int64)reader["recipe_ingredient_id"];
            ri.Quantity = (Int32)reader["quantity"];
            ri.Unit = (string)reader["unit"];
            ri.Notes = (string)reader["notes"];

            return ri;
        }