// work out if the Fact already exists, if it does then use it for the mapping table, else create it
        private static void addFacts(RecipeObj recipe)
        {
            using (var context = new WoolworthsDBDataContext())
            {
                // clear the current mappings
                var maps = context.RecipeFactMaps.Where(r => r.RecipeID == recipe.RecipeID);
                context.RecipeFactMaps.DeleteAllOnSubmit(maps);
                context.SubmitChanges();

                if (recipe.Facts == null)
                {
                    return;
                }

                foreach (var factType in recipe.Facts)
                {
                    // create or retrieve the RecipeCuisine.ID value
                    var master       = context.RecipeFacts.SingleOrDefault(c => c.Description == factType);
                    var recipeFactID = 0;
                    if (master == null)
                    {
                        var toCreate = new RecipeFact()
                        {
                            Description = factType
                        };
                        context.RecipeFacts.InsertOnSubmit(toCreate);
                        context.SubmitChanges();
                        recipeFactID = toCreate.ID;
                    }
                    else
                    {
                        recipeFactID = master.ID;
                    }

                    // add to the mapping table
                    var map = new RecipeFactMap()
                    {
                        RecipeFactID = recipeFactID,
                        RecipeID     = recipe.RecipeID
                    };
                    context.RecipeFactMaps.InsertOnSubmit(map);
                    context.SubmitChanges();
                }
            }
        }
Example #2
0
        public void Recipes_can_be_created_from_json()
        {
            var settings = new JsonSerializerSettingsBuilder()
                           .UseTraceWriter(new XunitTraceWriter(_output))
                           .UseMissingMemberHandling(MissingMemberHandling.Error)
                           .Build();

            AssertEx.ForEach(_fixture.Db.Recipes,
                             json =>
            {
                var actual = JsonConvert.DeserializeObject <Recipe>(json, settings);

                RecipeFact.Id_is_positive(actual);
                RecipeFact.Output_item_id_is_positive(actual);
                RecipeFact.Output_item_count_is_positive(actual);
                RecipeFact.Min_rating_is_between_0_and_500(actual);
                RecipeFact.Time_to_craft_is_positive(actual);
            });
        }
        // work out if the Fact already exists, if it does then use it for the mapping table, else create it
        private static void addFacts(RecipeObj recipe)
        {
            using (var context = new WoolworthsDBDataContext())
            {
                // clear the current mappings
                var maps = context.RecipeFactMaps.Where(r => r.RecipeID == recipe.RecipeID);
                context.RecipeFactMaps.DeleteAllOnSubmit(maps);
                context.SubmitChanges();

                if (recipe.Facts == null)
                    return;

                foreach (var factType in recipe.Facts)
                {
                    // create or retrieve the RecipeCuisine.ID value
                    var master = context.RecipeFacts.SingleOrDefault(c => c.Description == factType);
                    var recipeFactID = 0;
                    if (master == null)
                    {
                        var toCreate = new RecipeFact()
                        {
                            Description = factType
                        };
                        context.RecipeFacts.InsertOnSubmit(toCreate);
                        context.SubmitChanges();
                        recipeFactID = toCreate.ID;
                    }
                    else
                    {
                        recipeFactID = master.ID;
                    }

                    // add to the mapping table
                    var map = new RecipeFactMap()
                    {
                        RecipeFactID = recipeFactID,
                        RecipeID = recipe.RecipeID
                    };
                    context.RecipeFactMaps.InsertOnSubmit(map);
                    context.SubmitChanges();
                }
            }
        }