November 2018
Intermediate to advanced
404 pages
10h 16m
English
The handler for deleting a record is not so different from the HTTP PUT operation. After retrieving the record, you simply call the delete(on:) method:
func removeEntry(_ req: Request) throws -> Future<HTTPStatus> { let id = try req.parameters.next(Int.self) return JournalEntry.find(id, on: req).flatMap { entry in guard let entry = entry else { throw Abort(.notFound) } return entry.delete(on: req).transform(to: HTTPStatus.noContent) }}
The return type for the preceding function is Future. After the record has been deleted, you issue HTTPStatus.noContent to the client.
Now, use curl again to test the delete handler:
$ curl --request DELETE http://localhost:8080/api/admin/1
The preceding command should have erased the record ...
Read now
Unlock full access