/
index.js
134 lines (120 loc) 路 3.08 KB
/
index.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
128
129
130
131
132
133
134
const express = require("express");
const db = require("./data/db");
const bodyParser = require("body-parser");
const helmet = require("helmet");
const server = express();
const PORT = process.env.PORT || 7000;
server.use(helmet());
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
// API Homepage
server.get("/", (req, res) => {
res.send("Hello, world!");
});
// Server listening
server.listen(PORT, () =>
console.log(`Server running on http://localhost:${PORT}`)
);
////////// SIGNALS //////////
// GET all signals
server.get("/api/signals", (req, res) => {
db("signals")
.then(signals => {
res.json(signals);
})
.catch(err => {
res.status(500).json({ message: "Failed to get all signals!" });
});
});
// GET signal by id
server.get("/api/signals/:id", (req, res) => {
const { id } = req.params;
db("signals")
.where({ id })
.then(signal => {
if (signal.length) {
res.json(signal);
} else {
res
.status(404)
.json({ message: "Could not find signal with given id." });
}
})
.catch(err => {
res.status(500).json({ message: "Failed to get signal." });
});
});
// POST signal
server.post("/api/signals", (req, res) => {
const signal = req.body;
db("signals")
.insert(signal)
.then(success => {
res.send(`Signal has been successfully saved!`);
})
.catch(err => {
res.status(500).json({ message: "Failed to save signal!" });
});
});
// PUT signal by id
server.put("/api/signals/:id", (req, res) => {
const { id } = req.params;
const signal = req.body;
db("signals")
.where({ id })
.update(signal)
.then(success => {
if (success === 1) {
res.send(`Signal has been successfully updated!`);
} else {
res
.status(404)
.json({ message: "Could not find signal with given id." });
}
})
.catch(err => {
res.status(500).json({ message: "Failed to update signal!" });
});
});
// DELETE signal by id
server.delete("/api/signals/:id", (req, res) => {
const { id } = req.params;
db("signals")
.where({ id })
.del()
.then(success => {
if (success === 1) {
res.send(`Signal has been successfully deleted!`);
} else {
res
.status(404)
.json({ message: "Could not find signal with given id." });
}
})
.catch(err => {
res.status(500).json({ message: "Failed to delete signal!" });
});
});
////////// COMPANIES //////////
// GET all companies
server.get("/api/companies", (req, res) => {
db("companies")
.then(companies => {
res.json(companies);
})
.catch(err => {
res.status(500).json({ message: "Failed to get all companies!" });
});
});
// POST company
server.post("/api/companies", (req, res) => {
const signal = req.body;
db("companies")
.insert(signal)
.then(success => {
res.send(`Company has been successfully saved!`);
})
.catch(err => {
res.status(500).json({ message: "Failed to save company!" });
});
});