示例#1
0
        partial void Merge(Recipe entity, RecipeDTO dto, object state)
        {
            int recipeId;
            if (int.TryParse(dto.RecipeId, out recipeId))
            {
                entity.RecipeId = recipeId;
                entity.ChatLink = new RecipeChatLink { RecipeId = recipeId }.ToString();
            }

            int outputItemId;
            if (int.TryParse(dto.OutputItemId, out outputItemId))
            {
                entity.OutputItemId = outputItemId;
            }

            int outputItemCount;
            if (int.TryParse(dto.OutputItemCount, out outputItemCount))
            {
                entity.OutputItemCount = outputItemCount;
            }

            int minimumRating;
            if (int.TryParse(dto.MinimumRating, out minimumRating))
            {
                entity.MinimumRating = minimumRating;
            }

            double timeToCraft;
            if (double.TryParse(dto.TimeToCraft, out timeToCraft))
            {
                entity.TimeToCraft = TimeSpan.FromMilliseconds(timeToCraft);
            }

            var craftingDisciplines = dto.CraftingDisciplines;
            if (craftingDisciplines != null)
            {
                entity.CraftingDisciplines = this.craftingDisciplineCollectionConverter.Convert(craftingDisciplines, state);
            }

            var flags = dto.Flags;
            if (flags != null)
            {
                entity.Flags = this.recipeFlagCollectionConverter.Convert(flags, state);
            }

            var ingredients = dto.Ingredients;
            if (ingredients != null)
            {
                entity.Ingredients = this.itemQuantityCollectionConverter.Convert(ingredients, state);
            }
        }
示例#2
0
        public async Task <RecipeDTO> GetByIdAsync(int id)
        {
            var getRecipe = await dbAccess.Recipes.GetByIdAsync(id);

            //Check if recipe exists
            if (getRecipe == null)
            {
                throw new ArgumentNullException("The recipe could not be found!");
            }

            //Convert user info to DTO
            var user = await dbAccess.Users.GetByIdAsync(getRecipe.UserId);

            UserDTO uResult = new UserDTO();

            uResult.Id       = user.Id;
            uResult.Username = user.Username;

            //Create list for ingredientlines
            List <IngredientLineDTO> ilResults = new List <IngredientLineDTO>();
            //Fetch ingredientlines in DB based on recipeId
            var ingredientLines = (await dbAccess.IngredientLines.FindByCondition(nameof(IngredientLine.RecipeId), getRecipe.Id)).ToList();

            //Loop over ingredientlines in DB, convert to DTO and add to new list of ingredientlines
            foreach (var il in ingredientLines)
            {
                IngredientLineDTO ingredientLineDTO = new IngredientLineDTO();
                var ingredient = await dbAccess.Ingredients.GetByIdAsync(il.IngredientId);

                IngredientDTO ingredientDTO = new IngredientDTO();
                ingredientDTO.Id                 = ingredient.Id;
                ingredientDTO.Name               = ingredient.Name;
                ingredientLineDTO.Ingredient     = ingredientDTO;
                ingredientLineDTO.Amount         = il.Amount;
                ingredientLineDTO.MeasureUnit    = il.MeasureUnit;
                ingredientLineDTO.MeasureUnitInt = (int)il.MeasureUnit;

                ilResults.Add(ingredientLineDTO);
            }

            //Create list for images
            List <ImageDTO> imResults = new List <ImageDTO>();
            //Fetch images in DB based on recipeId
            var images = (await dbAccess.Images.FindByCondition(nameof(Image.RecipeId), getRecipe.Id)).ToList();

            //Loop over images in DB, convert to DTO and add to new list of images
            foreach (var im in images)
            {
                ImageDTO imageDTO = new ImageDTO();
                imageDTO.Id       = im.Id;
                imageDTO.FileName = im.FileName;

                imResults.Add(imageDTO);
            }

            //Convert recipe to DTO and return result
            RecipeDTO result = new RecipeDTO();

            result.User            = uResult;
            result.Id              = getRecipe.Id;
            result.Title           = getRecipe.Title;
            result.Slug            = getRecipe.Slug;
            result.Instruction     = getRecipe.Instruction;
            result.CreatedAt       = getRecipe.CreatedAt;
            result.IngredientLines = ilResults;
            result.RowVer          = getRecipe.RowVer;
            result.Images          = imResults;

            return(result);
        }
示例#3
0
        private void UpdateImage(RecipeDTO dto, List <Image> existing)
        {
            //Add image - Identify images to be created
            List <ImageDTO> toBeCreated = new List <ImageDTO>();

            foreach (var item in dto.Images)
            {
                if (existing.Any(ex => ex.Id == item.Id))
                {
                    //Nothing happens!
                }
                else
                {
                    toBeCreated.Add(item);
                }
            }

            //Remaining image - Identify images to remain as is
            List <ImageDTO> toRemain = new List <ImageDTO>();

            foreach (var item in dto.Images)
            {
                if (existing.Any(ex => ex.Id == item.Id))
                {
                    toRemain.Add(item);
                }
                //else - Nothing happens!
            }

            //Delete image - Identify images to be deleted
            List <Image> toBeDeleted = new List <Image>();

            foreach (var e in existing)
            {
                if (toBeCreated.Any(ex => ex.Id == e.Id) || toRemain.Any(ex => ex.Id == e.Id))
                {
                    //Nothing happens
                }
                else
                {
                    toBeDeleted.Add(e);
                }
            }

            //Create new image
            foreach (var item in toBeCreated)
            {
                //Create new image
                Image image = new Image();
                image.Id       = 0;
                image.RecipeId = dto.Id;
                image.FileName = item.FileName;

                //Send to DB
                dbAccess.Images.AddAsync(image);
            }

            //Delete image
            foreach (var item in toBeDeleted)
            {
                dbAccess.Images.DeleteAsync(item.Id);
            }
        }
示例#4
0
        private void UpdateIngredientLine(RecipeDTO dto, List <IngredientLine> existing)
        {
            //Add ingredient - Identify ingredientLine to be created
            List <IngredientLineDTO> toBeCreated = new List <IngredientLineDTO>();

            foreach (var item in dto.IngredientLines)
            {
                if (existing.Any(ex => ex.Id == item.Id))
                {
                    //Nothing happens!
                }
                else
                {
                    toBeCreated.Add(item);
                }
            }

            //Update ingredient - Identify ingredientLine to be updated
            List <IngredientLineDTO> toBeUpdated = new List <IngredientLineDTO>();

            foreach (var item in dto.IngredientLines)
            {
                if (existing.Any(ex => ex.Id == item.Id))
                {
                    toBeUpdated.Add(item);
                }
                //else - Nothing happens!
            }

            //Delete ingredient - Identify ingredientLine to be deleted
            //Type is not DTO, as we delete from the database
            List <IngredientLine> toBeDeleted = new List <IngredientLine>();

            foreach (var e in existing)
            {
                if (toBeCreated.Any(ex => ex.Id == e.Id) || toBeUpdated.Any(ex => ex.Id == e.Id))
                {
                    //Nothing happens!
                }
                else
                {
                    toBeDeleted.Add(e);
                }
            }

            //Create new ingredientLine
            foreach (var item in toBeCreated)
            {
                //Get or create the required ingredient
                Ingredient ingredient = dbAccess.Ingredients.FindByCondition(nameof(Ingredient.Name), item.Ingredient.Name).Result.ToList().FirstOrDefault();
                if (ingredient == null)
                {
                    var ingredientToBeCreated = new Ingredient();
                    ingredientToBeCreated.Id   = 0;
                    ingredientToBeCreated.Name = item.Ingredient.Name;

                    ingredient = dbAccess.Ingredients.AddAsync(ingredientToBeCreated).Result;
                }

                //Create new ingredientLine
                IngredientLine il = new IngredientLine();
                il.Id           = 0;
                il.RecipeId     = dto.Id;
                il.IngredientId = ingredient.Id;
                il.Amount       = item.Amount;
                il.MeasureUnit  = ConvertToEnum(item.MeasureUnitText);

                //Send to DB
                dbAccess.IngredientLines.AddAsync(il);
            }

            //Update existing ingredientLine
            foreach (var item in toBeUpdated)
            {
                IngredientLine il = dbAccess.IngredientLines.GetByIdAsync(item.Id).Result;

                il.Amount      = item.Amount;
                il.MeasureUnit = ConvertToEnum(item.MeasureUnitText);

                //Send to DB
                dbAccess.IngredientLines.UpdateAsync(il);
            }

            //Delete existing ingredientLine
            foreach (var item in toBeDeleted)
            {
                dbAccess.IngredientLines.DeleteAsync(item.Id);
            }
        }
