-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_Kunde.js
127 lines (112 loc) · 4.15 KB
/
rest_Kunde.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
* Kunde REST handlers
*/
exports.register = (router, db, logger) => {
// GET without ID: list items
router.get("/kunde", function(req, res, next) {
logger.debug("GET kunde");
db.query(
"SELECT KNR, Name, Adresse, Telefonnr FROM Kunde",
(err, results, fields) => {
if (err)
res.status(500).send(JSON.stringify(err));
else
res.send(results);
}
);
});
// GET with ID: get one item
router.get("/kunde/:KNR", function(req, res, next) {
logger.debug("GET kunde/" + req.params.KNR);
db.query(
"SELECT KNR, Name, Adresse, Telefonnr FROM Kunde WHERE KNR=?",
[ req.params.KNR ],
(err, results, fields) => {
if (err)
res.status(500).send(JSON.stringify(err));
else
res.send(results);
}
);
});
// POST without ID: create with auto-assigned ID
router.post("/kunde", function(req, res, next) {
logger.debug("POST kunde");
db.query(
"INSERT INTO Kunde (Name, Adresse, Telefonnr) VALUES (?, ?, ?)",
["Name", "Adresse", "Telefonnr"].map((a) => req.body[a]),
(err, results, fields) => {
if (err)
res.status(409).send(JSON.stringify(err));
else
res.append("Location", "/kunde/" + results.insertId).send(JSON.stringify({ KNR: results.insertId}));
}
);
});
// POST with ID: create with ID
router.post("/kunde/:KNR", function(req, res, next) {
logger.debug("POST kunde/" + req.params.KNR);
db.query(
"INSERT INTO Kunde (Name, Adresse, Telefonnr, KNR) VALUES (?, ?, ?, ?)",
["Name", "Adresse", "Telefonnr"].map((a) => req.body[a]).concat(req.params.KNR),
(err, results, fields) => {
if (err)
res.status(409).send(JSON.stringify(err));
else
res.status(204).end();
}
);
});
// PUT without ID: replace everything
router.put("/kunde", function(req, res, next) {
logger.debug("PUT kunde");
res.status(405).end();
});
// PUT with ID: replace one item
router.put("/kunde/:KNR", function(req, res, next) {
logger.debug("PUT kunde/" + req.params.KNR);
res.status(405).end();
});
// PATCH without ID: modify everything
router.patch("/kunde", function(req, res, next) {
logger.debug("PATCH kunde");
res.status(405).end();
});
// PATCH with ID: modify one item
router.patch("/kunde/:KNR", function(req, res, next) {
logger.debug("PATCH kunde/" + req.params.KNR);
var args = ["Name", "Adresse", "Telefonnr"].map((a) => req.body[a]).concat(req.params.KNR);
logger.debug("query body: " + JSON.stringify(req.body));
logger.debug("query args: " + JSON.stringify(args));
db.query(
"UPDATE Kunde SET Name=?, Adresse=?, Telefonnr=? WHERE KNR=?",
["Name", "Adresse", "Telefonnr"].map((a) => req.body[a]).concat(req.params.KNR),
(err, results, fields) => {
if (err)
res.status(500).send(JSON.stringify(err));
else
res.status(204).end();
}
);
});
// DELETE without ID: delete everything
router.delete("/kunde", function(req, res, next) {
logger.debug("DELETE kunde");
res.status(405).end();
});
// DELETE with ID: delete one item
router.delete("/kunde/:KNR", function(req, res, next) {
logger.debug("DELETE kunde/" + req.params.KNR);
db.query(
"DELETE FROM Kunde WHERE KNR=?",
[ req.params.KNR ],
(err, results, fields) => {
if (err)
res.status(500).send(JSON.stringify(err));
else
res.status(204).end();
}
);
});
}
// vim: set sw=4 ts=4 et: