public bool Exists(Category category)
 {
     using (var db = new DBContext())
     {
         return db.Categories.Where(x => x.Name == category.Name).Count() > 0;
     }
 }
 public List<Category> GetCategories()
 {
     using (var db = new DBContext())
     {
         return db.Categories.ToList();
     }
 }
 public Expense GetExpenseById(int id)
 {
     using (var db = new DBContext())
     {
         return db.Expenses.Single(x => x.ExpenseId == id);
     }
 }
 public List<Expense> GetExpenses(DateTime from, DateTime to)
 {
     var db = new DBContext();
     return db.Expenses
         .Where(expense => DbFunctions.TruncateTime(expense.Date) >= from 
             && DbFunctions.TruncateTime(expense.Date) <= to)
         .ToList();
 }
        public int Add(Expense expense)
        {
            using (var db = new DBContext())
            {
                db.Categories.Attach(expense.Category);
                db.Expenses.Add(expense);
                db.SaveChanges();

                return expense.ExpenseId;
            }
        }
 public void Add(Category category)
 {
     using (var db = new DBContext())
     {
         try
         {
             db.Categories.Add(category);
             db.SaveChanges();
         }
         catch (System.Data.Entity.Infrastructure.DbUpdateException)
         {
             throw new Repositories.Exceptions.CategoryAlreadyExistsException();
         }
     }
 }
        public Dictionary<string, double> GetExpenseSummaryByCategory(DateTime from, DateTime to)
        {
            Dictionary<string, double> expensesByCategory = new Dictionary<string,double>();
            var db = new DBContext();
            foreach (var category in db.Categories)
            {
                double summary = db.Expenses
                    .Where(expense => DbFunctions.TruncateTime(expense.Date) >= from 
                        && DbFunctions.TruncateTime(expense.Date) <= to)
                    .Select(expense => expense.Amount)
                    .Sum();
                expensesByCategory.Add(category.Name, summary);
            }

            return expensesByCategory;
        }