From cc5f7e896bbf634b3dc41290bd0aeafe4cb4c192 Mon Sep 17 00:00:00 2001 From: sipp11 Date: Fri, 21 Aug 2015 14:02:46 +0700 Subject: [PATCH] Update some controller with count and return structure like DRF --- api/controllers/FlagController.js | 12 ++--- api/controllers/NationalityController.js | 7 +-- api/controllers/ObjReferenceController.js | 7 +-- api/controllers/ObjRevisionController.js | 59 ++++++++++++--------- api/controllers/PrefixController.js | 7 +-- api/controllers/UserActivityController.js | 62 +++++++++++++---------- config/connections.js | 20 ++------ config/env/development/connections.js | 17 +++++-- config/policies.js | 4 ++ 9 files changed, 108 insertions(+), 87 deletions(-) diff --git a/api/controllers/FlagController.js b/api/controllers/FlagController.js index d3d2073..faaca1b 100644 --- a/api/controllers/FlagController.js +++ b/api/controllers/FlagController.js @@ -9,10 +9,10 @@ module.exports = { search: function(req, res) { var word = req.param('query'), - flagQuery = Flag.find(); + query = Flag.find(); if (word != undefined) - flagQuery = Flag.find({ + query = Flag.find({ where: { or: [ {english: {contains: word}}, @@ -20,13 +20,11 @@ module.exports = { {iso2: word}, ] }, - sort: { - thai: 1, - english: 1 - } + sort: { thai: 1, english: 1 } }); - flagQuery.limit(10).exec(function(err, flags) { + page = ((req.query.page != undefined) ? +req.query.page : 1); + query.paginate({page: page, limit: 10}).exec(function(err, items) { if (err) { res.send(400); } else { diff --git a/api/controllers/NationalityController.js b/api/controllers/NationalityController.js index 5dd9504..5c39682 100644 --- a/api/controllers/NationalityController.js +++ b/api/controllers/NationalityController.js @@ -9,10 +9,10 @@ module.exports = { search: function(req, res) { var word = req.param('query'), - nationalityQuery = Nationality.find(); + query = Nationality.find(); if (word != undefined) - nationalityQuery = Nationality.find({ + query = Nationality.find({ where: { or: [ {english: {contains: word}}, @@ -25,7 +25,8 @@ module.exports = { } }); - nationalityQuery.limit(10).exec(function(err, items) { + page = ((req.query.page != undefined) ? +req.query.page : 1); + query.paginate({page: page, limit: 10}).exec(function(err, items) { if (err) { res.send(400); } else { diff --git a/api/controllers/ObjReferenceController.js b/api/controllers/ObjReferenceController.js index 8d78caf..19000eb 100644 --- a/api/controllers/ObjReferenceController.js +++ b/api/controllers/ObjReferenceController.js @@ -9,10 +9,10 @@ module.exports = { search: function(req, res) { var word = req.param('query'), - refQuery = ObjReference.find(); + query = ObjReference.find(); if (word != undefined) - refQuery = ObjReference.find({ + query = ObjReference.find({ where: { or: [ {team: word}, @@ -26,7 +26,8 @@ module.exports = { } }); - refQuery.limit(10).exec(function(err, items) { + page = ((req.query.page != undefined) ? +req.query.page : 1); + query.paginate({page: page, limit: 10}).exec(function(err, items) { if (err) { res.send(400); } else { diff --git a/api/controllers/ObjRevisionController.js b/api/controllers/ObjRevisionController.js index 4c21f03..26f02fe 100644 --- a/api/controllers/ObjRevisionController.js +++ b/api/controllers/ObjRevisionController.js @@ -9,10 +9,10 @@ module.exports = { search: function(req, res) { var word = req.param('query'), - revisionQuery = ObjRevision.find(); + query = ObjRevision.find(); if (word != undefined) - revisionQuery = ObjRevision.find({ + query = ObjRevision.find({ where: { or: [ {obj: {jarvisId: {contains: word}}}, @@ -24,7 +24,8 @@ module.exports = { } }); - revisionQuery.limit(10).exec(function(err, items) { + page = ((req.query.page != undefined) ? +req.query.page : 1); + query.paginate({page: page, limit: 10}).exec(function(err, items) { if (err) { res.send(400); } else { @@ -35,39 +36,51 @@ module.exports = { jarvisid: function(req, res) { var word = req.param('query'), - revisionQuery = ObjRevision.find(); + _options = {}, + _count, query; if (word != undefined) - revisionQuery = ObjRevision.find({ - jarvisId: word, - sort: { date: 1} - }); + _options['jarvisId'] = word; + _options['sort'] = {date: -1}; - revisionQuery.limit(10).exec(function(err, items) { - if (err) { + query = ObjRevision.find(_options); + page = ((req.query.page != undefined) ? +req.query.page : 1); + ObjRevision.count(_options).exec(function(err, total) { + if (err) res.send(400); - } else { - res.send(items); - } + + query.paginate({page: page, limit: 10}).exec(function(err, items) { + if (err) { + res.send(400); + } else { + res.send({count: total, results: items}); + } + }); }); }, bbapi: function(req, res) { var word = req.param('query'), - revisionQuery = ObjRevision.find(); + _options = {}, + _count, query; if (word != undefined) - revisionQuery = ObjRevision.find({ - bbApi: word, - sort: { date: 1} - }); + _options['bbApi'] = word; + _options['sort'] = {date: -1}; - revisionQuery.limit(10).exec(function(err, items) { - if (err) { + query = ObjRevision.find(_options); + page = ((req.query.page != undefined) ? +req.query.page : 1); + ObjRevision.count(_options).exec(function(err, total) { + if (err) res.send(400); - } else { - res.send(items); - } + + query.paginate({page: page, limit: 10}).exec(function(err, items) { + if (err) { + res.send(400); + } else { + res.send({count: total, results: items}); + } + }); }); } diff --git a/api/controllers/PrefixController.js b/api/controllers/PrefixController.js index b73f1da..ccd1445 100644 --- a/api/controllers/PrefixController.js +++ b/api/controllers/PrefixController.js @@ -9,10 +9,10 @@ module.exports = { search: function(req, res) { var word = req.param('query'), - prefixQuery = Prefix.find(); + query = Prefix.find(); if (word != undefined) - prefixQuery = Prefix.find({ + query = Prefix.find({ where: { or: [ {english: {contains: word}}, @@ -25,7 +25,8 @@ module.exports = { } }); - prefixQuery.limit(10).exec(function(err, items) { + page = ((req.query.page != undefined) ? +req.query.page : 1); + query.paginate({page: page, limit: 10}).exec(function(err, items) { if (err) { res.send(400); } else { diff --git a/api/controllers/UserActivityController.js b/api/controllers/UserActivityController.js index 986c558..43ec2d9 100644 --- a/api/controllers/UserActivityController.js +++ b/api/controllers/UserActivityController.js @@ -24,7 +24,8 @@ module.exports = { } }); - refQuery.limit(10).exec(function(err, items) { + page = ((req.query.page != undefined) ? +req.query.page : 1); + refQuery.paginate({page: page, limit: 10}).exec(function(err, items) { if (err) { res.send(400); } else { @@ -35,46 +36,55 @@ module.exports = { oneteam: function(req, res) { var teamQ = req.param('team'), - refQuery = UserActivity.find(); + _options = {}, + _count, query; if (teamQ != undefined) - refQuery = UserActivity.find({ - team: teamQ, - sort: { - date: -1 - } - }); + _options['team'] = teamQ; + _options['sort'] = {date: -1}; - refQuery.limit(10).exec(function(err, items) { - if (err) { + query = UserActivity.find(_options); + page = ((req.query.page != undefined) ? +req.query.page : 1); + UserActivity.count(_options).exec(function(err, total) { + if (err) res.send(400); - } else { - res.send(items); - } + + query.paginate({page: page, limit: 10}).exec(function(err, items) { + if (err) { + res.send(400); + } else { + res.send({count: total, results: items}); + } + }); }); + }, oneuser: function(req, res) { var teamQ = req.param('team'), usernameQ = req.param('username'), - refQuery = UserActivity.find(); + _options = {}, + _count, query; if ((teamQ != undefined) && (usernameQ != undefined)) - refQuery = UserActivity.find({ - team: teamQ, - username: usernameQ, - sort: { - date: -1 - } - }); + _options = {team: teamQ, username: usernameQ}; + _options['sort'] = {date: -1}; - refQuery.limit(10).exec(function(err, items) { - if (err) { + query = UserActivity.find(_options); + page = ((req.query.page != undefined) ? +req.query.page : 1); + UserActivity.count(_options).exec(function(err, total) { + if (err) res.send(400); - } else { - res.send(items); - } + + query.paginate({page: page, limit: 10}).exec(function(err, items) { + if (err) { + res.send(400); + } else { + res.send({count: total, results: items}); + } + }); }); + } diff --git a/config/connections.js b/config/connections.js index ab998e1..1c1a76d 100644 --- a/config/connections.js +++ b/config/connections.js @@ -56,23 +56,9 @@ module.exports.connections = { * Run: npm install sails-mongo * * * ***************************************************************************/ - - hackintoshMongo: { - adapter: 'sails-mongo', - host: '10hackintosh', - port: 27017, - // user: 'username', - // password: 'password', - database: 'sails', - }, - jarvisBlackboard: { - adapter: 'sails-mongo', - host: '10hackintosh', - port: 27017, - // user: 'username', - // password: 'password', - database: 'jarvis-bb', - }, + // someMongoDBServer: { + // adapter: 'sails-mongo', + // }, /*************************************************************************** * * * PostgreSQL is another officially supported relational database. * diff --git a/config/env/development/connections.js b/config/env/development/connections.js index fbc7c64..89d383f 100644 --- a/config/env/development/connections.js +++ b/config/env/development/connections.js @@ -1,13 +1,20 @@ -/*module.exports.connections = { - +module.exports.connections = { hackintoshMongo: { adapter: 'sails-mongo', - host: '10hackintosh.vnll', + host: '10hackintosh', + port: 27017, + // user: 'username', + // password: 'password', + database: 'sails', + }, + jarvisBlackboard: { + adapter: 'sails-mongo', + host: '10hackintosh', port: 27017, // user: 'username', // password: 'password', - database: 'feeder', + database: 'jarvis-bb', } + }; -*/ \ No newline at end of file diff --git a/config/policies.js b/config/policies.js index d23d009..49cde91 100644 --- a/config/policies.js +++ b/config/policies.js @@ -47,12 +47,16 @@ module.exports.policies = { }, 'objrevision': { '*': 'sessionAuth', + 'index': true, + 'find': true, 'search': true, 'jarvisid': true, 'bbapi': true, }, 'objreference': { '*': 'sessionAuth', + 'index': true, + 'find': true, 'search': true, }, 'useractivity': {