Commit 01545d19301e3762256643a264bfefb16fd93310
1 parent
057b3f4f52
Exists in
master
Generate json products
Showing 3 changed files with 79 additions and 0 deletions Side-by-side Diff
lib/db-evo.js
... | ... | @@ -0,0 +1,47 @@ |
1 | +const mysql = require('mysql'); | |
2 | + | |
3 | +const config = require('komodo-sdk/config'); | |
4 | + | |
5 | +exports.ERROR_POOL_NOT_READY = new Error('DB-EVO: pool is not ready'); | |
6 | + | |
7 | +exports.pool = config.mysql ? mysql.createPool({ | |
8 | + host: config.mysql.evo.host || 'localhost', | |
9 | + database: config.mysql.evo.database || 'komodo', | |
10 | + user: config.mysql.evo.user || 'komodo', | |
11 | + password: config.mysql.evo.password, | |
12 | +}) : null; | |
13 | + | |
14 | +exports.query = (query, values, cb) => { | |
15 | + // pool.query.apply(null, arguments); | |
16 | + if (!this.pool || !this.pool.query) { | |
17 | + if (typeof cb === 'function') { | |
18 | + cb(this.ERROR_POOL_NOT_READY); | |
19 | + } | |
20 | + | |
21 | + return; | |
22 | + } | |
23 | + | |
24 | + this.pool.query(query, values, cb); | |
25 | +}; | |
26 | + | |
27 | +exports.format = (sql, values, cb) => new Promise((resolve, reject) => { | |
28 | + if (!this.pool) { | |
29 | + reject(this.ERROR_POOL_NOT_READY); | |
30 | + if (typeof cb === 'function') cb(this.ERROR_POOL_NOT_READY); | |
31 | + return; | |
32 | + } | |
33 | + | |
34 | + this.pool.getConnection((err, connection) => { | |
35 | + if (err) { | |
36 | + reject(err); | |
37 | + if (typeof cb === 'function') cb(err); | |
38 | + return; | |
39 | + } | |
40 | + | |
41 | + const formatted = connection.format(sql, values); | |
42 | + connection.release(); | |
43 | + | |
44 | + resolve(formatted); | |
45 | + if (typeof cb === 'function') cb(null, formatted); | |
46 | + }); | |
47 | +}); |
lib/webservice/index.js
... | ... | @@ -10,8 +10,10 @@ const app = express(); |
10 | 10 | const listenPort = config.webservice.port || config.webservice.listen_port; |
11 | 11 | |
12 | 12 | const routerPriceplan = require('./router/priceplan'); |
13 | +const routerProduct = require('./router/product'); | |
13 | 14 | |
14 | 15 | app.use('/apikey/:apikey/priceplan', routerPriceplan); |
16 | +app.use('/apikey/:apikey/products', routerProduct); | |
15 | 17 | |
16 | 18 | if (CLUSTER_MODE && cluster.isMaster) { |
17 | 19 | logger.info(`Master ${process.pid} is running`); |
lib/webservice/router/product.js
... | ... | @@ -0,0 +1,30 @@ |
1 | +const express = require('express'); | |
2 | +const logger = require('komodo-sdk/logger'); | |
3 | +const dbEvo = require('../../db-evo'); | |
4 | + | |
5 | +const router = express.Router(); | |
6 | +module.exports = router; | |
7 | + | |
8 | +function pageJson(req, res) { | |
9 | + const query = ` | |
10 | + SELECT | |
11 | + UPPER(service.keyword) AS name | |
12 | + FROM product | |
13 | + LEFT JOIN service ON service.svc_id = product.svc_id | |
14 | + WHERE | |
15 | + service.svc_cat IN (1, 6) | |
16 | + ORDER BY SOUNDEX(service.keyword), LENGTH(service.keyword), service.keyword; | |
17 | + `.trim(); | |
18 | + | |
19 | + dbEvo.query(query, [], (err, result) => { | |
20 | + if (err) { | |
21 | + logger.warn(`ROUTER-PRODUCTS: DB error. ${err.toString()}`); | |
22 | + } | |
23 | + | |
24 | + const products = (result || []).map((item) => item.name); | |
25 | + | |
26 | + res.json(products); | |
27 | + }); | |
28 | +} | |
29 | + | |
30 | +router.get('/json', pageJson); |