November 2018
Intermediate to advanced
404 pages
10h 16m
English
Following getTotal(), add the following handler for creating a new entry:
func newEntry(_ req: Request) throws -> Future<HTTPStatus> { // [1] let newID = UUID().uuidString // [2] return try req.content.decode(Entry.self).map(to: HTTPStatus.self) { entry in // [3] let newEntry = Entry(id: newID, title: entry.title, content: entry.content) // [4] guard let result = self.journal.create(newEntry) else { // [5] throw Abort(.badRequest) // [6] } print("Created: \(result)") return .ok // [7] }}
The newEntry(_ req:) function handles the creation of a new entry in several steps:
Read now
Unlock full access