public static void UpdateDrink(Drink drink) { using (BeeftecheeDb context = new BeeftecheeDb()) { context.Entry(drink).State = EntityState.Modified; } }
public static async Task SaveChanges() { using (BeeftecheeDb context = new BeeftecheeDb()) { await context.SaveChangesAsync(); } }
//Returns asynchronously all burgers including all the ingredients from the database public static async Task <List <Burger> > GetBurgersAsync() { using (BeeftecheeDb context = new BeeftecheeDb()) { var model = context.Burgers.Include(b => b.Bread).Include(b => b.Cheese).Include(b => b.Meat).Include(b => b.Sauce).Include(b => b.Veggie); foreach (var burger in model) { //Find and place the objects into the burger by their id burger.Meat = context.Meats.Find(burger.MeatId); burger.Bread = context.Breads.Find(burger.BreadId); burger.Sauce = context.Sauces.Find(burger.SauceId); burger.Veggie = context.Veggies.Find(burger.VeggieId); burger.Cheese = context.Cheeses.Find(burger.CheeseId); //Calculate the Total Price of the burger decimal totalPrice = burger.Bread.Price + burger.Meat.Price; totalPrice += context.Sauces.Find(burger.SauceId) == null ? 0 : context.Sauces.Find(burger.SauceId).Price; totalPrice += context.Cheeses.Find(burger.CheeseId) == null ? 0 : context.Cheeses.Find(burger.CheeseId).Price; totalPrice += context.Veggies.Find(burger.VeggieId) == null ? 0 : context.Veggies.Find(burger.VeggieId).Price; burger.Price = totalPrice; } return(await model.ToListAsync()); } }
public static async Task AddDrinkAsync(Drink drink) { using (BeeftecheeDb context = new BeeftecheeDb()) { context.Drinks.Add(drink); await context.SaveChangesAsync(); } }
//Returns asynchronously a drink with a specific ID public static async Task <Drink> FindDrinkAsync(int?id) { using (BeeftecheeDb context = new BeeftecheeDb()) { var model = context.Drinks.FindAsync(id); return(await model); } }
//Returns asynchronously a burger with a specific ID public static async Task <Burger> FindBurgerAsync(int?id) { using (BeeftecheeDb context = new BeeftecheeDb()) { var model = context.Burgers.Include(b => b.Bread).Include(b => b.Cheese).Include(b => b.Meat).Include(b => b.Sauce).Include(b => b.Veggie).SingleOrDefaultAsync(x => x.Id == id); return(await model); } }
//Returns asynchronously all the drinks from the database public static async Task <List <Drink> > GetDrinksAsync() { using (BeeftecheeDb context = new BeeftecheeDb()) { var model = from x in context.Drinks select x; return(await model.ToListAsync()); } }
//Returns the max id of the burger database public static int GetBurgerMaxId() { using (BeeftecheeDb context = new BeeftecheeDb()) { if (context.Burgers.Any()) { return(context.Burgers.Max(y => y.Id)); } return(1); } }
public async Task DeleteAsync <T>(T entity) where T : class { try { using (BeeftecheeDb context = new BeeftecheeDb()) { context.Entry(entity).State = EntityState.Deleted; await context.SaveChangesAsync(); } } catch (Exception ex) { throw new ArgumentException("Invalid Entity", ex); } }
public async Task <List <T> > GetAllAsync <T>() where T : class { try { using (BeeftecheeDb context = new BeeftecheeDb()) { DbSet <T> dbSet = context.Set <T>(); return(await dbSet.ToListAsync()); } } catch (Exception ex) { throw new ArgumentException("Invalid Entity", ex); } }