コード例 #1
0
ファイル: EfBusinessLayer.cs プロジェクト: Sharpiro/Health
 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();
     }
 }
コード例 #2
0
ファイル: EfBusinessLayer.cs プロジェクト: Sharpiro/Health
 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();
     }
 }
コード例 #3
0
ファイル: EfBusinessLayer.cs プロジェクト: Sharpiro/Health
 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();
     }
 }
コード例 #4
0
ファイル: EfBusinessLayer.cs プロジェクト: Sharpiro/Health
 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();
     }
 }
コード例 #5
0
ファイル: EfBusinessLayer.cs プロジェクト: Sharpiro/Health
 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();
     }
 }
コード例 #6
0
ファイル: EfBusinessLayer.cs プロジェクト: Sharpiro/Health
 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();
     }
 }