// получение списка категорий из бд public List<Category> GetCategories() { using (_context = new Context()) { return _context.Categories.ToList(); } }
// добавление нового блюда в бд public void AddDish(Dish dish) { using (_context = new Context()) { if (dish.Category != null) _context.Categories.Attach(dish.Category); _context.Dishes.Add(dish); _context.SaveChanges(); } }
// создание и инициализация бд из текстовых файлов public DataBaseRepository() { using (_context = new Context()) { if (!_context.Database.Exists()) { _context.Database.Create(); var categories = LoadCategoriesFromFile(); _context.Categories.AddRange(categories); var dishes = LoadDishesFromFile(); _context.Dishes.AddRange(dishes); var history = LoadHistoryFromFile(); _context.EatingHistory.AddRange(history); _context.SaveChanges(); } } }
// добавление записи в таблицу истории public void AddEatingHistoryItem(Dish dish, float quantity) { EatingHistoryItem eatingHistoryItem = new EatingHistoryItem { Date = DateTime.Now.Date, Dish = dish, Quantity = quantity, Calories = dish.Calories / 100 * quantity, Fats = dish.Fats / 100 * quantity, Proteins = dish.Proteins / 100 * quantity, Carbohydrates = dish.Carbohydrates / 100 * quantity }; using (_context = new Context()) { _context.Dishes.Attach(dish); _context.EatingHistory.Add(eatingHistoryItem); _context.SaveChanges(); } }
// получение истории за конкретную дату public List<EatingHistoryItem> GetDailyHistoryData(DateTime date) { using (_context = new Context()) { var request = (from eh in _context.EatingHistory where eh.Date == date.Date select eh).ToList(); var request1 = (from eh in _context.EatingHistory where eh.Date == date.Date join d in _context.Dishes on eh.Dish equals d select d).ToList(); return request; } }
// удаление записи из таблицы истории public void RemoveEatingHistoryItem(EatingHistoryItem item) { using (_context = new Context()) { var request = from eh in _context.EatingHistory where eh.ID == item.ID select eh; _context.EatingHistory.Remove(request.Single()); _context.SaveChanges(); } }
// получение списка блюд конкретной категории из бд public List<Dish> GetDishes(Category category) { using (_context = new Context()) { return (from d in _context.Dishes where d.Category.Id == category.Id select d).ToList(); } }
// получение списка блюд из бд public List<Dish> GetDishes() { using (_context = new Context()) { return _context.Dishes.ToList(); } }