Skip to content

Commit cc6e78b

Browse files
author
schneiem
committed
v0.3
1 parent 13d1085 commit cc6e78b

File tree

4 files changed

+146
-10
lines changed

4 files changed

+146
-10
lines changed

.idea/ticketNodeDocker.iml

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const db = require("../models");
2+
const Ticket = db.tickets;
3+
const Op = db.Sequelize.Op;
4+
5+
// Create and Save a new Ticket
6+
exports.create = (req, res) => {
7+
// Validate request
8+
if (!req.body.qr) {
9+
res.status(400).send({
10+
message: "QR code can not be empty!"
11+
});
12+
return;
13+
}
14+
15+
// Create a Ticket
16+
const ticket = {
17+
qr: req.body.qr,
18+
status: req.body.status,
19+
mail: req.body.mail,
20+
mobile: req.body.mobile,
21+
scantime: req.body.scantime
22+
};
23+
24+
// Save Ticket in the database
25+
Ticket.create(ticket)
26+
.then(data => {
27+
res.send(data);
28+
})
29+
.catch(err => {
30+
res.status(500).send({
31+
message:
32+
err.message || "Some error occurred while creating the Ticket."
33+
});
34+
});
35+
};
36+
37+
// Retrieve all Tickets from the database.
38+
exports.findAll = (req, res) => {
39+
const qr = req.query.qr;
40+
var condition = qr ? { qr: { [Op.iLike]: `%${qr}%` } } : null;
41+
42+
Ticket.findAll({ where: condition })
43+
.then(data => {
44+
res.send(data);
45+
})
46+
.catch(err => {
47+
res.status(500).send({
48+
message:
49+
err.message || "Some error occurred while retrieving tickets."
50+
});
51+
});
52+
};
53+
54+
// Find a single Ticket with an id
55+
exports.findOne = (req, res) => {
56+
const id = req.params.id;
57+
58+
Ticket.findByPk(id)
59+
.then(data => {
60+
res.send(data);
61+
})
62+
.catch(err => {
63+
res.status(500).send({
64+
message: "Error retrieving Ticket with id=" + id
65+
});
66+
});
67+
};
68+
69+
// Update a Ticket by the id in the request
70+
exports.update = (req, res) => {
71+
const id = req.params.id;
72+
73+
Ticket.update(req.body, {
74+
where: { id: id }
75+
})
76+
.then(num => {
77+
if (num == 1) {
78+
res.send({
79+
message: "Ticket was updated successfully."
80+
});
81+
} else {
82+
res.send({
83+
message: `Cannot update Ticket with id=${id}. Maybe Ticket was not found or req.body is empty!`
84+
});
85+
}
86+
})
87+
.catch(err => {
88+
res.status(500).send({
89+
message: "Error updating Ticket with id=" + id
90+
});
91+
});
92+
};
93+
94+
// Delete a Ticket with the specified id in the request
95+
exports.delete = (req, res) => {
96+
const id = req.params.id;
97+
98+
Ticket.destroy({
99+
where: { id: id }
100+
})
101+
.then(num => {
102+
if (num == 1) {
103+
res.send({
104+
message: "Ticket was deleted successfully!"
105+
});
106+
} else {
107+
res.send({
108+
message: `Cannot delete Ticket with id=${id}. Maybe Ticket was not found!`
109+
});
110+
}
111+
})
112+
.catch(err => {
113+
res.status(500).send({
114+
message: "Could not delete Ticket with id=" + id
115+
});
116+
});
117+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = app => {
2+
const tutorials = require("../controllers/ticket.controller.js");
3+
4+
var router = require("express").Router();
5+
6+
// Create a new Tutorial
7+
router.post("/", tutorials.create);
8+
9+
// Retrieve all Tutorials
10+
router.get("/", tutorials.findAll);
11+
12+
// Retrieve all published Tutorials
13+
router.get("/published", tutorials.findAllPublished);
14+
15+
// Retrieve a single Tutorial with id
16+
router.get("/:id", tutorials.findOne);
17+
18+
// Update a Tutorial with id
19+
router.put("/:id", tutorials.update);
20+
21+
// Delete a Tutorial with id
22+
router.delete("/:id", tutorials.delete);
23+
24+
// Delete all Tutorials
25+
router.delete("/", tutorials.deleteAll);
26+
27+
app.use("/api/tutorials", router);
28+
};

ticketNode/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ db.sequelize.sync()
3030
// });
3131

3232

33-
require("./app/routes/turorial.routes")(app);
33+
require("./app/routes/ticket.routes")(app);
3434

3535
// set port, listen for requests
3636
const PORT = process.env.PORT || 8080;

0 commit comments

Comments
 (0)