1
|
1 |
View Code Duplication |
const db = require("../db/database.js"); |
|
|
|
|
2
|
|
|
|
3
|
1 |
|
module.exports = (function () { |
4
|
|
|
function addOrderItem(res, body) { |
5
|
4 |
|
db.run("INSERT INTO order_items (orderId, productId, amount, apiKey) VALUES (?, ?, ?, ?)", |
6
|
|
|
body.order_id, |
7
|
|
|
body.product_id, |
8
|
|
|
body.amount, |
9
|
|
|
body.api_key, (err) => { |
10
|
4 |
|
if (err) { |
11
|
3 |
|
return res.status(500).json({ |
12
|
|
|
errors: { |
13
|
|
|
status: 500, |
14
|
|
|
source: "POST /order_item", |
15
|
|
|
title: "Database error", |
16
|
|
|
detail: err.message |
17
|
|
|
} |
18
|
|
|
}); |
19
|
|
|
} else { |
|
|
|
|
20
|
1 |
|
res.status(201).json({ data: body }); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
}); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function updateOrderItem(res, body) { |
26
|
4 |
|
if (Number.isInteger(parseInt(body.order_id)) && |
27
|
|
|
Number.isInteger(parseInt(body.product_id))) { |
28
|
1 |
|
db.run("UPDATE order_items SET orderId = ?, productId = ?, amount = ?" + |
29
|
|
|
" WHERE apiKey = ? AND orderId = ? AND productId = ?", |
30
|
|
|
body.order_id, |
31
|
|
|
body.product_id, |
32
|
|
|
body.amount, |
33
|
|
|
body.api_key, |
34
|
|
|
body.order_id, |
35
|
|
|
body.product_id, (err) => { |
36
|
2 |
|
if (err) { |
37
|
|
|
return res.status(500).json({ |
38
|
|
|
errors: { |
39
|
|
|
status: 500, |
40
|
|
|
source: "PUT /order_item", |
41
|
|
|
title: "Database error", |
42
|
|
|
detail: err.message |
43
|
|
|
} |
44
|
|
|
}); |
45
|
|
|
} else { |
|
|
|
|
46
|
1 |
|
res.status(204).send(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
}); |
49
|
|
|
} else { |
50
|
2 |
|
res.status(400).json({ |
51
|
|
|
errors: { |
52
|
|
|
status: 400, |
53
|
|
|
detail: "Required attributes order id and product id" + |
54
|
|
|
" was not included in the request." |
55
|
|
|
} |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
function deleteOrderItem(res, body) { |
61
|
4 |
|
if (Number.isInteger(parseInt(body.order_id)) && |
62
|
|
|
Number.isInteger(parseInt(body.product_id))) { |
63
|
1 |
|
db.run("DELETE FROM order_items WHERE apiKey = ? AND orderId = ? AND productId = ?", |
64
|
|
|
body.api_key, |
65
|
|
|
body.order_id, |
66
|
|
|
body.product_id, (err) => { |
67
|
2 |
|
if (err) { |
68
|
|
|
return res.status(500).json({ |
69
|
|
|
errors: { |
70
|
|
|
status: 500, |
71
|
|
|
source: "DELETE /order_item", |
72
|
|
|
title: "Database error", |
73
|
|
|
detail: err.message |
74
|
|
|
} |
75
|
|
|
}); |
76
|
|
|
} else { |
|
|
|
|
77
|
1 |
|
res.status(204).send(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
}); |
80
|
|
|
} else { |
81
|
2 |
|
res.status(400).json({ |
82
|
|
|
errors: { |
83
|
|
|
status: 400, |
84
|
|
|
detail: "Required attributes order id and product id" + |
85
|
|
|
" was not included in the request." |
86
|
|
|
} |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return { |
92
|
|
|
|
93
|
|
|
addOrderItem: addOrderItem, |
94
|
|
|
updateOrderItem: updateOrderItem, |
95
|
|
|
deleteOrderItem: deleteOrderItem, |
96
|
|
|
}; |
97
|
|
|
}()); |
98
|
|
|
|