From 50e086fbc0e7014378df63b8749ac9f4aec94750 Mon Sep 17 00:00:00 2001 From: nelo Date: Tue, 20 Jun 2017 13:06:13 +0200 Subject: [PATCH 1/2] list for my model --- services/client/common/models/my-model.js | 22 ++++++++++++++++ .../methods/ticket-state/change-state.js | 13 +++------- .../methods/warehouse/list-warehouses.js | 25 +++++++------------ services/production/common/models/my-model.js | 22 ++++++++++++++++ services/service/models/my-model.js | 22 ++++++++++++++++ 5 files changed, 79 insertions(+), 25 deletions(-) diff --git a/services/client/common/models/my-model.js b/services/client/common/models/my-model.js index f6dfad090..37f8d846b 100644 --- a/services/client/common/models/my-model.js +++ b/services/client/common/models/my-model.js @@ -25,7 +25,29 @@ module.exports = function(self) { for(let method in disableMethods) { //this.disableRemoteMethod(method, disableMethods[method]); } + + this.remoteMethod('list', { + accepts: [ + { + arg: 'filter', + type: 'object', + description: 'Filter defining where' + } + ], + returns: { + type: [this.modelName], + root: true + }, + http: { + verb: 'get', + path: '/list' + } + }); + }; + + self.list = function(filter, cb) {}; + self.rawSql = function(query, params, cb) { this.dataSource.connector.execute(query, params, function(error, response) { cb(error, response); diff --git a/services/production/common/methods/ticket-state/change-state.js b/services/production/common/methods/ticket-state/change-state.js index 687b8ba22..5384ba86f 100644 --- a/services/production/common/methods/ticket-state/change-state.js +++ b/services/production/common/methods/ticket-state/change-state.js @@ -2,13 +2,6 @@ module.exports = function(TicketState) { TicketState.remoteMethodCtx('changeState', { description: 'Change state of tickets', accepts: [ - { - arg: 'tickets', - type: 'array', - required: true, - description: 'Array of tickets', - http: {source: 'path'} - }, { arg: 'state', type: 'number', @@ -23,11 +16,13 @@ module.exports = function(TicketState) { }, http: { verb: 'put', - path: '/:tickets/:state/changeState' + path: '/:state/changeState' } }); - TicketState.changeState = function(ctx, tickets, state, cb) { + TicketState.changeState = function(ctx, state, cb) { + var tickets = ctx.req.body.tickets; + TicketState.connectToService(ctx, "client"); TicketState.app.models.Employee.findOne({where: {userFk: ctx.req.accessToken.userId}}, function(err, emp){ diff --git a/services/production/common/methods/warehouse/list-warehouses.js b/services/production/common/methods/warehouse/list-warehouses.js index 9b3c315be..c1a2413ee 100644 --- a/services/production/common/methods/warehouse/list-warehouses.js +++ b/services/production/common/methods/warehouse/list-warehouses.js @@ -1,18 +1,6 @@ module.exports = function(Warehouse) { - Warehouse.remoteMethod('list', { - description: 'List production warehouses', - returns: { - arg: 'warehouses', - type: 'array' - }, - http: { - verb: 'get', - path: '/list' - } - }); - - Warehouse.list = function(cb) { - Warehouse.find(where(), function(err, warehouses){ + Warehouse.list = function(filter, cb) { + Warehouse.find(where(filter), function(err, warehouses){ if(!err) cb(null, warehouses); else @@ -20,7 +8,12 @@ module.exports = function(Warehouse) { }); }; - var where = function(){ - return {"where": {"tpv": {"neq": 0}}} + 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/models/my-model.js b/services/production/common/models/my-model.js index f6dfad090..37f8d846b 100644 --- a/services/production/common/models/my-model.js +++ b/services/production/common/models/my-model.js @@ -25,7 +25,29 @@ module.exports = function(self) { for(let method in disableMethods) { //this.disableRemoteMethod(method, disableMethods[method]); } + + this.remoteMethod('list', { + accepts: [ + { + arg: 'filter', + type: 'object', + description: 'Filter defining where' + } + ], + returns: { + type: [this.modelName], + root: true + }, + http: { + verb: 'get', + path: '/list' + } + }); + }; + + self.list = function(filter, cb) {}; + self.rawSql = function(query, params, cb) { this.dataSource.connector.execute(query, params, function(error, response) { cb(error, response); diff --git a/services/service/models/my-model.js b/services/service/models/my-model.js index f6dfad090..37f8d846b 100644 --- a/services/service/models/my-model.js +++ b/services/service/models/my-model.js @@ -25,7 +25,29 @@ module.exports = function(self) { for(let method in disableMethods) { //this.disableRemoteMethod(method, disableMethods[method]); } + + this.remoteMethod('list', { + accepts: [ + { + arg: 'filter', + type: 'object', + description: 'Filter defining where' + } + ], + returns: { + type: [this.modelName], + root: true + }, + http: { + verb: 'get', + path: '/list' + } + }); + }; + + self.list = function(filter, cb) {}; + self.rawSql = function(query, params, cb) { this.dataSource.connector.execute(query, params, function(error, response) { cb(error, response); From 509dab5ce4add2ddae047a298c0535773897c5dc Mon Sep 17 00:00:00 2001 From: nelo Date: Tue, 20 Jun 2017 13:14:20 +0200 Subject: [PATCH 2/2] list states --- .../production/common/methods/state/find.js | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/services/production/common/methods/state/find.js b/services/production/common/methods/state/find.js index f40937891..af8a7c217 100644 --- a/services/production/common/methods/state/find.js +++ b/services/production/common/methods/state/find.js @@ -1,30 +1,25 @@ module.exports = function(State) { - State.remoteMethod('productionStates', { - description: 'Get production states', - returns: { - arg: 'states', - type: 'object' - }, - http: { - verb: 'get', - path: '/productionStates' - } - }); - - State.productionStates = function(cb) { - State.find(where(), function(err, states) { + State.list = function(filter, cb) { + State.find(where(filter), function(err, states) { if(!err){ cb(null, states); } }); }; - function where() { - return { - where: { - "order": {gt: 0} - }, - order: "order, name" + function where(filter) { + + if(!filter) + return { + where: { + "order": {gt: 0 } + }, + order: "order, name" + } + else{ + filter.where.order = {gt: 0 }; + filter.order = "order, name"; + return filter; } }