public IHttpActionResult AddToCurrentUsersPlan(Meal currentMeal, string day)
        {
            var currentUsersName = RequestContext.Principal.Identity.Name;
            var id = currentMeal.Id;

            var mealPlan = db.MealPlans.FirstOrDefault(w => w.User.Email == currentUsersName) ?? new MealPlan();

            var currentUser = db.Users.First(x => x.Email == currentUsersName);
            mealPlan.User = mealPlan.User ?? currentUser;

            var meal = db.Meals.Find(id);
            var mealPlanItem = new MealPlanItem
            {
                Day = day,
                Date = DateTime.Now,
                Meal = meal
            };

            //Meals is an ICollection of MealPlanData. so create the list and add the mealPlanData that we created before to it.
            mealPlan.MealPlanItems = mealPlan.MealPlanItems ?? new List<MealPlanItem>();

            mealPlan.MealPlanItems.Add(mealPlanItem);

            db.MealPlans.AddOrUpdate(mealPlan);

            db.SaveChanges();

            return Ok(mealPlan);
        }
Ejemplo n.º 2
0
        public IHttpActionResult PostMeal(Meal meal)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var currentUsersName = RequestContext.Principal.Identity.Name;
            var currentUser = db.Users.Where(x => x.Email == currentUsersName).First();
            meal.User= currentUser;
            db.Meals.Add(meal);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = meal.Id }, meal);
        }
Ejemplo n.º 3
0
        public IHttpActionResult PutMeal(int id, Meal meal)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != meal.Id)
            {
                return BadRequest();
            }

            db.Entry(meal).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MealExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Ejemplo n.º 4
0
        public IHttpActionResult AddToCurrentUsersPlan(Meal currentMeal, string day)
        {
            var currentUsersName = RequestContext.Principal.Identity.Name;
            var id = currentMeal.Id;
            if (db.MealPlans.Where(w => w.User.Email == currentUsersName).FirstOrDefault() == null)
            {
                MealPlan mealPlan = new MealPlan();
                var meal = db.Meals.Find(id);
                var currentUser = db.Users.Where(x => x.Email == currentUsersName).First();
                mealPlan.User = currentUser;

                List<Meal> userListOfMeals = new List<Meal>();
                userListOfMeals.Add(meal);

                var mealPlanData = new MealPlanData
                {
                    Day = day,
                    Date = DateTime.Now,
                    Meals = userListOfMeals
                };

                //Meals is an ICollection of MealPlanData. so create the list and add the mealPlanData that we created before to it.
                mealPlan.MealPlanData = new List<MealPlanData>()
                {
                    mealPlanData
                };

                db.MealPlans.Add(mealPlan);
            }
            else
            {

                var mealPlan = db.MealPlans.Where(w => w.User.Email == currentUsersName).First();

                //if(mealPlan.User == null)
                //{
                //    mealPlan.User = db.Users.Where(u => u.UserName.Equals(currentUsersName)).FirstOrDefault();
                //    db.SaveChanges();
                //};

                var mondayMeals = mealPlan.MealPlanData.Where(m => m.Day.Equals(day, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if(mondayMeals != null)
                {
                    if (mondayMeals.Meals != null)
                    {
                        mondayMeals.Meals.Add(currentMeal);
                    }
                    else
                    {
                        mondayMeals.Meals = new List<Meal>
                        {
                            currentMeal
                        };
                    }
                }
                else
                {
                   var mealPlanData = new MealPlanData
                   {
                       Day = day,
                       Date = DateTime.Now,
                       Meals = new List<Meal>{
                           currentMeal
                       }
                   };

                   mealPlan.MealPlanData.Add(mealPlanData);
                }
            }

            db.SaveChanges();

            return Ok();
        }