Conditions | 1 |
Paths | 1 |
Total Lines | 61 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
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"); |
||
149 | function copyOrders(res, apiKey) { |
||
150 | let sql = "INSERT INTO orders" + |
||
151 | " (orderId," + |
||
152 | " customerName," + |
||
153 | " customerAddress," + |
||
154 | " customerZip," + |
||
155 | " customerCity," + |
||
156 | " customerCountry," + |
||
157 | " statusId," + |
||
158 | " apiKey)" + |
||
159 | " SELECT orderId," + |
||
160 | " customerName," + |
||
161 | " customerAddress," + |
||
162 | " customerZip," + |
||
163 | " customerCity," + |
||
164 | " customerCountry," + |
||
165 | " statusId," + |
||
166 | "'" + apiKey + "'" + |
||
167 | " FROM orders" + |
||
168 | " WHERE apiKey = ?"; |
||
169 | |||
170 | db.run(sql, copyApiKey, (err) => { |
||
171 | if (err) { |
||
172 | return res.status(500).json({ |
||
173 | errors: { |
||
174 | status: 500, |
||
175 | source: "/copy_orders", |
||
176 | title: "Database error", |
||
177 | detail: err.message |
||
178 | } |
||
179 | }); |
||
180 | } else { |
||
181 | let orderItemsSQL = "INSERT INTO order_items" + |
||
182 | " (orderId," + |
||
183 | " productId," + |
||
184 | " amount," + |
||
185 | " apiKey)" + |
||
186 | " SELECT orderId," + |
||
187 | " productId," + |
||
188 | " amount," + |
||
189 | "'" + apiKey + "'" + |
||
190 | " FROM order_items" + |
||
191 | " WHERE apiKey = ?"; |
||
192 | |||
193 | db.run(orderItemsSQL, copyApiKey, (err) => { |
||
194 | if (err) { |
||
195 | return res.status(500).json({ |
||
196 | errors: { |
||
197 | status: 500, |
||
198 | source: "/copy_orders", |
||
199 | title: "Database error in order_items", |
||
200 | detail: err.message |
||
201 | } |
||
202 | }); |
||
203 | } else { |
||
204 | orders.getAllOrders(res, apiKey, 201); |
||
205 | } |
||
206 | }); |
||
207 | } |
||
208 | }); |
||
209 | } |
||
210 | |||
219 |