From 62e671dd9e6e495da79f4d7051935960d2e442d0 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Thu, 11 Feb 2016 21:35:56 -0800 Subject: [PATCH] Refactor and deduplicate RolesRouter, fix missing query on /roles. --- src/Routers/RolesRouter.js | 43 ++++++++++++++++++++++++++++++++++ src/index.js | 3 ++- src/roles.js | 48 -------------------------------------- 3 files changed, 45 insertions(+), 49 deletions(-) create mode 100644 src/Routers/RolesRouter.js delete mode 100644 src/roles.js diff --git a/src/Routers/RolesRouter.js b/src/Routers/RolesRouter.js new file mode 100644 index 0000000000..b20a91ee86 --- /dev/null +++ b/src/Routers/RolesRouter.js @@ -0,0 +1,43 @@ + +import ClassesRouter from './ClassesRouter'; +import PromiseRouter from '../PromiseRouter'; +import rest from '../rest'; + +export class RolesRouter extends ClassesRouter { + handleFind(req) { + req.params.className = '_Role'; + return super.handleFind(req); + } + + handleGet(req) { + req.params.className = '_Role'; + return super.handleGet(req); + } + + handleCreate(req) { + req.params.className = '_Role'; + return super.handleCreate(req); + } + + handleUpdate(req) { + req.params.className = '_Role'; + return super.handleUpdate(req); + } + + handleDelete(req) { + req.params.className = '_Role'; + return super.handleDelete(req); + } + + getExpressRouter() { + let router = new PromiseRouter(); + router.route('GET','/roles', req => { return this.handleFind(req); }); + router.route('GET','/roles/:objectId', req => { return this.handleGet(req); }); + router.route('POST','/roles', req => { return this.handleCreate(req); }); + router.route('PUT','/roles/:objectId', req => { return this.handleUpdate(req); }); + router.route('DELETE','/roles/:objectId', req => { return this.handleDelete(req); }); + return router; + } +} + +export default RolesRouter; diff --git a/src/index.js b/src/index.js index 9d9910bd4b..952c1c05d6 100644 --- a/src/index.js +++ b/src/index.js @@ -22,6 +22,7 @@ import { ClassesRouter } from './Routers/ClassesRouter'; import { InstallationsRouter } from './Routers/InstallationsRouter'; import { UsersRouter } from './Routers/UsersRouter'; import { SessionsRouter } from './Routers/SessionsRouter'; +import { RolesRouter } from './Routers/RolesRouter'; // Mutate the Parse object to add the Cloud Code handlers addParseCloud(); @@ -133,7 +134,7 @@ function ParseServer(args) { new ClassesRouter().getExpressRouter(), new UsersRouter().getExpressRouter(), new SessionsRouter().getExpressRouter(), - require('./roles'), + new RolesRouter().getExpressRouter(), require('./analytics'), new InstallationsRouter().getExpressRouter(), require('./functions'), diff --git a/src/roles.js b/src/roles.js deleted file mode 100644 index 6aaf806526..0000000000 --- a/src/roles.js +++ /dev/null @@ -1,48 +0,0 @@ -// roles.js - -var Parse = require('parse/node').Parse, - PromiseRouter = require('./PromiseRouter'), - rest = require('./rest'); - -var router = new PromiseRouter(); - -function handleCreate(req) { - return rest.create(req.config, req.auth, - '_Role', req.body); -} - -function handleUpdate(req) { - return rest.update(req.config, req.auth, '_Role', - req.params.objectId, req.body) - .then((response) => { - return {response: response}; - }); -} - -function handleDelete(req) { - return rest.del(req.config, req.auth, - '_Role', req.params.objectId) - .then(() => { - return {response: {}}; - }); -} - -function handleGet(req) { - return rest.find(req.config, req.auth, '_Role', - {objectId: req.params.objectId}) - .then((response) => { - if (!response.results || response.results.length == 0) { - throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, - 'Object not found.'); - } else { - return {response: response.results[0]}; - } - }); -} - -router.route('POST','/roles', handleCreate); -router.route('GET','/roles/:objectId', handleGet); -router.route('PUT','/roles/:objectId', handleUpdate); -router.route('DELETE','/roles/:objectId', handleDelete); - -module.exports = router; \ No newline at end of file