Commit 4a9c4be08a847820ce81831078d84dce290a4fca
1 parent
8d03693231
Exists in
master
Change product splitter separator to ";" and ","
Whitespace tidak lagi menjadi separator
Showing 2 changed files with 26 additions and 24 deletions Side-by-side Diff
api-server/index.js
1 | 1 | /** |
2 | 2 | * API Server |
3 | - * | |
4 | - * @todo make it work | |
5 | 3 | */ |
6 | 4 | |
7 | 5 | const express = require('express'); |
... | ... | @@ -32,15 +30,16 @@ function isValidApikey(apikey) { |
32 | 30 | function needValidApikey(req, res, next) { |
33 | 31 | if (isValidApikey(req.params.apikey)) { |
34 | 32 | next(); |
35 | - } | |
36 | - else { | |
33 | + } else { | |
37 | 34 | res.end('INVALID_APIKEY'); |
38 | 35 | } |
39 | 36 | } |
40 | 37 | |
41 | -isConfigured() && app.listen(config.apiserver.port, function () { | |
42 | - logger.info('API-SERVER listening', {port: config.apiserver.port}); | |
43 | -}); | |
38 | +if (isConfigured()) { | |
39 | + app.listen(config.apiserver.port, () => { | |
40 | + logger.info('API-SERVER listening', { port: config.apiserver.port }); | |
41 | + }); | |
42 | +} | |
44 | 43 | |
45 | 44 | // initialize xid |
46 | 45 | app.use((req, res, next) => { |
api-server/router-products.js
1 | -"use strict"; | |
1 | +/* eslint-disable no-continue */ | |
2 | 2 | |
3 | 3 | const express = require('express'); |
4 | 4 | const naturalSort = require('node-natural-sort'); |
... | ... | @@ -10,34 +10,37 @@ const matrix = require('../matrix'); |
10 | 10 | const router = express.Router(); |
11 | 11 | module.exports = router; |
12 | 12 | |
13 | +const splitProductSeparator = / *[;,]+ */; | |
14 | + | |
13 | 15 | function pageIndex(req, res) { |
14 | 16 | res.json({ |
15 | 17 | method: '/products', |
16 | 18 | error: null, |
17 | - result: config.products | |
19 | + result: config.products, | |
18 | 20 | }); |
19 | 21 | } |
20 | 22 | |
21 | 23 | function pageAdd(req, res) { |
22 | - let products = req.params.product || req.query.product | |
24 | + let products = req.params.product || req.query.product; | |
23 | 25 | |
24 | 26 | if (!products) { |
25 | 27 | res.json({ |
26 | 28 | method: '/products/add', |
27 | 29 | error: true, |
28 | - error_msg: 'Usage: /products/add/<NEW_PRODUCT>' | |
30 | + error_msg: 'Usage: /products/add/<NEW_PRODUCT>', | |
29 | 31 | }); |
30 | 32 | |
31 | 33 | return; |
32 | 34 | } |
33 | 35 | |
34 | 36 | if (typeof products === 'string') { |
35 | - products = products.trim().split(/[\s,]+/); | |
37 | + products = products.trim().split(splitProductSeparator); | |
36 | 38 | } |
37 | 39 | |
38 | 40 | const productsCount = products.length; |
39 | - for (let i=0; i<productsCount; i++) { | |
41 | + for (let i = 0; i < productsCount; i += 1) { | |
40 | 42 | const product = products[i]; |
43 | + | |
41 | 44 | if (!product.trim()) { |
42 | 45 | continue; |
43 | 46 | } |
... | ... | @@ -45,7 +48,7 @@ function pageAdd(req, res) { |
45 | 48 | config.products.push(product.trim().toUpperCase()); |
46 | 49 | } |
47 | 50 | |
48 | - config.products.map(function(x) { return x.toUpperCase(); }); | |
51 | + config.products.map((product) => product.toUpperCase()); | |
49 | 52 | unique(config.products); |
50 | 53 | config.products.sort(naturalSort()); |
51 | 54 | matrix.config_is_dirty = true; |
... | ... | @@ -54,34 +57,34 @@ function pageAdd(req, res) { |
54 | 57 | method: '/products/add', |
55 | 58 | error: null, |
56 | 59 | new_product: products, |
57 | - products: config.products | |
58 | - }) | |
60 | + products: config.products, | |
61 | + }); | |
59 | 62 | } |
60 | 63 | |
61 | 64 | function pageDel(req, res) { |
62 | - let products = req.params.product || req.query.product | |
65 | + let products = req.params.product || req.query.product; | |
63 | 66 | if (!products) { |
64 | 67 | res.json({ |
65 | 68 | method: '/products/del', |
66 | 69 | error: true, |
67 | - error_msg: 'Usage: /products/del/<PRODUCT_TO_DELETE> or /products/del?product=<PRODUCT_TO_DELETE>' | |
70 | + error_msg: 'Usage: /products/del/<PRODUCT_TO_DELETE> or /products/del?product=<PRODUCT_TO_DELETE>', | |
68 | 71 | }); |
69 | 72 | |
70 | 73 | return; |
71 | 74 | } |
72 | 75 | |
73 | 76 | if (typeof products === 'string') { |
74 | - products = products.trim().split(/[\s,]+/); | |
77 | + products = products.trim().split(splitProductSeparator); | |
75 | 78 | } |
76 | 79 | |
77 | - config.products.map(function(x) { return x.toUpperCase(); }); | |
80 | + config.products.map((product) => product.toUpperCase()); | |
78 | 81 | const productsCount = products.length; |
79 | - for (let i=0; i<productsCount; i++) { | |
82 | + for (let i = 0; i < productsCount; i += 1) { | |
80 | 83 | const product = products[i].toUpperCase(); |
81 | 84 | const idx = config.products.indexOf(product); |
82 | 85 | if (idx >= 0) { |
83 | 86 | matrix.config_is_dirty = true; |
84 | - config.products.splice(idx, 1) | |
87 | + config.products.splice(idx, 1); | |
85 | 88 | } |
86 | 89 | } |
87 | 90 | |
... | ... | @@ -89,8 +92,8 @@ function pageDel(req, res) { |
89 | 92 | method: '/products/del', |
90 | 93 | error: null, |
91 | 94 | product_to_delete: products, |
92 | - products: config.products | |
93 | - }) | |
95 | + products: config.products, | |
96 | + }); | |
94 | 97 | } |
95 | 98 | |
96 | 99 | router.get('/', pageIndex); |