Skip to content

Instantly share code, notes, and snippets.

@mpokryva
Last active July 26, 2022 15:55
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 mpokryva/66a52e8a6a25848d0312fee5af17d10a to your computer and use it in GitHub Desktop.
Save mpokryva/66a52e8a6a25848d0312fee5af17d10a to your computer and use it in GitHub Desktop.
/*
** ---- Without RLS ---- **
1. Check if user is a member of the org
a. If so, execute the update query
b. Else, return a 404
*/
Org update(userId, orgId, updatePayload) {
if (dao.isOrgMember(userId, orgId)) {
return dao.updateOrg(updatePayload);
} else {
throw new NotFoundException();
}
}
/* -- DAO layer -- */
boolean isOrgMember(userId, orgId) {
return query("EXISTS(SELECT 1 ...)");
}
Org updateOrg(updatePayload) {
return query("UPDATE orgs SET ... RETURNING *");
}
/*
** ----- With RLS ---- **
1. Execute the update query
a. If the org was returned from the db, return the org in the response
b. Else, return a 404
*/
Org update(userId, orgId, updatePayload) {
Optional<Org> maybeOrg = dao.updateOrg(updatePayload);
if (maybeOrg.isPresent()) {
return maybeOrg.get();
} else {
throw new NotFoundException();
}
}
/* -- DAO layer -- */
Optional<Org> updateOrg(updatePayload) {
return query("UPDATE orgs SET ... RETURNING *");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment