From 8bb91180e7e28d90bea9d38c53754c95eb671963 Mon Sep 17 00:00:00 2001 From: oof1lab Date: Sun, 19 Nov 2017 17:12:56 +0530 Subject: [PATCH] refactor : count api uses where condition --- lib/xapi.js | 39 +++++++++++++++++++++------------------ lib/xsql.js | 4 ++-- tests/tests.js | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/lib/xapi.js b/lib/xapi.js index a2b652ae7d..e8f766d62b 100644 --- a/lib/xapi.js +++ b/lib/xapi.js @@ -1,10 +1,8 @@ 'use strict'; var Xsql = require('./xsql.js'); -var whrHelp = require('./util/whereClause.helper.js'); var multer = require('multer'); var path = require('path'); -const colors = require('colors'); //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) { @@ -544,12 +530,29 @@ class Xapi { async count(req, res) { - let query = 'select count(1) as no_of_rows from ??'; - let params = []; + let queryParams = {} - 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); } diff --git a/lib/xsql.js b/lib/xsql.js index c9e284b783..f19d44b318 100644 --- a/lib/xsql.js +++ b/lib/xsql.js @@ -854,7 +854,7 @@ class Xsql { /** * 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 - * this else clause deals with this + * else clause deals with this */ diff --git a/tests/tests.js b/tests/tests.js index 9fb9cc6cd4..fb8818df10 100644 --- a/tests/tests.js +++ b/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) { // // //post to an url with data