From 2e7a225249c4d52f9e2a3a2a088bbb20a0f134c2 Mon Sep 17 00:00:00 2001 From: nelo Date: Thu, 29 Jun 2017 11:48:57 +0200 Subject: [PATCH] rawsql with promises --- .../src/production-actions/production-actions.js | 2 +- services/client/common/models/my-model.js | 16 +++++++++++----- .../common/methods/ticket/change-time.js | 16 +++++----------- services/production/common/models/my-model.js | 16 +++++++++++----- services/service/models/my-model.js | 16 +++++++++++----- 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/client/production/src/production-actions/production-actions.js b/client/production/src/production-actions/production-actions.js index d7c7d912b..79552ce6b 100644 --- a/client/production/src/production-actions/production-actions.js +++ b/client/production/src/production-actions/production-actions.js @@ -55,7 +55,7 @@ export default class ProductionActions { ); } _changeTime(ids, time, index) { - this.$http.put(`/production/api/changeTime?time=${time}`, {tickets: ids}).then( + this.$http.put(`/production/api/Tickets/${time}/changeTime`, {tickets: ids}).then( () => { index.forEach( i => { diff --git a/services/client/common/models/my-model.js b/services/client/common/models/my-model.js index 754e538a8..8f3294622 100644 --- a/services/client/common/models/my-model.js +++ b/services/client/common/models/my-model.js @@ -79,10 +79,16 @@ module.exports = function(self) { }; self.rawSql = function(query, params, cb) { - this.dataSource.connector.execute(query, params, function(error, response) { - if (error) - cb(error, null); - return response; + var connector = this.dataSource.connector; + return new Promise(function(resolve, reject) { + connector.execute(query, params, function(error, response) { + if (error && !reject) + cb(error, null); + else if (error && reject) + reject(error); + else + resolve(response); + }); }); }; @@ -163,7 +169,7 @@ module.exports = function(self) { returnValues(); } }); - this.count(filter.where, function(err, totalCount){ + this.count(filter.where, function(err, totalCount) { if (err) { error(); } else { diff --git a/services/production/common/methods/ticket/change-time.js b/services/production/common/methods/ticket/change-time.js index feca79c59..f6ff225bb 100644 --- a/services/production/common/methods/ticket/change-time.js +++ b/services/production/common/methods/ticket/change-time.js @@ -20,11 +20,6 @@ module.exports = function(Ticket) { Ticket.changeTime = function(ctx, time, cb) { var tickets = ctx.req.body.tickets; - changeTime(tickets, time, cb); - }; - - var changeTime = function(tickets, time, cb){ - var FakeProduction = Ticket.app.models.FakeProduction; var hour = `${time}:00`; @@ -34,11 +29,10 @@ module.exports = function(Ticket) { FakeProduction.updateAll({ticketFk: {inq: tickets}}, {hour: hour}, function(err, res){ if(err) cb(err, null) - else{ - var response = Ticket.rawSql(query, params, cb); - cb(null, response); - } + else + Ticket.rawSql(query, params, cb).then(function(response) { + cb(null, response); + }); }); - - } + }; } \ 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 754e538a8..8f3294622 100644 --- a/services/production/common/models/my-model.js +++ b/services/production/common/models/my-model.js @@ -79,10 +79,16 @@ module.exports = function(self) { }; self.rawSql = function(query, params, cb) { - this.dataSource.connector.execute(query, params, function(error, response) { - if (error) - cb(error, null); - return response; + var connector = this.dataSource.connector; + return new Promise(function(resolve, reject) { + connector.execute(query, params, function(error, response) { + if (error && !reject) + cb(error, null); + else if (error && reject) + reject(error); + else + resolve(response); + }); }); }; @@ -163,7 +169,7 @@ module.exports = function(self) { returnValues(); } }); - this.count(filter.where, function(err, totalCount){ + this.count(filter.where, function(err, totalCount) { if (err) { error(); } else { diff --git a/services/service/models/my-model.js b/services/service/models/my-model.js index 754e538a8..8f3294622 100644 --- a/services/service/models/my-model.js +++ b/services/service/models/my-model.js @@ -79,10 +79,16 @@ module.exports = function(self) { }; self.rawSql = function(query, params, cb) { - this.dataSource.connector.execute(query, params, function(error, response) { - if (error) - cb(error, null); - return response; + var connector = this.dataSource.connector; + return new Promise(function(resolve, reject) { + connector.execute(query, params, function(error, response) { + if (error && !reject) + cb(error, null); + else if (error && reject) + reject(error); + else + resolve(response); + }); }); }; @@ -163,7 +169,7 @@ module.exports = function(self) { returnValues(); } }); - this.count(filter.where, function(err, totalCount){ + this.count(filter.where, function(err, totalCount) { if (err) { error(); } else {