Skip to content

Instantly share code, notes, and snippets.

@NeuralGlue
Created November 27, 2019 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeuralGlue/50d4d2bcf6d7fa1d9b8b6a4000f2a1f4 to your computer and use it in GitHub Desktop.
Save NeuralGlue/50d4d2bcf6d7fa1d9b8b6a4000f2a1f4 to your computer and use it in GitHub Desktop.
//Assuming that you have a route such as: /things/Thing.parameter
// Alo that you have decorated the route to use authentication via the auth module
func patchThing (_ req: Request) throws -> Future<Thing> {
let user = try req.requireAuthenticated(User.self)
return try flatMap(
to: Thing.self,
req.parameters.next(Thing.self),
req.content.decode(Thing.self)) { thing, updatedThing in
// ensure the thing belongs to this user
guard try thing.ownerId == user.requireID() else {
throw Abort(.forbidden)
}
thing.attribute1 = updatedThing.attribute1
thing.attribute2 = updatedThing.attribute2
...
return thing.save(on: req)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment