Defining TODO Endpoints

1. GET- TODOS

Our first endpoint will be todos that retrieves all items available in the storage.

We can see how simple that is! It just does a storage.getAll call. This endpoint will respond to GET /todos URL in our server.

2. POST -TODO

Now we can define a new endpoint to add todos that will respond at POST /todo

This one is a little more interesting to look at. Because it is a POST we need to extract the Item from the body of the message. However, there is a gotcha, the Item cannot be created without an UUID and that is what the postTodo function does. In other words, we extract the taskName from the POST message body and create an Item object with it and a random UUID .

The idea is that you don’t need to have a different Item model without the id to be sent on the requests.

Then function storage.add(item) does not really care about the id, it just add a new Item to the storage and returns the recently created one (it has its own new id).

Once the Item has been added, we return the recently created one.

We can test it from our terminal by doing

which returns

3. GET -TODO

Retrieving a particular Item is an easy task. We only need the id and then we can ask for the storage to get it for us.

We can test it by doing:

Which returns the same todo we inserted before.

Notice that if the item is not in the storage, .fold will return NotFound HTTP message back to the client.

4. DELETE-TODO

In the same way we can do deleteTodo

We look for the Item with the given id. If it exists then we delete it. Then we return accordingly using .fold .

TodoService