コード例 #1
0
        public async Task <ActionResult> Post([FromBody] Dish dish, string ing_name)
        {
            // Converting ingredient name to title case (for convention)
            ing_name = textInfo.ToTitleCase(ing_name.ToLower());

            try
            {
                // Making sure ingredient exists. If it does not, this will throw an exception
                await _ingredientRepository.GetByName(ing_name);

                // Inserting record in the Dish table
                await _repository.Insert(dish);

                int last_inserted_dish = await _repository.getLastInserted();

                // Inserting record in the Dish_Ingredient table
                await _dishIngredientRepository.Insert(new Dish_Ingredient { Dish_ID = last_inserted_dish, Ing_Name = ing_name });

                return(Ok("Dish record inserted successfully\n"));
            }
            catch (Npgsql.PostgresException ex)
            {
                // Postgres threw an exception
                return(BadRequest(ex.Message.ToString()));
            }
            catch
            {
                // Unknown error
                return(BadRequest("Error: Dish record was not inserted. Make sure you are providing an ingredient that exists\n"));
            }
        }