Пример #1
0
        public void UpdateChangedProperties(SetOfFood setOfFood, out bool result, out string message)
        {
            // Header
            setOfFood = CalculateNutrition(setOfFood);
            _setOfFoodRepository.UpdateChangedProperties(setOfFood.Id, setOfFood, out result, out message);

            // Detail
            var addedDetails   = setOfFood.FoodsInSets.Where(x => x.Id == 0).ToList();              // new details added
            var updatedDetails = setOfFood.FoodsInSets.Where(x => x.Id != 0).ToList();              // get updated details
            var existedDetails = _foodsInSetRepository.FindAll(x => x.SetOfFoodId == setOfFood.Id); // existed details

            setOfFood.FoodsInSets.Clear();

            foreach (var detail in updatedDetails)
            {
                _foodsInSetRepository.UpdateChangedProperties(detail.Id, detail, out result, out message);
            }

            foreach (var detail in addedDetails)
            {
                detail.SetOfFoodId = setOfFood.Id;
                _foodsInSetRepository.Add(detail, out result, out message);
            }

            _foodsInSetRepository.RemoveMultiple(existedDetails.Except(updatedDetails).ToList(), out result, out message);

            if (result)
            {
                SaveChanges();
            }
            else
            {
                Dispose();
            }
        }
Пример #2
0
 public void Add(SetOfFood setOfFood, out bool result, out string message)
 {
     setOfFood = CalculateNutrition(setOfFood);
     _setOfFoodRepository.Add(setOfFood, out result, out message);
     if (result)
     {
         SaveChanges();
     }
     else
     {
         Dispose();
     }
 }
Пример #3
0
        public SetOfFood CalculateNutrition(SetOfFood setOfFood)
        {
            if (setOfFood.FoodsInSets.Any())
            {
                foreach (var foodInSet in setOfFood.FoodsInSets)
                {
                    var food = _foodRepository.FindById(foodInSet.FoodId);
                    setOfFood.Fat          += food.FatPerUnit * foodInSet.Quantity;
                    setOfFood.SaturatedFat += food.SaturatedFatPerUnit * foodInSet.Quantity;
                    setOfFood.Carb         += food.CarbPerUnit * foodInSet.Quantity;
                    setOfFood.Protein      += food.ProteinPerUnit * foodInSet.Quantity;
                    setOfFood.Kcal         += food.KcalPerUnit * foodInSet.Quantity;
                }
            }

            return(setOfFood);
        }
Пример #4
0
        public IActionResult SaveEntity(SetOfFood entity, long[] listFoodIds, int[] listQuantities)
        {
            bool   result    = true;
            string message   = string.Empty;
            var    userEmail = User.GetSpecificClaim("Email");

            if (ModelState.IsValid)
            {
                entity.UserModified = userEmail;

                if (listFoodIds.Count() > 0)
                {
                    int i = 0;
                    foreach (var foodId in listFoodIds)
                    {
                        var foodInSet = new FoodsInSet()
                        {
                            FoodId = foodId, Quantity = listQuantities[i], Active = true, UserCreated = userEmail, UserModified = userEmail
                        };
                        entity.FoodsInSets.Add(foodInSet);
                        i++;
                    }
                }

                if (entity.Id == 0)
                {
                    entity.UserCreated = userEmail;
                    _setOfFoodService.Add(entity, out result, out message);
                }
                else
                {
                    _setOfFoodService.UpdateChangedProperties(entity, out result, out message);
                }

                return(new OkObjectResult(new GenericResult(result, message)));
            }
            else
            {
                List <string> listErrors  = ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage).ToList();
                string        errorString = String.Join("\n", listErrors.ToArray());
                return(new OkObjectResult(new GenericResult(false, errorString)));
            }
        }