/// <summary> /// Function create meal from given dishes /// in case of eating single component it is needed to create virtual dish connected by Conncetor list in dish with component, but without adding it to Context /// Dishes should be given without connectors but when they are virtual it should ahve Connector list /// not tested cause UI not prepared YET /// </summary> /// <param name="userId"></param> /// <param name="weigth"></param> /// <param name="mealType"></param> /// <param name="dishesList"></param> /// <returns></returns> internal static bool AddMeal(string userLogin, long weigth, Enums.MealType mealType, List <Dish> dishesList) { int userId; try { userId = GetUserByUserName(userLogin); } catch (ArgumentException e) { Console.Write("Missing user name" + e); return(false); } catch (Exception e) { Console.Write("User not exist" + e); return(false); } if (userId == 0 || weigth == 0 || dishesList.Count == 0) { return(false); } AteDatabase entity = new AteDatabase(); try { Meal meal = new Meal() { FKUserId = userId, Weigth = weigth, MealType = (short)mealType, MealDate = DateTime.Now }; List <Dish> tempDish = new List <Dish>(); foreach (Dish dish in dishesList) { if (dish.Connectors.Count == 0) { tempDish.Add(entity.Dishes.Where(w => w.DishId == dish.DishId).Single()); } else { tempDish.Add(dish); } } foreach (var dish in tempDish) { foreach (Connector con in dish.Connectors) { meal.Connectors.Add(new Connector() { FK_ComponentId = con.FK_ComponentId, ComponentWeigth = con.ComponentWeigth }); } } entity.Meals.Add(meal); entity.SaveChanges(); } catch (Exception e) { Console.Write("AddMeal error" + e); return(false); } return(true); }
public async Task <string> GetStatistics(string UserLogin, int DaysNum, Enums.MealType KindOfMeal) { return(await Task.Run(() => HomeController.GetStatistics(UserLogin, DaysNum, KindOfMeal))); }
/// <summary> /// Fuction create statistics object of <see cref="MealNutritionalValues">MealNutritionalValues</see> eaten in given time peroid /// </summary> /// <param name="userLogin"></param> /// <param name="daysNum"></param> /// <param name="kindOfMeal"></param> /// <returns></returns> internal static string GetStatistics(string userLogin, int daysNum, Enums.MealType kindOfMeal) { if (daysNum == 0) { return(String.Empty); } int userId; try { userId = GetUserByUserName(userLogin); } catch (ArgumentException e) { Console.Write("Missing user name" + e); return(String.Empty); } catch (Exception e) { Console.Write("User not exist" + e); return(String.Empty); } AteDatabase entity = new AteDatabase(); Statistics statistics = new Statistics(); User user = null; try { user = entity.Users.Single(s => s.UserId == userId); if (user.Weight.HasValue && user.Growth.HasValue) { double userGrowth = user.Growth.Value / 100.0; statistics.BMI = 1.0 * user.Weight.Value / (userGrowth * userGrowth); } } catch (Exception e) { Console.Write("Error in getting user" + e); } try { ///DONE: <see cref="Statistics.PropperDayValues"></see> have to bo read from db and set!!! StatisticData statData = entity.StatisticDatas.Where(w => w.WeightFrom <user.Weight && w.WeigthTo> user.Weight && w.Gender == user.Gender).Single(); statistics.PropperDayValues = new MealNutritionalValues() { Calories = statData.DayKcal, Proteins = statData.DayProtein, Carbohydrates = statData.DayCarnohydrates, Fats = statData.DayFats, MealType = Enums.MealType.Wszystkie }; } catch (Exception e) { Console.Write("Statistics Propper Day Values not found" + e); } try { List <Meal> meals; DateTime statisticTime = DateTime.Now.AddDays(-1 * daysNum); if (kindOfMeal == Enums.MealType.Wszystkie) { meals = entity.Meals.Where(w => w.FKUserId == userId && w.MealDate > statisticTime).ToList(); } else { meals = entity.Meals.Where(w => w.FKUserId == userId && w.MealDate > statisticTime && w.MealType == (short)kindOfMeal).ToList(); } if (meals.Count > 0) { for (int i = daysNum - 1; i >= 0; i--) { DateTime statisticDate = DateTime.Now.AddDays(-1 * i).Date; List <Meal> tempDayMeals = meals.Where(w => w.MealDate == statisticDate).ToList(); statistics.DayFoods.Add(new DayFood() { MealDate = statisticDate, }); foreach (Meal m in tempDayMeals) { double fats = 0, kcal = 0, prot = 0, carb = 0; foreach (Connector con in m.Connectors) { kcal += con.ComponentWeigth.GetValueOrDefault() / 100.0 * con.Component.CaloriesIn100g; fats += con.ComponentWeigth.GetValueOrDefault() / 100.0 * con.Component.FatsIn100g; prot += con.ComponentWeigth.GetValueOrDefault() / 100.0 * con.Component.ProteinIn100g; carb += con.ComponentWeigth.GetValueOrDefault() / 100.0 * con.Component.CarbohydratesIn100g; } statistics.DayFoods.Last().MealNutritionalVal.Add(new MealNutritionalValues() { MealType = (Enums.MealType)m.MealType, Calories = kcal, Carbohydrates = carb, Proteins = prot, Fats = fats }); } } return(new JavaScriptSerializer().Serialize(statistics)); } } catch (Exception) { } return(new JavaScriptSerializer().Serialize(statistics)); }