/// <summary> /// Subtract from quantity 1 /// </summary> /// <param name="food"></param> public void minusAmountFood(Food food) { Food food2 = new Food(); using (var db = new BE.AppContext()) { foreach (var item in db.Foods) { //search the food and if exsit sub 1 to amount if (item.ID.Equals(food.ID) && item.Date.Equals(food.Date) && item.Description.Equals(food.Description)) { food2 = item; int amount = int.Parse(food2.Amount); amount--; food2.Amount = amount.ToString(); db.Foods.Remove(item); } } db.SaveChanges(); if (int.Parse(food2.Amount) > 0)//when the amount greater than 0 { db.Foods.Add(food2); } db.SaveChanges(); } }
/// <summary> ///check if user exist in data base /// </summary> /// <param name="id"></param> /// <returns></returns> public bool CheckId(String id) { using (var db = new BE.AppContext()) { foreach (var item in db.Users) { if (item.Id == id) { return(true); } } } return(false); }
/// <summary> ///check if the user is exsit /// </summary> /// <param name="client"></param> /// <returns></returns> public String CheckUser(BE.Client client) { using (var db = new BE.AppContext()) { foreach (var item in db.Users) { if (item.Id == client.Id && item.Password == client.password) { return(item.Id); } } } return(null); }
/// <summary> ///add user to db of users /// </summary> /// <param name="user"></param> public void addUser(User user) { if (CheckId(user.Id))//if the user exist on database { throw new Exception("The user exist on system"); } else { using (var db = new BE.AppContext()) { db.Users.Add(user); db.SaveChanges(); } } }
/// <summary> ///return list of food according to meal , date and Id /// </summary> /// <param name="date"></param> /// <param name="id"></param> /// <param name="meal"></param> /// <returns></returns> public List <BE.Food> ListOfFoodsInDate(DateTime date, String id, String meal) { List <BE.Food> lst = new List <BE.Food>(); using (var db = new BE.AppContext()) { foreach (var item in db.Foods) { //when id and meal and Date equal to this(item) food if (item.ID.Equals(id) && item.Meal.Equals(meal) && item.Date.Date.Equals(date.Date)) { lst.Add((Food)item); } } } return(lst); }
/// <summary> ///add food to the data base /// </summary> /// <param name="food"></param> public void AddFood(BE.Food food) { int count = 0; bool flag = false; String xmlfood = XmlFood(food.Description); food.ndbo = GetKeyOfFood(xmlfood, food.Description); BE.Food food2 = new BE.Food(); using (var db = new BE.AppContext()) { count = db.Foods.Count(); //if the food exist to this user- upadte the amount var qury1 = (from f in db.Foods where f.ID.Equals(food.ID) && f.Date.Equals(food.Date) && f.Description.Equals(food.Description) select f); foreach (var item in qury1) { food2 = item; int amount = int.Parse(food2.Amount); amount++; food2.Amount = amount.ToString(); db.Foods.Remove(item); flag = true; } db.SaveChanges(); if (flag) { db.Foods.Add(food2); } //if food not exist - add to db if (!flag && food.ndbo != null) { food.key = count + 1; db.Foods.Add(food); } db.SaveChanges(); } }
/// <summary> ///Sum of all components /// </summary> /// <param name="id"></param> /// <param name="date"></param> /// <param name="time"></param> /// <returns></returns> public Component SumOfComponents(String id, DateTime date, String time) { Component c = new Component(); if (time == "day")//sum according to day { using (var db = new BE.AppContext()) { foreach (var item in db.Foods) { if (item.ID.Equals(id) && item.Date.Date.Equals(date.Date))//check id and date that equal to Now { Component c1 = new Component(); c1 = ReturnComponent(item.ndbo, int.Parse(item.Amount)); c.ndbo = c1.ndbo; c.Carbohydrate += c1.Carbohydrate; //c.Energy += c1.Energy; c.Fats += c1.Fats; c.Fiber += c1.Fiber; c.Protien += c1.Protien; c.Sugar += c1.Sugar; c.Iron += c1.Iron; c.Cholesterol += c1.Cholesterol; } } } } else if (time == "month")//sum according to month { using (var db = new BE.AppContext()) { //check this month and this year and id var qury1 = (from f in db.Foods where f.ID.Equals(id) && f.Date.Month.Equals(date.Month) && f.Date.Year.Equals(date.Year) select f); foreach (var item in qury1) { Component c1 = new Component(); c1 = ReturnComponent(item.ndbo, int.Parse(item.Amount)); c.ndbo = c1.ndbo; c.Carbohydrate += c1.Carbohydrate; //c.Energy += c1.Energy; c.Fats += c1.Fats; c.Fiber += c1.Fiber; c.Protien += c1.Protien; c.Sugar += c1.Sugar; c.Cholesterol += c1.Cholesterol; c.Iron += c1.Iron; } } } else if (time == "week")//sum according to week { using (var db = new BE.AppContext()) { List <BE.Food> qury1 = new List <Food>(); //check this week and id and the date in the week foreach (var item in db.Foods) { if (item.ID.Equals(id) && dateInWeek(item.Date, date)) { qury1.Add(item); } } foreach (var item in qury1) { Component c1 = new Component(); c1 = ReturnComponent(item.ndbo, int.Parse(item.Amount)); c.ndbo = c1.ndbo; c.Carbohydrate += c1.Carbohydrate; // c.Energy += c1.Energy; c.Fats += c1.Fats; c.Fiber += c1.Fiber; c.Protien += c1.Protien; c.Sugar += c1.Sugar; c.Cholesterol += c1.Cholesterol; c.Iron += c1.Iron; } } } else if (time == "year")//sum according to month { using (var db = new BE.AppContext()) { //check this month and this year and id var qury1 = (from f in db.Foods where f.ID.Equals(id) && f.Date.Year.Equals(date.Year) select f); foreach (var item in qury1) { Component c1 = new Component(); c1 = ReturnComponent(item.ndbo, int.Parse(item.Amount)); c.ndbo = c1.ndbo; c.Carbohydrate += c1.Carbohydrate; c.Iron += c1.Iron; c.Fats += c1.Fats; c.Fiber += c1.Fiber; c.Protien += c1.Protien; c.Sugar += c1.Sugar; c.Cholesterol += c1.Cholesterol; // c.Water += c1.Water; } } } return(c); }