Example #1
0
        public IHttpActionResult EditDiet(DietAndMealCollection dietAndMealCollection)
        {
            var dm   = dietAndMealCollection.DietModel;
            var mcm  = dietAndMealCollection.MealCollectionModel;
            var user = UserViewModel.GetCurrentUser();

            if (dm.User == user.Id)
            {
                var m = new MealCollectionModel();
                foreach (var meal in mcm)
                {
                    m = _db.MealCollection.Find(meal);
                    if (m != null)
                    {
                        m.Amount = meal.Amount;
                        m.Day    = meal.Day;
                        m.FoodId = meal.FoodId;
                        m.Edible = meal.Edible;
                        m.Meal   = meal.Meal;
                        m.Name   = meal.Name;
                        m.WeekNo = meal.WeekNo;
                    }
                    else
                    {
                        _db.MealCollection.Add(meal);
                    }
                }
                _db.Entry(m).State = EntityState.Modified;
                _db.SaveChanges();
                return(Ok());
            }
            return(BadRequest("Not correct user"));
        }
Example #2
0
        public IHttpActionResult GetDiet(string dietId)
        {
            var mealCollectionModels = _db.MealCollection.Where(x => x.DietId == dietId).ToList();
            var damc = new DietAndMealCollection
            {
                DietModel           = _db.Diet.Find(dietId),
                MealCollectionModel = mealCollectionModels
            };

            return(Ok(damc));
        }
Example #3
0
        public IHttpActionResult PostDiet(DietAndMealCollection dietAndMealCollection)
        {
            var dm     = dietAndMealCollection.DietModel;
            var mcm    = dietAndMealCollection.MealCollectionModel;
            var user   = UserViewModel.GetCurrentUser();
            var dietId = Guid.NewGuid().ToString();

            dm.Id     = dietId;
            dm.Author = user.Name;
            dm.User   = user.Id;

            _db.Diet.Add(dm);
            foreach (var meal in mcm.Where(x => !string.IsNullOrEmpty(x.Name)))
            {
                var mealId = Guid.NewGuid().ToString();
                meal.DietId = dietId;
                meal.Id     = mealId;
                _db.MealCollection.Add(meal);
            }
            _db.SaveChanges();
            return(Ok(dietId));
        }