paint-brush
Creating a todolist backend with persistenceby@012parth
3,791 reads
3,791 reads

Creating a todolist backend with persistence

by Parth MudgalOctober 19th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This <a href="https://hackernoon.com/tagged/todolist" target="_blank">todolist</a> project helps showcase building a <a href="https://hackernoon.com/tagged/backend" target="_blank">backend</a> for small apps. This article will cover the following aspects

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Creating a todolist backend with persistence
Parth Mudgal HackerNoon profile picture

This todolist project helps showcase building a backend for small apps. This article will cover the following aspects

  • Have a SQL based database for persistence (sqlite)
  • Defining the todo and project entities
  • Deploy on cloud (Heroku)
  • Test the new APIs using curl

For a while now I have been using Daptin for small apps. It allows you to iterate quickly and create a backand for an app without too much work. It also seems like a perfect jumping off for a series of tutorials where we will be building a “Todo list” application.

The setup

First thing you’ll want to do is to signup on heroku (free instance is enough for testing this out), since we will deploy our backend on heroku. Then follow this link to deploy an instance of daptin on heroku.


Heroku_Deploy Daptin_dashboard.heroku.com

Choose a name for this app

Choose an app name for this instance (I have chosen todolist-tutorial). After you click ‘deploy app’, it will take a couple of minutes to get the instance running.

Deploying app can take 3–5 minutes

Once the deployment is complete, you will get the link to the instance. Click “View” to go to the newly deployed instance.

“View” will take us to the newly deployed instance

Create an account using Sign Up, Then Sign In

Once the instance is ready, we will create a user account on daptin. Create an account using Sign Up, then Sign In.

Define Schema

We need to define our todo entity. Lets take this opportunity to create a slightly more complex todo-list implementation. We will have “Project” which will have a name and “Todo” will belong to one of these projects.

We will create a YAML file to define the schema (you can use JSON also) and upload it to daptin to get our APIs

  • Todo will have a “title”, a “description” and a “schedule_date”.
  • Project will have a “name”.
  • We also need to define the relation between the todo and the project entity. Over all, it will look something as follows:

You can find the complete file I have used here. Save this file so that we can upload it to daptin.

Select Upload Schema and choose the previously created file

Upload this will take another 15–20 seconds. On completion, our APIs are ready.

Testing the APIs

Lets try them out. We will use curl to call the API from terminal:

curl “https://todolist-tutorial.herokuapp.com/api/todo"

Response













{“links”: {“current_page”: 1,“from”: 0,“last_page”: 0,“last_page_url”: “//api/todo?page[number]=0”,“next_page_url”: “//api/todo?page[number]=2”,“per_page”: 10,“to”: 10,“total”: 0},“data”: []}

Since we have not created any todo yet, we get an empty array in the “data” key. Lets create a new todo

curl “https://todolist-tutorial.herokuapp.com/api/todo" — data ‘{“data”: { “type”: “todo”, “attributes”: {“title”: “get this done”, “schedule”: “2017–10–19”}}}’

Response:








{“errors”: [{“status”: “500”,“title”: “Unauthorized”}]}

Whoops, since we call the create API without an Auth token, we are not allowed to do this. Lets sign in using terminal to get a new token.

curl “https://todolist-tutorial.herokuapp.com/action/user/signin" — data ‘{“attributes”: {“email”: “[email protected]”, “password”: “<password>”}}’

Token in the sign in response:









[{“ResponseType”: “client.store.set”,“Attributes”: {“key”: “token”,“value”: “eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RlckBnbWFpbC5jb20iLCJleHAiOjE1MDg0MDA4OTcsImlhdCI6IjIwMTctMTAtMTlUMDc6MTQ6NTcuNjUxMjY1NTU5WiIsImlzcyI6ImRhcHRpbiIsImp0aSI6ImI1ZjJlMDA0LTRlMGMtNGMwZi05MDc3LWZmM2NmNThmNmI1OSIsIm5hbWUiOiJ0ZXN0ZXIiLCJuYmYiOjE1MDgzOTcyOTcsInBpY3R1cmUiOiJodHRwczovL3d3dy5ncmF2YXRhci5jb20vYXZhdGFyLzhjM2ZlMWFkMjVlNmQ1ZjQ3NTEyZWE3MzY1NDE5OTY2XHUwMDI2ZD1tb25zdGVyaWQifQ.gM8SdgCpr_iCsI0zGN0nCnKTYiDhkwXZanWrYD1fMRg”}}]

We will use this token to create a todo now. We send the token in the “Authorization” Header. The token is a Oauth2 token.

We can now use the first api to get the list of todos. We will also need to use the Authorization token.

We can also allow guests to create and fetch these todos. But lets cover that in next post.

Couple of ending notes:

  • The database being used here is SQLite. We can also use MySQL or Postgresql. Again, we will go through that in another post
  • Heroku shuts down free instances after 30 mins of idleness. So all changes will be lost. The paid instance does it have this issue and costs about $5 per month.
  • The deployed instance will allow guests to sign up and sign in. Click “Become Admin” on the daptin dashboard to close registration. You can enable this later.