Esempio n. 1
0
        public FoodItem AddFoodItem(AddFoodItemBindingModel model)
        {
            var foodItemMacroElements = new List <FoodItemMacroElement>();
            var foodItemMicroElements = new List <FoodItemMicroElement>();

            foreach (var macroElement in model.MacroElements)
            {
                var foodItemMacroElement = new FoodItemMacroElement
                {
                    AmountInGrams = macroElement.AmountInGramsPer100Grams,
                    MacroElement  = this.context.MacroElements.FirstOrDefault(me => me.Name == macroElement.Name)
                };

                foodItemMacroElements.Add(foodItemMacroElement);
            }

            foreach (var microElement in model.MicroElements.Where(mi => mi.AmountInMilligramsPer100Grams > 0))
            {
                var foodItemMicroElement = new FoodItemMicroElement
                {
                    AmountInMilligrams = microElement.AmountInMilligramsPer100Grams,
                    MicroElement       = this.context.MicroElements.FirstOrDefault(me => me.Name == microElement.Name)
                };

                foodItemMicroElements.Add(foodItemMicroElement);
            }

            var foodItem = new FoodItem
            {
                Name                  = model.Name,
                Description           = model.Description,
                FoodItemMacroElements = foodItemMacroElements,
                FoodItemMicroElements = foodItemMicroElements
            };

            this.context.FoodItems.Add(foodItem);
            this.context.SaveChanges();

            if (model.Image != null)
            {
                var foodItemId = this.context.FoodItems.First(fi => fi.Name == model.Name).Id;

                var imageLocation = this.imagesService.CreateImage(model.Image, this.GetType().Name.Replace("Service", string.Empty), foodItemId);

                this.context.FoodItems.First(fi => fi.Id == foodItemId).ImageLocation = imageLocation;
                foodItem.ImageLocation = imageLocation;
                context.SaveChanges();
            }

            return(foodItem);
        }
Esempio n. 2
0
        public FoodItem EditFoodItem(EditFoodItemViewModel model)
        {
            var foodItem = this.context.FoodItems.Include(fi => fi.FoodItemMicroElements).ThenInclude(fime => fime.MicroElement)
                           .Include(fi => fi.FoodItemMacroElements).ThenInclude(fime => fime.MacroElement)
                           .FirstOrDefault(fi => fi.Id == model.Id);

            if (foodItem == null)
            {
                return(null);
            }

            foreach (var macroElementViewModel in model.MacroElements)
            {
                var macroElementInFoodItem = foodItem.FoodItemMacroElements.FirstOrDefault(fime =>
                                                                                           fime.MacroElement.Name == macroElementViewModel.Name);
                if (macroElementInFoodItem != null)
                {
                    macroElementInFoodItem.AmountInGrams = macroElementViewModel.AmountInGramsPer100Grams;
                }
                else
                {
                    var newFoodItemMacroElement = new FoodItemMacroElement
                    {
                        FoodItem      = foodItem,
                        MacroElement  = this.context.MacroElements.First(me => me.Name == macroElementViewModel.Name),
                        AmountInGrams = macroElementViewModel.AmountInGramsPer100Grams
                    };

                    foodItem.FoodItemMacroElements.Add(newFoodItemMacroElement);
                }
            }

            foreach (var microElementViewModel in model.MicroElements)
            {
                var microElementInFoodItem = foodItem.FoodItemMicroElements.FirstOrDefault(fime =>
                                                                                           fime.MicroElement.Name == microElementViewModel.Name);
                if (microElementInFoodItem != null)
                {
                    microElementInFoodItem.AmountInMilligrams = microElementViewModel.AmountInMilligramsPer100Grams;
                }
                else
                {
                    var newFoodItemMicroElement = new FoodItemMicroElement
                    {
                        FoodItem           = foodItem,
                        MicroElement       = this.context.MicroElements.First(me => me.Name == microElementViewModel.Name),
                        AmountInMilligrams = microElementViewModel.AmountInMilligramsPer100Grams
                    };

                    foodItem.FoodItemMicroElements.Add(newFoodItemMicroElement);
                }
            }

            foodItem.Name        = model.Name;
            foodItem.Description = model.Description;

            if (model.NewImage != null)
            {
                string newImageLocation = this.imagesService.RecreateImage(model.NewImage, this.GetType().Name.Replace("Service", string.Empty), model.ImageLocation, model.Id);
                foodItem.ImageLocation = newImageLocation;
            }

            context.SaveChanges();
            return(foodItem);
        }