public IHttpActionResult PostMealPlan(MealPlan mealPlan)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.MealPlans.Add(mealPlan);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = mealPlan.Id }, mealPlan);
        }
        public IHttpActionResult PutMealPlan(int id, MealPlan mealPlan)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

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

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

            return StatusCode(HttpStatusCode.NoContent);
        }
Exemplo n.º 3
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();
        }