Beispiel #1
0
        public ActionResult Edit(int FoodId, FoodViewModel editedFood)
        {
            try
            {
                // TODO: Add update logic here
                //var food = foods.First(f => f.FoodId == FoodId);
                using (var db = new FoodLogContext())
                {
                    var food = db.Foods.First(f => f.FoodId == FoodId);
                    food.Name           = editedFood.Food.Name;
                    food.Calories       = editedFood.Food.Calories;
                    food.GramsOfCarb    = editedFood.Food.GramsOfCarb;
                    food.GramsOfFat     = editedFood.Food.GramsOfFat;
                    food.GramsOfProtein = editedFood.Food.GramsOfProtein;
                    food.FoodType       = food.FoodType.FoodTypes.First(t => t.Name == editedFood.SelectedFoodType);
                    db.SaveChanges();
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
Beispiel #2
0
        public ActionResult Create(FoodViewModel newFood)
        {
            try
            {
                // TODO: Add insert logic here
                var food = new FoodModel()
                {
                    Name           = newFood.Food.Name,
                    Calories       = newFood.Food.Calories,
                    GramsOfCarb    = newFood.Food.GramsOfCarb,
                    GramsOfFat     = newFood.Food.GramsOfFat,
                    GramsOfProtein = newFood.Food.GramsOfProtein
                };
                food.FoodType = food.FoodType.FoodTypes.First(t => t.Name == newFood.SelectedFoodType);

                using (var db = new FoodLogContext())
                {
                    db.Foods.Add(food);
                    db.SaveChanges();
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
 public async Task UpdateAsync(T item)
 {
     using (var _ctx = new FoodLogContext())
     {
         _ctx.Entry(item).State = EntityState.Modified;
         await _ctx.SaveChangesAsync();
     }
 }
 public void Update(T item)
 {
     using (var _ctx = new FoodLogContext())
     {
         _ctx.Entry(item).State = EntityState.Modified;
         _ctx.SaveChanges();
     }
 }
 public void Create(T item)
 {
     using (var _ctx = new FoodLogContext())
     {
         _ctx.Set <T>().Add(item);
         _ctx.SaveChanges();
     }
 }
        public async Task CreateAsync(T item)
        {
            using (var _ctx = new FoodLogContext())
            {
                await _context.Set <T>().AddAsync(item);

                await _context.SaveChangesAsync();
            }
        }
Beispiel #7
0
        //List<FoodModel> foods = new List<FoodModel>() {new FoodModel()
        //{
        //    FoodId = 1,
        //    Name = "Black Beans",
        //    ServingSize = 1,
        //    Calories = 227,
        //    GramsOfProtein = 15.2,
        //    GramsOfFat = .9,
        //    GramsOfCarb = 40.8,
        //    FoodType = FoodTypeModel.GetFoodTypeList().First(t => t.Name == "Legumes")
        //} };



        // GET: Food
        public ActionResult Index()
        {
            List <FoodModel> foods = new List <FoodModel>();

            using (var db = new FoodLogContext())
            {
                foods.AddRange(db.Foods.ToList());
            }
            return(View(foods));
        }
Beispiel #8
0
 // GET: Food/Delete/5
 public ActionResult Delete(int FoodId)
 {
     using (var db = new FoodLogContext())
     {
         db.Foods.Remove(db.Foods.First(food => food.FoodId == FoodId));
         db.SaveChanges();
     }
     //foods.Remove(foods.First(food => food.FoodId == FoodId));
     return(RedirectToAction("Index"));
 }
Beispiel #9
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            var ctx = new FoodLogContext();

            ctx.Database.EnsureDeleted();
            ctx.Database.EnsureCreated();
            Console.WriteLine("Done!");

/*            FoodsBusinessObject _fbo = new FoodsBusinessObject();
 *
 *          var regex = new Regex("^" + "", RegexOptions.IgnoreCase);
 *          var regex2 = new Regex("^" + "b", RegexOptions.IgnoreCase);
 *          var res2 = await _fbo.FilterAsync(x => regex.IsMatch(x.Name));
 *          var res3 = await _fbo.FilterAsync(x => regex2.IsMatch(x.Name));*/
        }
Beispiel #10
0
        public ActionResult Delete(int FoodId, FormCollection collection)
        {
            try
            {
                using (var db = new FoodLogContext())
                {
                    db.Foods.Remove(db.Foods.First(food => food.FoodId == FoodId));
                    db.SaveChanges();
                }

                // TODO: Add delete logic here
                //foods.Remove(foods.First(food => food.FoodId == FoodId));
                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
Beispiel #11
0
        private IEnumerable <SelectListItem> GetFoodTypeSelectList()
        {
            var selectList = new List <SelectListItem>();

            using (var db = new FoodLogContext())
            {
                foreach (var foodType in db.FoodTypes.ToList())
                {
                    selectList.Add(new SelectListItem()
                    {
                        Value = foodType.Name, Text = foodType.Name
                    });
                }
            }
            //foreach (var foodType in FoodTypeModel.GetFoodTypeList())
            //{
            //    selectList.Add(new SelectListItem() { Value = foodType.Name, Text = foodType.Name });
            //}
            return(selectList);
        }
Beispiel #12
0
        // GET: Food/Edit/5
        public ActionResult Edit(int FoodId)
        {
            //return View(new FoodViewModel()
            //{
            //    Food = foods.First(food => food.FoodId == FoodId),
            //    SelectedFoodType = string.Empty,
            //    FoodTypesSelectList = GetFoodTypeSelectList()
            //});

            FoodModel food;

            using (var db = new FoodLogContext())
            {
                food = db.Foods.First(t => t.FoodId == FoodId);
            }

            return(View(new FoodViewModel()
            {
                Food = food,
                SelectedFoodType = string.Empty,
                FoodTypesSelectList = GetFoodTypeSelectList()
            }));
        }
 public BaseDataAccessObject()
 {
     _context = new FoodLogContext();
 }