Updating a Todo item

Finally, we want to be able to update an item in our Todo list to complete it or take some notes:

/// Update a specific todo item 
drop.post("updateTodo") { request in
guard let id = request.headers["id"]?.int,
let name = request.headers["name"],
let description = request.headers["description"],
let notes = request.headers["notes"],
let completed = request.headers["completed"],
let synced = request.headers["synced"]
else {
return try JSON(node: ["message":
"Please include mandatory parameters"])
}

let todoItem = Todo(todoId: id,
name: name,
description: description,
notes: notes,
completed: completed.toBool()!,
synced: synced.toBool()!)

let todos = TodoStore.sharedInstance
let message = todos.update(item: todoItem)
return try JSON(node: ["message": message])
}

Here, we check for the headers first and, if they are present, we use the update method in TodoStore to update a specific item in our Store. We can test it like this:

curl -X "POST" "http://localhost:8080/updateTodo" 
-H "Cookie: test=123"
-H "id: 3"
-H "notes: new note"
-H "name: updated name"
-H "description: updated description"
-H "completed: yes"

At this point, we should have a simple backend API to create, list, update, and delete Todo items in memory. In the next section, we will develop an iOS application to leverage this API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset