- Hands-On Server-Side Web Development with Swift
- Angus Yeung
- 106字
- 2021-06-10 19:00:26
Updating an entry
The handler for updating an entry is the second handler belonging to the /journal/Int.parameter route group:
func editEntry(_ req: Request) throws -> Future<HTTPStatus> {
let index = try req.parameters.next(Int.self)
let newID = UUID().uuidString
return try req.content.decode(Entry.self).map(to: HTTPStatus.self) { entry in // [1]
let newEntry = Entry(id: newID,
title: entry.title,
content: entry.content)
guard let result = self.journal.update(index: index, entry: newEntry) else {
throw Abort(.badRequest)
}
print("Updated: \(result)")
return .ok
}
}
The line at [1] shows that the JSON object is decoded and serialized into an Entry object, and then an array containing HTTPStatus is returned from the mapping of a result using map(to:).