public bool AddSandwishInMenu(Sandwich sandwishToUse) { if (sandwishToUse == null) { throw new ArgumentNullException(nameof(sandwishToUse)); } if (sandwishToUse.SandwichId != 0) { throw new Exception(nameof(sandwishToUse)); } if (String.IsNullOrEmpty(sandwishToUse.Name) || String.IsNullOrWhiteSpace(sandwishToUse.Name)) { throw new Exception(nameof(sandwishToUse)); } using (var ctx = new SandwishContext()) { if (ctx.Sandwiches.Any(x => x.Name == sandwishToUse.Name)) { throw new Exception(nameof(sandwishToUse)); } ctx.Sandwiches.Add(sandwishToUse); ctx.SaveChanges(); } return(true); }
public List <Ingredient> GetIngredients() { using (var ctx = new SandwishContext()) { return(ctx.ingredients.ToList()); } }
public List <Sandwich> GetSandwishes() { using (var ctx = new SandwishContext()) { return(ctx.Sandwiches.Include(x => x.Ingredients).ToList()); } }
public bool AddIngredientInStock(Ingredient ingredientToAdd) { if (ingredientToAdd == null) { throw new ArgumentNullException(nameof(ingredientToAdd)); } if (ingredientToAdd.IngredientId != 0) { throw new Exception(nameof(ingredientToAdd)); } if (String.IsNullOrEmpty(ingredientToAdd.Name) || String.IsNullOrWhiteSpace(ingredientToAdd.Name)) { throw new Exception(nameof(ingredientToAdd)); } using (var ctx = new SandwishContext()) { if (ctx.ingredients.Any(x => x.Name == ingredientToAdd.Name)) { throw new Exception(nameof(ingredientToAdd)); } ctx.ingredients.Add(ingredientToAdd); ctx.SaveChanges(); } return(true); }
public List <Sandwich> GetSandwishes() { using (var ctx = new SandwishContext()) { return(ctx.Sandwiches.ToList()); } }
public Ingredient GetIngredient(int id) { if (id == 0) { throw new ArgumentNullException(nameof(id)); } using (var ctx = new SandwishContext()) { return(ctx.ingredients.FirstOrDefault(x => x.IngredientId == id)); } }
public Sandwich GetSandwish(int id) { using (var ctx = new SandwishContext()) { //return ctx.Sandwiches.Include( x=>x.Ingredients).Any(s => s.SandwichId == id)); /*var query = from s in ctx.Sandwiches * where s.SandwichId == id * select s; * * return query.FirstOrDefault<Sandwich>();*/ return(ctx.Sandwiches.Include(i => i.Ingredients).FirstOrDefault(x => x.SandwichId == id)); } }
public bool ComposeSandwish(Sandwich sandwishToUse, ICollection <Ingredient> ingredientsToAdd) { foreach (var ing in ingredientsToAdd) { sandwishToUse.Ingredients.Add(ing); } using (var ctx = new SandwishContext()) { if (!ctx.Sandwiches.Any(x => x.Name == sandwishToUse.Name)) { throw new Exception(nameof(sandwishToUse)); } ctx.Sandwiches.Attach(sandwishToUse); ctx.SaveChanges(); } return(true); }
public bool DeleteIngredientInStock(int id) { if (id == 0) { throw new ArgumentNullException(nameof(id)); } using (var ctx = new SandwishContext()) { var ingToSuppr = ctx.ingredients.FirstOrDefault(x => x.IngredientId == id); if (ingToSuppr == null) { throw new ArgumentNullException(nameof(id)); } ; ctx.ingredients.Remove(ingToSuppr); ctx.SaveChanges(); } return(true); }
public bool EditIngredientInStock(Ingredient ingredientToEdit) { if (ingredientToEdit == null) { throw new ArgumentNullException(nameof(ingredientToEdit)); } if (ingredientToEdit.IngredientId == 0) { throw new Exception(nameof(ingredientToEdit)); } if (String.IsNullOrEmpty(ingredientToEdit.Name) || String.IsNullOrWhiteSpace(ingredientToEdit.Name)) { throw new Exception(nameof(ingredientToEdit)); } using (var ctx = new SandwishContext()) { ctx.Entry(ingredientToEdit).State = EntityState.Modified; ctx.SaveChanges(); } return(true); }