Last active
January 25, 2016 17:03
-
-
Save danscan/d84b90b153075f28da4a to your computer and use it in GitHub Desktop.
Policy function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function requestUserOwnsPost(requestUser, post) { | |
return requestUser.id === post.owner; | |
} | |
const db = { | |
postsById: { | |
'2': { | |
id: 2, | |
owner: 1, | |
body: 'hey', | |
}, | |
}, | |
getPost(postId) { | |
return this.postsById[postId]; | |
}, | |
patchPost(postId, patch) { | |
const post = this.getPost(postId); | |
const patchedPost = { ...post, ...patch }; | |
this.postsById[postId] = patchedPost; | |
return patchedPost; | |
}, | |
}; | |
app.patch('/posts/:postId', (req, res, next) => { | |
const { postId } = req.params; | |
const post = db.getPost(postId); | |
const permitOperation = requestUserOwnsPost(req.user, post); | |
if (!permitOperation) { | |
return next(new Error('You cannot patch a post you do not own')); | |
} | |
return req.json(db.patchPost(postId, req.body)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment