Conditions | 1 |
Paths | 1 |
Total Lines | 291 |
Code Lines | 206 |
Lines | 125 |
Ratio | 42.96 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | const db = require("../db/database.js"); |
||
3 | module.exports = (function () { |
||
4 | const dataFields = "orderId as id, customerName as name," + |
||
5 | " customerAddress as address," + |
||
6 | " customerZip as zip, customerCity as city," + |
||
7 | " customerCountry as country, s.status, s.id as status_id"; |
||
8 | |||
9 | const orderItemsDataFields = "oi.productId as product_id, oi.amount," + |
||
10 | " p.articleNumber as article_number, p.productName as name," + |
||
11 | " p.productDescription as description, p.productSpecifiers as specifiers," + |
||
12 | " p.stock, p.location, (p.price/100) as price"; |
||
13 | |||
14 | function getAllOrders(res, apiKey, status=200) { |
||
15 | let orders = { data: []}; |
||
16 | |||
17 | db.all("SELECT " + dataFields + |
||
18 | " FROM orders o INNER JOIN status s ON s.id = o.statusId" + |
||
19 | " WHERE o.apiKey = ?", |
||
20 | apiKey, (err, orderRows) => { |
||
21 | if (err) { |
||
22 | return res.status(500).json({ |
||
23 | errors: { |
||
24 | status: 500, |
||
25 | source: "/orders", |
||
26 | title: "Database error", |
||
27 | detail: err.message |
||
28 | } |
||
29 | }); |
||
30 | } |
||
31 | |||
32 | if (orderRows.length === 0) { |
||
33 | return res.status(status).json(orders); |
||
34 | } |
||
35 | |||
36 | View Code Duplication | orderRows.forEach(function(order) { |
|
|
|||
37 | db.all("SELECT " + orderItemsDataFields + " FROM order_items oi " + |
||
38 | "INNER JOIN products p ON oi.productId=p.productId AND oi.apiKey=p.apiKey" + |
||
39 | " WHERE oi.apiKey = ? AND oi.orderId = ?", |
||
40 | apiKey, |
||
41 | order.id, (err, orderItemRows) => { |
||
42 | if (err) { |
||
43 | return res.status(500).json({ |
||
44 | errors: { |
||
45 | status: 500, |
||
46 | source: "/orders order_items", |
||
47 | title: "Database error", |
||
48 | detail: err.message |
||
49 | } |
||
50 | }); |
||
51 | } |
||
52 | |||
53 | order.order_items = orderItemRows; |
||
54 | orders.data.push(order); |
||
55 | |||
56 | if (orders.data.length === orderRows.length) { |
||
57 | res.status(status).json(orders); |
||
58 | } |
||
59 | }); |
||
60 | }); |
||
61 | }); |
||
62 | } |
||
63 | |||
64 | function getOrder(res, apiKey, orderId) { |
||
65 | if (Number.isInteger(parseInt(orderId))) { |
||
66 | db.get("SELECT " + dataFields + |
||
67 | " FROM orders o INNER JOIN status s ON s.id = o.statusId" + |
||
68 | " WHERE o.apiKey = ? AND orderId = ?", |
||
69 | apiKey, |
||
70 | orderId, (err, order) => { |
||
71 | if (err) { |
||
72 | return res.status(500).json({ |
||
73 | errors: { |
||
74 | status: 500, |
||
75 | source: "/order/" + orderId, |
||
76 | title: "Database error", |
||
77 | detail: err.message |
||
78 | } |
||
79 | }); |
||
80 | } |
||
81 | |||
82 | if (order === undefined) { |
||
83 | return res.json({ data: {} }); |
||
84 | } |
||
85 | |||
86 | order.order_items = []; |
||
87 | db.each("SELECT " + orderItemsDataFields + " FROM order_items oi " + |
||
88 | " INNER JOIN products p ON oi.productId=p.productId" + |
||
89 | " AND oi.apiKey=p.apiKey" + |
||
90 | " WHERE oi.apiKey = ? AND oi.orderId = ?", |
||
91 | apiKey, |
||
92 | order.id, (err, orderItemRow) => { |
||
93 | if (err) { |
||
94 | return res.status(500).json({ |
||
95 | errors: { |
||
96 | status: 500, |
||
97 | source: "/order/" + orderId + " order_items", |
||
98 | title: "Database error", |
||
99 | detail: err.message |
||
100 | } |
||
101 | }); |
||
102 | } |
||
103 | |||
104 | order.order_items.push(orderItemRow); |
||
105 | }, function () { |
||
106 | res.json({ data: order }); |
||
107 | }); |
||
108 | }); |
||
109 | } else { |
||
110 | res.status(400).json({ |
||
111 | errors: { |
||
112 | status: 400, |
||
113 | detail: "Required attribute order id " + |
||
114 | " is not an integer." |
||
115 | } |
||
116 | }); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | function searchOrder(res, apiKey, query) { |
||
121 | const searchQuery = "%" + query + "%"; |
||
122 | let orders = { data: []}; |
||
123 | |||
124 | db.all("SELECT " + dataFields + |
||
125 | " FROM orders o INNER JOIN status s ON s.id = o.statusId" + |
||
126 | " WHERE o.apiKey = ? AND" + |
||
127 | "(customerName LIKE ? OR customerAddress LIKE ? OR customerZip LIKE ?" + |
||
128 | " OR customerCity LIKE ? OR customerCountry LIKE ?)", |
||
129 | apiKey, |
||
130 | searchQuery, |
||
131 | searchQuery, |
||
132 | searchQuery, |
||
133 | searchQuery, |
||
134 | searchQuery, (err, orderRows) => { |
||
135 | if (err) { |
||
136 | return res.status(500).json({ |
||
137 | errors: { |
||
138 | status: 500, |
||
139 | source: "/order/search/" + query, |
||
140 | title: "Database error", |
||
141 | detail: err.message |
||
142 | } |
||
143 | }); |
||
144 | } |
||
145 | |||
146 | if (orderRows.length === 0) { |
||
147 | return res.json(orders); |
||
148 | } |
||
149 | |||
150 | View Code Duplication | orderRows.forEach(function(order) { |
|
151 | db.all("SELECT " + orderItemsDataFields + " FROM order_items oi " + |
||
152 | " INNER JOIN products p ON oi.productId=p.productId" + |
||
153 | " AND oi.apiKey=p.apiKey WHERE oi.apiKey = ? AND oi.orderId = ?", |
||
154 | apiKey, |
||
155 | order.id, (err, orderItemRows) => { |
||
156 | if (err) { |
||
157 | return res.status(500).json({ |
||
158 | errors: { |
||
159 | status: 500, |
||
160 | source: "/order/search/" + query + " order_items", |
||
161 | title: "Database error", |
||
162 | detail: err.message |
||
163 | } |
||
164 | }); |
||
165 | } |
||
166 | |||
167 | order.order_items = orderItemRows; |
||
168 | orders.data.push(order); |
||
169 | |||
170 | if (orders.data.length === orderRows.length) { |
||
171 | res.json(orders); |
||
172 | } |
||
173 | }); |
||
174 | }); |
||
175 | }); |
||
176 | } |
||
177 | |||
178 | function addOrder(res, body) { |
||
179 | db.run("INSERT INTO orders (orderId, customerName, customerAddress, customerZip," + |
||
180 | " customerCity, customerCountry, statusId, apiKey) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", |
||
181 | body.id, |
||
182 | body.name, |
||
183 | body.address, |
||
184 | body.zip, |
||
185 | body.city, |
||
186 | body.country, |
||
187 | body.status_id || 100, |
||
188 | body.api_key, (err) => { |
||
189 | if (err) { |
||
190 | return res.status(500).json({ |
||
191 | errors: { |
||
192 | status: 500, |
||
193 | source: "POST /order", |
||
194 | title: "Database error", |
||
195 | detail: err.message |
||
196 | } |
||
197 | }); |
||
198 | } else { |
||
199 | res.status(201).json({ data: body }); |
||
200 | } |
||
201 | }); |
||
202 | } |
||
203 | |||
204 | function updateOrder(res, body) { |
||
205 | View Code Duplication | if (Number.isInteger(parseInt(body.id))) { |
|
206 | db.run("UPDATE orders SET customerName = ?, customerAddress = ?, customerZip = ?," + |
||
207 | " customerCity = ?, customerCountry = ?, statusId = ?" + |
||
208 | " WHERE apiKey = ? AND orderId = ?", |
||
209 | body.name, |
||
210 | body.address, |
||
211 | body.zip, |
||
212 | body.city, |
||
213 | body.country, |
||
214 | body.status_id || 100, |
||
215 | body.api_key, |
||
216 | body.id, (err) => { |
||
217 | if (err) { |
||
218 | return res.status(500).json({ |
||
219 | errors: { |
||
220 | status: 500, |
||
221 | source: "PUT /order", |
||
222 | title: "Database error", |
||
223 | detail: err.message |
||
224 | } |
||
225 | }); |
||
226 | } else { |
||
227 | res.status(204).send(); |
||
228 | } |
||
229 | }); |
||
230 | } else { |
||
231 | res.status(400).json({ |
||
232 | errors: { |
||
233 | status: 400, |
||
234 | detail: "Required attribute order id (id) " + |
||
235 | " was not included in the request." |
||
236 | } |
||
237 | }); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | function deleteOrder(res, body) { |
||
242 | View Code Duplication | if (Number.isInteger(parseInt(body.id))) { |
|
243 | db.run("DELETE FROM orders WHERE apiKey = ? AND orderId = ?", |
||
244 | body.api_key, |
||
245 | body.id, (err) => { |
||
246 | if (err) { |
||
247 | return res.status(500).json({ |
||
248 | errors: { |
||
249 | status: 500, |
||
250 | source: "DELETE /order", |
||
251 | title: "Database error", |
||
252 | detail: err.message |
||
253 | } |
||
254 | }); |
||
255 | } else { |
||
256 | db.run("DELETE FROM order_items WHERE apiKey = ? AND orderId = ?", |
||
257 | body.api_key, |
||
258 | body.id, (err) => { |
||
259 | if (err) { |
||
260 | return res.status(500).json({ |
||
261 | errors: { |
||
262 | status: 500, |
||
263 | source: "DELETE /order order_items", |
||
264 | title: "Database error", |
||
265 | detail: err.message |
||
266 | } |
||
267 | }); |
||
268 | } else { |
||
269 | res.status(204).send(); |
||
270 | } |
||
271 | }); |
||
272 | } |
||
273 | }); |
||
274 | } else { |
||
275 | res.status(400).json({ |
||
276 | errors: { |
||
277 | status: 400, |
||
278 | detail: "Required attribute order id (id) " + |
||
279 | " was not included in the request." |
||
280 | } |
||
281 | }); |
||
282 | } |
||
283 | } |
||
284 | |||
285 | return { |
||
286 | getAllOrders: getAllOrders, |
||
287 | getOrder: getOrder, |
||
288 | searchOrder: searchOrder, |
||
289 | addOrder: addOrder, |
||
290 | updateOrder: updateOrder, |
||
291 | deleteOrder: deleteOrder, |
||
292 | }; |
||
293 | }()); |
||
294 |