/// <summary> /// Adds an Ingredient to the database. /// An Ingredient will only be added if certain criteria have been fulfilled. /// If not, an error will be shown. /// </summary> private void AddIngredient() { try { if (Action == "add") { _ingredientRepo.AddIngredient(Business.Ingredient.ConvertToNewDataIngredient(Ingredient)); } if (Action == "edit") { Data.Ingredient dataIngredient = _ingredientRepo.GetIngredientById(Ingredient.ID); dataIngredient.SellPrice = Ingredient.SellPrice; dataIngredient.BuyPrice = Ingredient.BuyPrice; dataIngredient.Amount = Ingredient.Amount; dataIngredient.Name = Ingredient.Name; dataIngredient.SellPrice = Ingredient.SellPrice; _ingredientRepo.UpdateIngredient(dataIngredient); } AddOrEditIngredientRequested?.Invoke(); } catch (ArgumentException e) { ErrorHandler.ThrowError(20001, e.Message); } catch (Exception e) { Console.WriteLine(e); throw; } }
public bool DeleteIngredientReturnSuccess(Data.Ingredient ingredient) { try { if (_context.RecipeItems.Count(x => x.Ingredient.Id == ingredient.Id) == 0) { if (!_context.Ingredients.Local.Any(i => i.Id == ingredient.Id)) { _context.Ingredients.Attach(ingredient); } _context.Ingredients.Remove(ingredient); _context.SaveChanges(); return(true); } else { ErrorHandler.ThrowError(10, "A product used in a recipe cannot be deleted."); return(false); } } catch (Exception e) { ErrorHandler.ThrowError(10, "A product used in a recipe cannot be deleted."); Console.WriteLine(e.Message); return(false); } }
public Data.Ingredient UpdateIngredient(Data.Ingredient ingredient) { if (!_context.Ingredients.Local.Any(i => i.Id == ingredient.Id)) { _context.Ingredients.Attach(ingredient); } _context.Entry(ingredient).State = EntityState.Modified; _context.SaveChanges(); return(ingredient); }
private void _stockViewModel_StockEditIngredientRequested(Data.Ingredient ing) { Business.Ingredient busIng = new Business.Ingredient() { Amount = ing.Amount, BrandName = ing.BrandName, BuyPrice = ing.BuyPrice, ID = ing.Id, Name = ing.Name, SellPrice = ing.SellPrice, Supplier = ing.Supplier }; StockAddOrEditIngredientViewModel = new StockAddOrEditIngredientViewModel(_ingredientRepo, _supplierRepo, busIng); StockAddOrEditIngredientViewModel.AddOrEditIngredientRequested += AddIngredientViewModel_AddOrEditIngredientRequested; StockAddOrEditIngredientViewModel.AddSupplierRequested += StockAddOrEditIngredientViewModel_AddSupplierRequested; StockAddOrEditIngredientViewModel.BackToStockRequested += StockAddOrEditIngredientViewModel_BackToStockRequested; CurrentViewModel = StockAddOrEditIngredientViewModel; }
public void DeleteIngredient(Data.Ingredient ingredient) { try { if (!_context.Ingredients.Local.Any(i => i.Id == ingredient.Id)) { _context.Ingredients.Attach(ingredient); } _context.Ingredients.Remove(ingredient); _context.SaveChanges(); } catch (Exception e) { ErrorHandler.ThrowError(10, "A product used in a recipe cannot be deleted."); Console.WriteLine(e.Message); } }
public void AddSale() { if (SelectedProduct != null) { ObservableCollection <Data.RecipeItem> recipes = new ObservableCollection <Data.RecipeItem>(_iRecipeRepo.GetRecipes() .Where(recipe => recipe.Product.ID.Equals(SelectedProduct.ID))); bool isAddSalePossible = true; for (int i = 0; i < Amount; i++) { isAddSalePossible = true; foreach (Data.RecipeItem recipe in recipes) { Data.Ingredient ingredient = recipe.Ingredient; if (ingredient.Amount < recipe.Amount) { isAddSalePossible = false; ErrorHandler.ThrowError(0, $"{Properties.Resources.ToLittleOf}: {recipe.Ingredient.Name}"); } } if (isAddSalePossible) { foreach (Data.RecipeItem recipe in recipes) { Data.Ingredient ingredient = recipe.Ingredient; ingredient.Amount -= recipe.Amount; _iIngredientRepo.UpdateIngredient(ingredient); } Data.Sale sale = new Data.Sale() { Cooked = 0, Delivered = 0, Paid = 0, Product = SelectedProduct.ConvertToData() }; _iOccupanciesRepo.AddSale(_occupancy, sale); MessageHandler.InvokeSuccessMessage(Properties.Resources.InformationAddSale, Properties.Resources.InformationSaleAdded.Replace("{product}", $"{SelectedProduct.Name}")); } } ViewBack(); } }
public Data.Ingredient AddIngredient(Data.Ingredient ingredient) { _context.Ingredients.Add(ingredient); _context.SaveChanges(); return(ingredient); }
public Data.Ingredient GetIngredientById(int id) { Data.Ingredient ing = _context.Ingredients.FirstOrDefault(x => x.Id == id); return(ing); }