/// <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;
            }
        }
Esempio n. 2
0
        public IActionResult PostIngredient(Ingredient ingredient)
        {
            /*
             * if (IngredientExists(ingredient.Id))
             * {
             *  _logger.LogWarning($"Ingredient with id: {ingredient.Id} already exists.");
             *  return Conflict();
             * }
             */
            _ingredientRepo.AddIngredient(ingredient);
            _ingredientRepo.SaveChanges();

            _logger.LogInformation($"Ingredient with id: {ingredient.Id} has been added.");
            return(CreatedAtAction(nameof(GetIngredient), new { id = ingredient.Id }, ingredient));
        }