Advanced Recipe Search
Introduction: Making Recipe Search Smarter
Welcome back! In the last lesson, you learned how to build basic endpoints to retrieve recipes from your database, including fetching a single recipe by its ID and listing recipes with pagination. These are the building blocks of any recipe API.
Now, let’s take things a step further. Imagine you open a cooking app and want to find recipes that use only the ingredients you have in your kitchen. Or maybe you want to see the steps of a recipe as a list so you can follow them one by one. These features make your app much more helpful and user-friendly.
In this lesson, you’ll learn how to:
- Search for recipes by a list of ingredients.
- Return recipe steps as an array, not just a long string.
These skills will help you build a smarter, more flexible recipe API.
Quick Model Recap
Before we dive in, let’s quickly remind ourselves how our data is organized. This will help you understand how searching and filtering work.
With Prisma, your models are defined in the schema.prisma file. Here’s a simplified look at the relevant models:
- The
RecipeIngredientmodel acts as a join table, connecting recipes and ingredients in a many-to-many relationship. - Each
Recipecan have manyIngredients, and eachIngredientcan be used in manyRecipes. - The
stepsfield onRecipeis a string containing all the steps, separated by newlines.
This structure is important for searching recipes by ingredients.
Returning Recipe Steps as an Array
Many apps and websites want to show recipe steps one at a time, not as a single long string. To help with this, you can create an endpoint that returns the steps as an array.