示例#5
0
        //Validation of inbound recipe(in DTO format)
        private void Validation(RecipeDTO recipe)
        {
            if (recipe == null)
            {
                throw new ArgumentNullException("Recipe cannot be null!");
            }
            else if (recipe.User == null)
            {
                throw new ArgumentNullException("User cannot be null!");
            }
            else if (recipe.Title == null)
            {
                throw new ArgumentNullException("Title cannot be null!");
            }
            else if (recipe.Instruction == null)
            {
                throw new ArgumentNullException("Instruction cannot be null!");
            }
            else if (recipe.User.Id <= 0)
            {
                throw new ArgumentException("UserId cannot be zero or less!");
            }

            if (recipe.Images != null && recipe.Images.Count > 0)
            {
                foreach (var i in recipe.Images)
                {
                    if (i.FileName == null)
                    {
                        throw new ArgumentNullException("FileName cannot be null!");
                    }
                }
            }

            if (recipe.IngredientLines == null)
            {
                throw new ArgumentNullException("IngredientLines cannot be null!");
            }
            else if (recipe.IngredientLines.Count == 0)
            {
                throw new ArgumentException("IngredientLines cannont be zero!");
            }
            else
            {
                foreach (var il in recipe.IngredientLines)
                {
                    if (il.Amount <= 0.0m)
                    {
                        throw new ArgumentException("Amount cannot be zero or less!");
                    }
                    else if (il.Ingredient == null)
                    {
                        throw new ArgumentNullException("Ingredient cannot be null!");
                    }
                    else if (il.Ingredient.Name == null)
                    {
                        throw new ArgumentNullException("Ingredient.Name cannot be null!");
                    }
                }
            }

            //Ensure users exists
            if (dbAccess.Users.GetByIdAsync(recipe.User.Id) == null)
            {
                throw new ArgumentException("UserId does not exist!");
            }
        }
示例#6
0
        public async Task <int> UpdateAsync(RecipeDTO recipeDTO)
        {
            Recipe recipe = await dbAccess.Recipes.GetByIdAsync(recipeDTO.Id);

            recipe.Title       = recipeDTO.Title;
            recipe.Instruction = recipeDTO.Instruction;
            recipe.RowVer      = recipeDTO.RowVer; // convert to byte array

            int updateResult = await dbAccess.Recipes.UpdateAsync(recipe);

            if (updateResult == 0)
            {
                return(0);
            }

            var existingIngredientLines = (await dbAccess.IngredientLines.FindByCondition(nameof(IngredientLine.RecipeId), recipeDTO.Id)).ToList();
            var existingImages          = (await dbAccess.Images.FindByCondition(nameof(Image.RecipeId), recipeDTO.Id)).ToList();

            // delete old ingredient lines on the recipe
            foreach (IngredientLine existingIngredientLine in existingIngredientLines)
            {
                await dbAccess.IngredientLines.DeleteAsync(existingIngredientLine.Id);
            }

            // delete old images on the recipe
            foreach (Image existingImage in existingImages)
            {
                await dbAccess.Images.DeleteAsync(existingImage.Id);
            }

            // loop thru the ingredient lines sent by the user to be inserted
            foreach (IngredientLineDTO ingredientLineDTO in recipeDTO.IngredientLines)
            {
                // check ingredient name exists
                var existingIngredientByName = await dbAccess.Ingredients.FindByCondition("Name", ingredientLineDTO.Ingredient.Name);

                IngredientLine newIngredientLine = new IngredientLine();

                if (existingIngredientByName.Any())
                {
                    // if the ingredient already exists by name, assign the existing ingredient to the ingredient line
                    newIngredientLine.IngredientId = existingIngredientByName.Single().Id;
                }
                else
                {
                    // the ingredient does not exist by name, we create it, get the new IngredientId and assign it to the ingredient line
                    Ingredient newIngredient = new Ingredient();
                    newIngredient.Name = ingredientLineDTO.Ingredient.Name;

                    var res = await dbAccess.Ingredients.AddAsync(newIngredient);

                    newIngredientLine.IngredientId = res.Id;
                }

                newIngredientLine.RecipeId    = recipe.Id;
                newIngredientLine.Amount      = ingredientLineDTO.Amount;
                newIngredientLine.MeasureUnit = (MeasureUnit)ingredientLineDTO.MeasureUnit;

                // saves the data to database
                await dbAccess.IngredientLines.AddAsync(newIngredientLine);
            }

            foreach (ImageDTO imageDTO in recipeDTO.Images)
            {
                Image image = new Image();
                image.FileName = imageDTO.FileName;
                image.RecipeId = recipe.Id;

                await dbAccess.Images.AddAsync(image);
            }

            return(updateResult);
        }
