November 2018
Intermediate to advanced
404 pages
10h 16m
English
The handler for editing a record follows the similar workflow as in the handler for entry retrieval, but takes an additional step to update the record with the intended changes:
func editEntry(_ req: Request) throws -> Future<JournalEntry> { let id = try req.parameters.next(Int.self) return try req.content.decode(JournalEntry.self).flatMap { updated in return JournalEntry.find(id, on: req) .flatMap(to: JournalEntry.self) { original in guard let original = original else { throw Abort(.notFound) } original.title = updated.title original.content = updated.content return original.save(on: req) } }}
In the preceding code, the id is retrieved first using parameters.next(Int.self), and then a query using find(on:) is issued to retrieve ...
Read now
Unlock full access