Browse Source

refactor : count api uses where condition

pull/13/head
oof1lab 7 years ago
parent
commit
8bb91180e7
  1. 39
      lib/xapi.js
  2. 4
      lib/xsql.js
  3. 19
      tests/tests.js

39
lib/xapi.js

@ -1,10 +1,8 @@
'use strict'; 'use strict';
var Xsql = require('./xsql.js'); var Xsql = require('./xsql.js');
var whrHelp = require('./util/whereClause.helper.js');
var multer = require('multer'); var multer = require('multer');
var path = require('path'); var path = require('path');
const colors = require('colors');
//define class //define class
@ -315,18 +313,6 @@ class Xapi {
} }
async distinct(req, res) {
let queryParamsObj = {}
queryParamsObj.query = ''
queryParamsObj.params = []
this.mysql.prepareListQuery(req, res, queryParamsObj, 4);
let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params);
res.status(200).json(results);
}
async nestedList(req, res) { async nestedList(req, res) {
@ -544,12 +530,29 @@ class Xapi {
async count(req, res) { async count(req, res) {
let query = 'select count(1) as no_of_rows from ??'; let queryParams = {}
let params = [];
params.push(req.app.locals._tableName); queryParams.query = 'select count(1) as no_of_rows from ?? ';
queryParams.params = [];
let results = await this.mysql.exec(query, params); queryParams.params.push(req.app.locals._tableName);
this.mysql.getWhereClause(req.query._where,req.app.locals._tableName, queryParams, ' where ')
let results = await this.mysql.exec(queryParams.query, queryParams.params);
res.status(200).json(results);
}
async distinct(req, res) {
let queryParamsObj = {}
queryParamsObj.query = ''
queryParamsObj.params = []
this.mysql.prepareListQuery(req, res, queryParamsObj, 4);
let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params);
res.status(200).json(results); res.status(200).json(results);
} }

4
lib/xsql.js

@ -854,7 +854,7 @@ class Xsql {
/** /**
* in second join - there will be ONE table and an ON condition * in second join - there will be ONE table and an ON condition
* this if clause deals with this * if clause deals with this
* *
*/ */
@ -877,7 +877,7 @@ class Xsql {
/** /**
* in first join - there will be TWO tables and an ON condition * in first join - there will be TWO tables and an ON condition
* this else clause deals with this * else clause deals with this
*/ */

19
tests/tests.js

@ -1655,6 +1655,25 @@ describe('xmysql : tests', function () {
}); });
}); });
it('GET /api/payments/count?_where=(amount,gt,19000) should PASS', function (done) {
//post to an url with data
agent.get('/api/payments/count?_where=(amount,gt,19000)') //enter url
.expect(200)//200 for success 4xx for failure
.end(function (err, res) {
// Handle /api/v error
if (err) {
return done(err);
}
//validate response
res.body[0]['no_of_rows'].should.be.equals(196)
return done();
});
});
// it('GET /api/xjoin?_join=pl.productlines,_j,pr.products,_j,ord.orderDetails&_on1=(pl.productline,eq,pr.productline)&_on2=(pr.productcode,eq,ord.productcode) should PASS', function (done) { // it('GET /api/xjoin?_join=pl.productlines,_j,pr.products,_j,ord.orderDetails&_on1=(pl.productline,eq,pr.productline)&_on2=(pr.productcode,eq,ord.productcode) should PASS', function (done) {
// //
// //post to an url with data // //post to an url with data

Loading…
Cancel
Save