示例#7
0
        public async Task <IEnumerable <RecipeDTO> > GetAllAsync()
        {
            List <RecipeDTO> recipeList = new List <RecipeDTO>();
            var resultSet = (await dbAccess.Recipes.GetAllAsync()).ToList();

            if (resultSet == null)
            {
                throw new ArgumentNullException("No recipes were found!");
            }

            foreach (var item in resultSet)
            {
                //Convert user info to DTO
                var user = await dbAccess.Users.GetByIdAsync(item.UserId);

                UserDTO uResult = new UserDTO();
                uResult.Id       = user.Id;
                uResult.Username = user.Username;

                //Create list for ingredientlines
                List <IngredientLineDTO> ilResults = new List <IngredientLineDTO>();
                //Fetch ingredientlines in DB based on recipeId
                var ingredientLines = (await dbAccess.IngredientLines.FindByCondition(nameof(IngredientLine.RecipeId), item.Id)).ToList();
                //Loop over ingredientlines in DB, convert to DTO and add to new list of ingredientlines
                foreach (var il in ingredientLines)
                {
                    IngredientLineDTO ingredientLineDTO = new IngredientLineDTO();
                    var ingredient = await dbAccess.Ingredients.GetByIdAsync(il.IngredientId);

                    IngredientDTO ingredientDTO = new IngredientDTO();
                    ingredientDTO.Name            = ingredient.Name;
                    ingredientDTO.Id              = ingredient.Id;
                    ingredientLineDTO.Id          = il.Id;
                    ingredientLineDTO.Ingredient  = ingredientDTO;
                    ingredientLineDTO.Amount      = il.Amount;
                    ingredientLineDTO.MeasureUnit = il.MeasureUnit;

                    ilResults.Add(ingredientLineDTO);
                }

                //Create list for images
                List <ImageDTO> imResults = new List <ImageDTO>();
                //Fetch images in DB based on recipeId
                var images = (await dbAccess.Images.FindByCondition(nameof(Image.RecipeId), item.Id)).ToList();
                //Loop over images in DB, convert to DTO and add to new list of images
                foreach (var im in images)
                {
                    ImageDTO imageDTO = new ImageDTO();
                    imageDTO.Id       = im.Id;
                    imageDTO.FileName = im.FileName;

                    imResults.Add(imageDTO);
                }

                //Convert recipe to DTO and return result
                RecipeDTO result = new RecipeDTO();
                result.User            = uResult;
                result.Id              = item.Id;
                result.Title           = item.Title;
                result.Slug            = item.Slug;
                result.Instruction     = item.Instruction;
                result.CreatedAt       = item.CreatedAt;
                result.IngredientLines = ilResults;
                result.Images          = imResults;
                result.RowVer          = item.RowVer;

                recipeList.Add(result);
            }
            return(recipeList);
        }
示例#8
0
 public static RecipeDTO ConvertRecipeMeasurements(RecipeDTO recipe, ConversionType conversionType, bool reverseConversion) =>
 RecipeService.Instance.ConvertRecipeMeasurements(recipe, conversionType, reverseConversion);
示例#9
0
 public ItemViewModel(IRecipeCategoryLogic recipeCategoryLogic, INutritionCalculator data, RecipeDTO recipe)
 {
     this.data = data;
     this.recipeCategoryLogic = recipeCategoryLogic;
     if (recipe != null)
     {
         this.data            = data;
         SelectedRecipe       = recipe;
         NutritionPer100Gramm = data.CalculateNutritionPer100Gram(SelectedRecipe);
         this.CurrentCategory = recipeCategoryLogic.GetCategoryByRecipeId(SelectedRecipe.Id);
         var images = SelectedRecipe.Images;
         this.ImagesViewsCollection = new ObservableCollection <RecipeImageDTO>(images);
         var instructions = SelectedRecipe.Instruction.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
         this.CurrentRecipeInstructions = new ObservableCollection <string>(instructions);
     }
 }
