Exemple #1
0
 public void AddFood(params Food[] food)
 {
     if (food.Length < 1) return;
     using (var context = new HealthContext())
     {
         context.Foods.AddOrUpdateCustom(f => f.Name, food);
         context.SaveChanges();
     }
 }
Exemple #2
0
 public void AddDay()
 {
     using (var context = new HealthContext())
     {
         var now = DateTime.UtcNow;
         var day = new Day
         {
             Created = new DateTime(now.Year, now.Month, now.Day)
         };
         context.Days.Add(day);
         context.SaveChanges();
     }
 }
Exemple #3
0
 public void AddMealEntry(string foodName, int calories, int mealId)
 {
     if (calories < 1) throw new Exception("calories value is invalid");
     using (var context = new HealthContext())
     {
         var food = context.Foods.FirstOrDefault(f => f.Name == foodName);
         var mealEntry = new MealEntry
         {
             Calories = calories,
             Food = food,
             MealId = mealId
         };
         context.MealEntries.Add(mealEntry);
         context.SaveChanges();
     }
 }
Exemple #4
0
 public void AddMeal(MealModel mealModel)
 {
     using (var context = new HealthContext())
     {
         var day = context.Days.FirstOrDefault(d => d.Created == mealModel.Date);
         var meal = new Meal
         {
             DayId = day.Id,
             MealNumber = mealModel.MealNumber,
             MealEntries = mealModel.MealEntries
         };
         context.Add(meal);
         context.MealEntries.AddRange(meal.MealEntries);
         context.SaveChanges();
     }
 }
Exemple #5
0
 public void DeleteDay(int id)
 {
     using (var context = new HealthContext())
     {
         var day = context.Days.Include(d => d.Meals).ThenInclude(m => m.MealEntries).FirstOrDefault(d => d.Id == id);
         if (day == null) throw new NullReferenceException("The day provided does not exist in the database");
         day.Meals.ToList().ForEach(m => context.MealEntries.RemoveRange(m.MealEntries));
         context.Meals.RemoveRange(day.Meals);
         context.Days.Remove(day);
         context.SaveChanges();
     }
 }
Exemple #6
0
 public void ClearDay()
 {
     using (var context = new HealthContext())
     {
         var day = context.Days.OrderByDescending(d => d.Created).FirstOrDefault();
         if (day == null) throw new NullReferenceException("The day provided does not exist in the database");
         var meals = context.Meals.Include(m => m.MealEntries).Where(m => m.DayId == day.Id).ToList();
         meals.ForEach(m => context.MealEntries.RemoveRange(m.MealEntries));
         context.Meals.RemoveRange(meals);
         context.SaveChanges();
     }
 }