From ce15b68a921aebe4857bb9d94776a7c09af23e7d Mon Sep 17 00:00:00 2001 From: nelo Date: Wed, 21 Jun 2017 07:28:03 +0200 Subject: [PATCH] list for all models --- services/client/common/models/my-model.js | 21 ++++++++++++++++--- .../production/common/methods/state/find.js | 6 +----- .../methods/warehouse/list-warehouses.js | 19 ----------------- .../common/methods/warehouse/list.js | 7 +++++++ services/production/common/models/my-model.js | 21 ++++++++++++++++--- services/service/models/my-model.js | 21 ++++++++++++++++--- 6 files changed, 62 insertions(+), 33 deletions(-) delete mode 100644 services/production/common/methods/warehouse/list-warehouses.js create mode 100644 services/production/common/methods/warehouse/list.js diff --git a/services/client/common/models/my-model.js b/services/client/common/models/my-model.js index 37f8d846b..1372fc644 100644 --- a/services/client/common/models/my-model.js +++ b/services/client/common/models/my-model.js @@ -25,7 +25,9 @@ module.exports = function(self) { for(let method in disableMethods) { //this.disableRemoteMethod(method, disableMethods[method]); } + }; + self.defineScope = function(serverFilter) { this.remoteMethod('list', { accepts: [ { @@ -43,10 +45,23 @@ module.exports = function(self) { path: '/list' } }); - - }; - self.list = function(filter, cb) {}; + this.list = function(clientFilter, cb) { + var where = {"and": []}; + (clientFilter) ? where.and.push(clientFilter.where) : undefined; + (serverFilter) ? where.and.push(serverFilter.where) : undefined; + + var order = (clientFilter && clientFilter.order) ? clientFilter.order : (serverFilter && serverFilter.order) ? serverFilter.order : undefined; + var limit = (serverFilter && serverFilter.limit) ? serverFilter.limit : (clientFilter && clientFilter.limit) ? clientFilter.limit : undefined; + + var filter = {"where": where, "order": order, "limit": limit}; + filter = removeEmpty(filter); + + this.find(filter, function(err, states) { + (!err) ? cb(null, states) : cb(err, null); + }); + }; + }; self.rawSql = function(query, params, cb) { this.dataSource.connector.execute(query, params, function(error, response) { diff --git a/services/production/common/methods/state/find.js b/services/production/common/methods/state/find.js index af8a7c217..19dc86b36 100644 --- a/services/production/common/methods/state/find.js +++ b/services/production/common/methods/state/find.js @@ -1,10 +1,6 @@ module.exports = function(State) { State.list = function(filter, cb) { - State.find(where(filter), function(err, states) { - if(!err){ - cb(null, states); - } - }); + }; function where(filter) { diff --git a/services/production/common/methods/warehouse/list-warehouses.js b/services/production/common/methods/warehouse/list-warehouses.js deleted file mode 100644 index c1a2413ee..000000000 --- a/services/production/common/methods/warehouse/list-warehouses.js +++ /dev/null @@ -1,19 +0,0 @@ -module.exports = function(Warehouse) { - Warehouse.list = function(filter, cb) { - Warehouse.find(where(filter), function(err, warehouses){ - if(!err) - cb(null, warehouses); - else - cb(err, null); - }); - }; - - var where = function(filter){ - if(!filter) - return {"where": {"tpv": {"neq": 0}}} - else { - filter.where.tpv = {"neq": 0} - return filter; - } - } -} \ No newline at end of file diff --git a/services/production/common/methods/warehouse/list.js b/services/production/common/methods/warehouse/list.js new file mode 100644 index 000000000..1f3dc06bb --- /dev/null +++ b/services/production/common/methods/warehouse/list.js @@ -0,0 +1,7 @@ +module.exports = function(Warehouse) { + + var serverFilter = {"where": {"tpv": {"neq": 0}}}; + + Warehouse.defineScope(serverFilter); + +} \ No newline at end of file diff --git a/services/production/common/models/my-model.js b/services/production/common/models/my-model.js index 37f8d846b..1372fc644 100644 --- a/services/production/common/models/my-model.js +++ b/services/production/common/models/my-model.js @@ -25,7 +25,9 @@ module.exports = function(self) { for(let method in disableMethods) { //this.disableRemoteMethod(method, disableMethods[method]); } + }; + self.defineScope = function(serverFilter) { this.remoteMethod('list', { accepts: [ { @@ -43,10 +45,23 @@ module.exports = function(self) { path: '/list' } }); - - }; - self.list = function(filter, cb) {}; + this.list = function(clientFilter, cb) { + var where = {"and": []}; + (clientFilter) ? where.and.push(clientFilter.where) : undefined; + (serverFilter) ? where.and.push(serverFilter.where) : undefined; + + var order = (clientFilter && clientFilter.order) ? clientFilter.order : (serverFilter && serverFilter.order) ? serverFilter.order : undefined; + var limit = (serverFilter && serverFilter.limit) ? serverFilter.limit : (clientFilter && clientFilter.limit) ? clientFilter.limit : undefined; + + var filter = {"where": where, "order": order, "limit": limit}; + filter = removeEmpty(filter); + + this.find(filter, function(err, states) { + (!err) ? cb(null, states) : cb(err, null); + }); + }; + }; self.rawSql = function(query, params, cb) { this.dataSource.connector.execute(query, params, function(error, response) { diff --git a/services/service/models/my-model.js b/services/service/models/my-model.js index 37f8d846b..1372fc644 100644 --- a/services/service/models/my-model.js +++ b/services/service/models/my-model.js @@ -25,7 +25,9 @@ module.exports = function(self) { for(let method in disableMethods) { //this.disableRemoteMethod(method, disableMethods[method]); } + }; + self.defineScope = function(serverFilter) { this.remoteMethod('list', { accepts: [ { @@ -43,10 +45,23 @@ module.exports = function(self) { path: '/list' } }); - - }; - self.list = function(filter, cb) {}; + this.list = function(clientFilter, cb) { + var where = {"and": []}; + (clientFilter) ? where.and.push(clientFilter.where) : undefined; + (serverFilter) ? where.and.push(serverFilter.where) : undefined; + + var order = (clientFilter && clientFilter.order) ? clientFilter.order : (serverFilter && serverFilter.order) ? serverFilter.order : undefined; + var limit = (serverFilter && serverFilter.limit) ? serverFilter.limit : (clientFilter && clientFilter.limit) ? clientFilter.limit : undefined; + + var filter = {"where": where, "order": order, "limit": limit}; + filter = removeEmpty(filter); + + this.find(filter, function(err, states) { + (!err) ? cb(null, states) : cb(err, null); + }); + }; + }; self.rawSql = function(query, params, cb) { this.dataSource.connector.execute(query, params, function(error, response) {