示例#10
0
 public void SetSelectedRecipe(RecipeDTO selectedRecipe) =>
 RecipeService.Instance.SelectedRecipe = selectedRecipe;
示例#11
0
 public static RecipeDTO GetScaledRecipe(RecipeDTO recipe, double scale) =>
 RecipeService.Instance.ScaleRecipe(recipe, scale);
示例#12
0
 public IHttpActionResult checkIfRecipeExist(int userId, RecipeDTO recipe)
 {
     //check if recipe is allready exist in cookbook
     return(Ok(BL.RecipeBL.checkIfRecipeExist(userId, recipe)));
 }
示例#13
0
 public IHttpActionResult addRecipeToCookbook(int userId, RecipeDTO recipe)
 {
     //add recipe to cookbook
     BL.RecipeBL.AddRecipeToCookbook(userId, recipe);
     return(Ok("added successfully!"));
 }
示例#14
0
        public void TestGetByNameVeloutePatateDouceLentilleCorail()
        {
            RecipeDTO expected = new RecipeDTO()
            {
                Name        = "Velouté de patate douce et lentilles corail",
                HumanTime   = 20,
                MachineTime = 27,
                Steps       = new RecipeStepDTO[]
                {
                    new RecipeStepDTO()
                    {
                        Instruction = "Eplucher et couper la patate douce en morceau. Mettre de côté.",
                        Foods       = new FoodDTO[]
                        {
                            new FoodDTO()
                            {
                                Name = "Patate douce",
                                Mass = 400,
                            },
                        },
                    },
                    new RecipeStepDTO()
                    {
                        Instruction = "Placer la lame 'Multi Blade'. Eplucher l'oignon, le couper en 4 et le mettre dans le bol. Fermer le bouchon. Pulse 10 secondes. Racler la parroi.",
                        Foods       = new FoodDTO[]
                        {
                            new FoodDTO()
                            {
                                Name = "Oignon",
                                Mass = 200,
                            },
                        },
                    },
                    new RecipeStepDTO()
                    {
                        Instruction = "Mettre à porté de main le reste des ingrédients.",
                        Foods       = new FoodDTO[]
                        {
                            new FoodDTO()
                            {
                                Name = "Lentille corail",
                                Mass = 100,
                            },
                            new FoodDTO()
                            {
                                Name = "Curry en poudre",
                                Mass = 5,
                            },
                            new FoodDTO()
                            {
                                Name = "Purée de tomate",
                                Mass = 25,
                            },
                            new FoodDTO()
                            {
                                Name = "Lait de coco",
                                Mass = 150,
                            },
                            new FoodDTO()
                            {
                                Name = "Eau",
                                Mass = 500,
                            },
                        },
                    },
                    new RecipeStepDTO()
                    {
                        Instruction = "Remplacer la lame 'Multi Blade' par le mélangeur 'Stir Assist'. Verser l'huile d'olive sur les oignons émincés. Ouvrir le bouchon. Température : 110°C, Durée : 2 minutes, Vitesse : Intermitante.",
                        Foods       = new FoodDTO[]
                        {
                            new FoodDTO()
                            {
                                Name = "Huile d'olive",
                                Mass = 10,
                            },
                        },
                    },
                    new RecipeStepDTO()
                    {
                        Instruction = "Remplacer le mélangeur 'Stir Assist' par la lame 'Multi Blade'. Ajouter tous les ingrédients mis de côté. Fermer le bouchon. Température : 100°C, Durée : 30 minutes, Vitesse : Intermitante.",
                        Foods       = Array.Empty <FoodDTO>(),
                    },
                    new RecipeStepDTO()
                    {
                        Instruction = "Température : 0°C, Durée : 2 minutes, Vitesse : Maximale.",
                        Foods       = Array.Empty <FoodDTO>(),
                    },
                },
            };
            RecipeDTO actual = this.toTest.GetByName("Velouté de patate douce et lentilles corail");

            Assert.AreEqual(expected, actual);
        }
示例#15
0
 public void Update(RecipeDTO recipe)
 {
     throw new NotImplementedException();
 }
示例#16
0
 public RecipeDTO Insert(RecipeDTO recipe)
 {
     throw new NotImplementedException();
 }