Skip to content

Instantly share code, notes, and snippets.

@dougritter
Created July 19, 2019 15:50
Show Gist options
  • Save dougritter/595e719c080878d190fc5c9e4209b61e to your computer and use it in GitHub Desktop.
Save dougritter/595e719c080878d190fc5c9e4209b61e to your computer and use it in GitHub Desktop.
Vapor Fluent - multiple insertions in PostgreSQL from a request
final class NoteController {
// NotesRequest is a struct that represents the request body
// It contains a function that returns a [Note] array.
// The line 9 creates a Note for each item in the array
func create(_ req: Request) throws -> Future<HTTPStatus> {
return try req.content.decode(NotesRequest.self).flatMap { notesRequest in
return NotesRequest.toNotes(notesRequest: notesRequest)
.map { item in Note(id: nil,
text: item.text,
retroId: item.retroId!,
laneId: item.laneId!)
.create(on: req) }
.flatten(on: req)
.transform(to: .ok)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment