public static PriceData GetPrices(Recipe r, SizeDTO[] sizes)
 {
     PriceData result = new PriceData()
     {
         PriceLow = BASE_PRICE * sizes[0].SizeValue,
         PriceMed = BASE_PRICE * sizes[1].SizeValue,
         PriceHigh = BASE_PRICE * sizes[2].SizeValue
     };
     foreach (var i in r.Ingredients)
     {
         if (sizes.Count() != 3) throw new Exception("Invalid sizes collection");
         result.PriceLow += i.NormalWeight * (double)i.PricePerUnit * sizes[0].SizeValue;
         result.PriceMed += i.NormalWeight * (double)i.PricePerUnit * sizes[1].SizeValue;
         result.PriceHigh += i.NormalWeight * (double)i.PricePerUnit * sizes[2].SizeValue;
     }
     return result;
 }
        public RecipeDTO ToSimpleDto(Recipe r)
        {
            RecipeDTO rDto = new RecipeDTO { Name = r.Name, RecipeID = r.RecipeID };

            if (r.Ingredients != null)
            {
                List<OrderIngredientDTO> ingDtos = new List<OrderIngredientDTO>();

                foreach (var ing in r.Ingredients)
                {
                    ingDtos.Add(new IngredientAssembler().ToOrderIngredientDto(ing));
                }
                rDto.Ingredients = ingDtos;
            }

            return rDto;
        }
        public TrioResponse<List<RecipeDTO>, List<OrderIngredientDTO>, int> UpdateOrRemoveRecipe(UpdateOrRemoveRequest<List<RecipeDTO>> request)
        {
            if (request.Data == null && request.DataToRemove == null)
                throw PizzaServiceFault.Create(Messages.NO_DATA);

            using (var db = new PizzaUnitOfWork())
            {
                return db.inTransaction(uof =>
                {
                    if (!HasRights(GetUser(request).Data, 2))
                        throw PizzaServiceFault.Create(Messages.NO_PERMISSIONS);

                    if (request.Data != null)
                    {
                        foreach (var recipe in request.Data)
                        {
                            Recipe rec = uof.Db.Recipies.Get(recipe.RecipeID);
                            if (rec != null)
                            {
                                rec.Name = recipe.Name;
                                int j;
                                for (int i = 0; i < rec.Ingredients.Count; i++)
                                {
                                    Ingredient ingredient = rec.Ingredients.ElementAt(i);
                                    for (j = 0; j < recipe.Ingredients.Count; j++)
                                    {
                                        if (ingredient.IngredientID == recipe.Ingredients[j].IngredientID)
                                            break;
                                    }
                                    if (j == recipe.Ingredients.Count)
                                    {
                                        rec.Ingredients.Remove(ingredient);
                                    }
                                }

                                foreach (var ing in recipe.Ingredients)
                                {
                                    Ingredient ingredient = uof.Db.Ingredients.Get(ing.IngredientID);
                                    if (ingredient == null)
                                    {
                                        db.RequestRollback = true;
                                        throw PizzaServiceFault.Create(Messages.INGS_LIST_OUT_OF_DATE);
                                    }

                                    rec.Ingredients.Add(ingredient);
                                }
                                uof.Db.Recipies.Update(rec);
                            }
                            else
                            {
                                rec = new Recipe { Name = recipe.Name };
                                rec.Ingredients = new List<Ingredient>();
                                foreach (var ing in recipe.Ingredients)
                                {
                                    Ingredient ingredient = uof.Db.Ingredients.Get(ing.IngredientID);
                                    if (ingredient == null)
                                    {
                                        db.RequestRollback = true;
                                        throw PizzaServiceFault.Create(Messages.INGS_LIST_OUT_OF_DATE);
                                    }
                                    rec.Ingredients.Add(ingredient);
                                }
                                uof.Db.Recipies.Insert(rec);
                            }
                        }
                    }
                    if (request.DataToRemove != null)
                    {
                        foreach (var recipe in request.DataToRemove)
                        {
                            Recipe rec = uof.Db.Recipies.Get(recipe.RecipeID);
                            if (rec != null)
                                uof.Db.Recipies.Delete(rec);
                        }
                    }

                    uof.Db.Commit();

                    return TrioResponse.Create(uof.Db.Recipies.FindAll().ToList().Select(recipeAssembler.ToSimpleDto).ToList(),
                        uof.Db.Ingredients.FindAll().Select(ingAssembler.ToOrderIngredientDto).ToList(),
                        0);
                });
            }
        }
        //private List<OrderDetail> mergeIngredients(List<OrderDetailDTO> det, IEnumerable<OrderIngredientDTO> ing)
        //{
        //    foreach (var d in det)
        //        foreach (var i in d.Ingredients)
        //        {
        //            Ingredient s = ing.FirstOrDefault((e) => { return e.IngredientID == i.Ingredient.IngredientID; });
        //            if (s == null) throw new Exception("Inconsistien data");
        //            i.Ingredient = s;
        //        }
        //    return det;
        //}
        //private List<OrderDetail> mergeSizes(List<OrderDetailDTO> det, IEnumerable<SizeDTO> sizes)
        //{
        //    foreach (var d in det)
        //    {
        //        d.Size = sizes.FirstOrDefault((e) => { return e.SizeValue == d.Size.SizeValue; });
        //        if (d.Size == null) throw new Exception("Inconsistien data");
        //    }
        //    return det;
        //}
        public void InsertRecipe(UpdateRequest<RecipeDTO> req)
        {
            using (var db = new PizzaUnitOfWork())
            {
                db.inTransaction(uow =>
                {
                    if (!HasRights(GetUser(req).Data, 2))
                        throw PizzaServiceFault.Create(Messages.NO_PERMISSIONS);

                    Recipe r = new Recipe { Name = req.Data.Name };
                    List<Ingredient> ings = new List<Ingredient>();

                    foreach (var ingDto in req.Data.Ingredients)
                    {
                        ings.Add(db.Ingredients.Get(ingDto.IngredientID));
                    }
                    r.Ingredients = ings;
                    db.Recipies.Insert(r);
                    uow.Db.Commit();
                });
            }
        }
        public void Recipe_PersistanceTest()
        {
            var compare = new CompareObjects();
            compare.IgnoreObjectTypes = true;

            InAutoRollbackTransaction(uof =>
                {
                    List<Ingredient> ings = new List<Ingredient>
                    {
                        new Ingredient{ ExtraWeight=3, Name="ING", NormalWeight=1, PricePerUnit=0.1M, StockQuantity=100}
                    };
                    Recipe r = new Recipe { Name = "R", Ingredients = ings };
                    uof.Db.Recipies.Insert(r);
                    uof.Db.Commit();
                    uof.Db.ObjectContext().DetachAll();

                    Recipe r2 = uof.Db.Recipies.Get(r.RecipeID);
                    Assert.IsTrue(r != r2);
                    Assert.IsTrue(compare.Compare(r, r2));
                });
        }