1
|
1 |
|
const db = require("../db/database.js"); |
2
|
|
|
|
3
|
1 |
|
const deliveries = { |
4
|
|
|
sql: "SELECT d.ROWID as id, d.productId as product_id, amount," + |
5
|
|
|
" d.deliveryDate as delivery_date, comment," + |
6
|
|
|
" p.productName as product_name" + |
7
|
|
|
" FROM deliveries d" + |
8
|
|
|
" INNER JOIN products p" + |
9
|
|
|
" ON p.ROWID = d.productId AND d.apiKey = p.apiKey" + |
10
|
|
|
" WHERE d.apiKey = ?", |
11
|
|
|
|
12
|
|
|
getDeliveries: function(res, apiKey) { |
13
|
4 |
|
db.all(deliveries.sql, apiKey, (err, rows) => { |
14
|
4 |
|
if (err) { |
15
|
|
|
return res.status(500).json({ |
16
|
|
|
errors: { |
17
|
|
|
status: 500, |
18
|
|
|
source: "/deliveries", |
19
|
|
|
title: "Database error", |
20
|
|
|
detail: err.message |
21
|
|
|
} |
22
|
|
|
}); |
23
|
|
|
} |
24
|
|
|
|
25
|
4 |
|
res.json({ data: rows }); |
|
|
|
|
26
|
|
|
}); |
27
|
|
|
}, |
28
|
|
|
|
29
|
|
|
getDelivery: function(res, deliveryId, apiKey, status=200) { |
30
|
2 |
|
db.get( |
31
|
|
|
deliveries.sql + " AND d.ROWID = ?", |
32
|
|
|
apiKey, |
33
|
|
|
deliveryId, |
34
|
|
|
function(err, row) { |
35
|
2 |
|
if (err) { |
36
|
|
|
return res.status(500).json({ |
37
|
|
|
errors: { |
38
|
|
|
status: 500, |
39
|
|
|
source: "/deliveries", |
40
|
|
|
title: "Database error", |
41
|
|
|
detail: err.message |
42
|
|
|
} |
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
res.status(status).json({ data: row }); |
|
|
|
|
47
|
|
|
}); |
48
|
|
|
}, |
49
|
|
|
|
50
|
|
|
addDelivery: function(res, body) { |
51
|
5 |
|
const sql = "INSERT INTO deliveries (productId, amount, deliveryDate," + |
52
|
|
|
" comment, apiKey) VALUES (?, ?, ?, ?, ?)"; |
53
|
|
|
|
54
|
5 |
|
db.run(sql, |
55
|
|
|
body.product_id, |
56
|
|
|
body.amount, |
57
|
|
|
body.delivery_date, |
58
|
|
|
body.comment, |
59
|
|
|
body.api_key, |
60
|
|
|
function(err) { |
61
|
5 |
|
if (err) { |
62
|
3 |
|
return res.status(500).json({ |
63
|
|
|
errors: { |
64
|
|
|
status: 500, |
65
|
|
|
source: "/delivery", |
66
|
|
|
title: "Database error", |
67
|
|
|
detail: err.message |
68
|
|
|
} |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
deliveries.getDelivery(res, this.lastID, body.api_key, 201); |
|
|
|
|
73
|
|
|
}); |
74
|
|
|
}, |
75
|
|
|
|
76
|
|
|
deleteDelivery: function(res, body) { |
77
|
2 |
|
if (Number.isInteger(parseInt(body.id))) { |
78
|
1 |
|
const sql = "DELETE FROM deliveries WHERE apiKey = ? AND ROWID = ?"; |
79
|
|
|
|
80
|
1 |
|
db.run(sql, |
81
|
|
|
body.api_key, |
82
|
|
|
body.id, (err) => { |
83
|
2 |
|
if (err) { |
84
|
|
|
return res.status(500).json({ |
85
|
|
|
errors: { |
86
|
|
|
status: 500, |
87
|
|
|
source: "/orders", |
88
|
|
|
title: "Database error", |
89
|
|
|
detail: err.message |
90
|
|
|
} |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
res.status(204).send(); |
|
|
|
|
95
|
|
|
}); |
96
|
|
|
} else { |
97
|
1 |
|
res.status(400).json({ |
98
|
|
|
errors: { |
99
|
|
|
status: 400, |
100
|
|
|
detail: "Required attribute delivery id (id)" + |
101
|
|
|
" was not included in the request." |
102
|
|
|
} |
103
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
}; |
107
|
|
|
|
108
|
|
|
module.exports = deliveries; |
109
|
|
|
|