public async Task <ActionResult> Post([FromBody] Dish_Ingredient dish_ingredient) { dish_ingredient.Ing_Name = textInfo.ToTitleCase(dish_ingredient.Ing_Name.ToLower()); try { // Making sure that the referred dish and ingredient exist await _ingredientRepository.GetByName(dish_ingredient.Ing_Name); await _dishRepository.GetById(dish_ingredient.Dish_ID); // Inserting record in the Dish_Ingredient table await _repository.Insert(dish_ingredient); return(Ok("Dish_Ingredient record inserted successfully\n")); } catch (Npgsql.PostgresException ex) { // Postgres threw an exception return(BadRequest(ex.Message.ToString())); } catch { // Unknown error return(BadRequest("Error: Dish_Ingredient record was not inserted\n")); } }
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")); } }