Ejemplo n.º 1
0
        public HttpResponseMessage PostFoodIntake(FoodIntake item)
        {
            var errors = CheckErrors(ModelState.Values.ToList());

            if (errors.Count == 0)
            {
                try
                {
                    item.Date           = DateTime.Now;
                    item.UserId         = Request.GetOwinContext().Authentication.User.Identity.GetUserId();
                    item.FoodPosibility = null;

                    _db.FoodIntakes.Add(item);
                    _db.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.Accepted));
                }
                catch
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError));
                }
            }
            else
            {
                return(Request.CreateResponse <List <string> >(HttpStatusCode.BadRequest, errors));
            }
        }
Ejemplo n.º 2
0
        public ActionResult DeleteConfirmed(int id)
        {
            FoodIntake foodIntake = db.FoodIntakes.Find(id);

            db.FoodIntakes.Remove(foodIntake);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 3
0
 public ActionResult Edit([Bind(Include = "Food,FoodType")] FoodIntake foodIntake)
 {
     if (ModelState.IsValid)
     {
         db.Entry(foodIntake).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(foodIntake));
 }
Ejemplo n.º 4
0
 public void ChangeIntake(FoodIntake item, double newValue)
 {
     FoodIntakes.Remove(item);
     FoodIntakes.Add(new FoodIntake
     {
         Food   = item.Food,
         Intake = newValue
     });
     RecalculateNutrients();
 }
Ejemplo n.º 5
0
        public ActionResult Create([Bind(Include = "Food,FoodType")] FoodIntake foodIntake)
        {
            if (ModelState.IsValid)
            {
                db.FoodIntakes.Add(foodIntake);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.Food = new SelectList(db.FoodIntakes, "Food", "FoodType", foodIntake.Food);

            return(View(foodIntake));
        }
Ejemplo n.º 6
0
        // GET: FoodIntakes/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            FoodIntake foodIntake = db.FoodIntakes.Find(id);

            if (foodIntake == null)
            {
                return(HttpNotFound());
            }
            return(View(foodIntake));
        }
Ejemplo n.º 7
0
        public bool AddFoodIntake(FoodIntake item)
        {
            var existing = FoodIntakes.FirstOrDefault(fi => fi.Food.Id == item.Food.Id);

            if (existing != null)
            {
                return(false);
                //existing.Intake = Math.Min(4, existing.Intake + item.Intake);
            }
            else
            {
                FoodIntakes.Add(item);
            }
            RecalculateNutrients();
            return(true);
        }
Ejemplo n.º 8
0
        public HttpResponseMessage UpdateFoodIntake(FoodIntake item)
        {
            var errors = CheckErrors(ModelState.Values.ToList());

            if (errors.Count == 0)
            {
                try
                {
                    _db.FoodIntakes.AddOrUpdate(item);
                    _db.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.Accepted));
                }
                catch
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError));
                }
            }
            else
            {
                return(Request.CreateResponse <List <string> >(HttpStatusCode.BadRequest, errors));
            }
        }