Last time we tested the local data source with a simple request.
Now let’s add some real power to it.
First, declare a bunch of useful functions in the
LocalSource interface. These have to do with our future app’s functionality like adding recipes and corresponding ingredients and instructions.
Some of them return
Long, because we’ll add new entries to the database, which will assign an
id to each of them and return that
Now implement these functions in
These are basic functions that will fill up our database with recipes, ingredients and instructions.
 When creating an ingredient or instruction, we have to specify recipeId which this item belongs to and get the appropriate recipe
 Then we’ll create a new ingredient or instruction entity and attach it to our recipe
 When adding a new recipe to the database we first create a new recipe entry
 Then add all the instructions and ingredients to it
Now let’s bind those functions to our backend. Just like we did with getPizza, we’ll add some more routes for adding and getting recipes in
Implement them in the same file:
 When adding a Recipe we specify the Recipe data class as the body input and get a newly created recipe entity id from the database
 To get a recipe by id we need to lookup for the recipe with the id, that we specified in API route
One last thing before we test it: we want to be able to serialize our classes between the client and the server. To do so, we’ll need
kotlinx.serialization plugin. Let’s adjust our dependencies.
First, add this to the
backend module add the serialization plugin to
You may also need to adjust the project
build.gradle.kts so that it matches the latest Kotlin version you specified, for example like so:
Sync the project.
@Serializable annotation to all our model data classes like this:
You may need to do a manual import:
Now let’s test this in action. Use Postman for it. Rebuild and make the backend again and run it locally as before:
java -jar ./backend/build/libs/backend.jar -port=9000
Then try adding a Pizza Dough recipe with Postman. Send a
POST request to our API endpoint
http://localhost:9000/recipes. You can use the following body:
"name": "dry yeast",
"name": "olive oil",
"description": "Mix water, sugar and yeast. Let it froth for 10 minutes"
"description": "Mix dough, salt, olive oil. Add water mixture from previous step."
"description": "Knead the dough for 10 minutes"
500 Internal Server Error 😱
Don’t be upset! We’ll find out why.
To help us investigate the cause we can install
Application.module() and let it return the cause of the error like this :
Now if you rebuild the backend and try the same request again you’ll see
io.ktor.features.CannotTransformContentToTypeException: Cannot transform this request's content to com.example.recipecollection.backend.model.Recipe
This happens because we forgot to install
ContentNegotiation between the client and the server to help us serialize objects. Let’s do it now, add this to you application:
Build the backend and try sending the request again.
We get the recipe
id in return.
Now try posting the
Then try the
GET /recipesendpoint and you’ll get our Pizza Dough recipe in return.
Time to start cooking! 🧑🍳