O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting a specific Todo item

The preceding call retrieves all the items. If we want to get a specific item, we can do that too as follows:

/// Get a specific todo item drop.get("todo") { in     guard let id = request.headers["id"]?.int else {         return try JSON(node: ["message": "Please provide the id of             todo item"])     }     let todos: [Todo] = TodoStore.sharedInstance.listItems()     var json = [Todo]()     let item = todos.filter { $0.todoId == id }     if item.count > 0 {         json.append(item[0])     }     return try JSON(node: json) } 

Here, we check for the existence of headers and use the listItems() method in our TodoStore class to retrieve that specific item. We can test it in curl by executing the following commands in the terminal:

curl -X "GET" "http://localhost:8080/todo/" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required