-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
170 lines (138 loc) · 3.46 KB
/
Copy pathindex.js
File metadata and controls
170 lines (138 loc) · 3.46 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const express = require('express');
const helmet = require('helmet');
const knex = require('knex');
const knexConfig = {
client: 'sqlite3',
connection: {
filename: './data/lambda.sqlite3',
},
useNullAsDefault: true, // needed for sqlite
};
const db = knex(knexConfig);
const server = express();
server.use(express.json());
server.use(helmet());
/* =============== Zoos endpoints ================ */
// ============== GET Endpoints
server.get('/api/zoos/', async (req, res) => {
try {
const zoos = await db('zoos')
res.status(200).json(zoos);
} catch (error) {
res.status(500).json(error)
}
});
server.get('/api/zoos/:id', async (req, res) => {
try {
const zoo = await db('zoos')
.where({ id: req.params.id })
.first()
res.status(200).json(zoo);
} catch (error) {
res.status(500).json(error)
}
});
// ============== POST Endpoint
server.post('/api/zoos', (req, res) => {
const zoo = req.body;
db.insert(zoo)
.into("zoos")
.then( zoo => {
res.status(201).json(zoo)
})
.catch(() => {
res.status(500).json({error: "we encountered an error"})
})
});
// ============== DELETE Endpoint
server.delete('/api/zoos/:id', (req, res) => {
db('zoos')
.where({id: req.params.id})
.del()
.then(() => {
res.status(200).json({message: "zoo deleted"})
})
.catch(() => {
res.status(500).json({error: "error"})
})
});
// ============== PUT Endpoint
server.put('/api/zoos/:id', async (req, res) => {
try {
const count = await db('zoos')
.where({ id: req.params.id })
.update(req.body);
if (count > 0) {
const zoo = await db('zoos')
.where({ id: req.params.id })
.first();
res.status(200).json(zoo);
} else {
res.status(404).json({ message: 'Records not found' });
}
} catch (error) {}
});
/* =============== Bears endpoints ================ */
// ============== GET Endpoints
server.get('/api/bears/', async (req, res) => {
try {
const bears = await db('bears')
res.status(200).json(bears);
} catch (error) {
res.status(500).json(error)
}
});
server.get('/api/bears/:id', async (req, res) => {
try {
const bear = await db('bears')
.where({ id: req.params.id })
.first()
res.status(200).json(bear);
} catch (error) {
res.status(500).json(error)
}
});
// ============== POST Endpoint
server.post('/api/bears', (req, res) => {
const bear = req.body;
db.insert(bear)
.into("bears")
.then( bear => {
res.status(201).json(bear)
})
.catch(() => {
res.status(500).json({error: "we encountered an error"})
})
});
// ============== DELETE Endpoint
server.delete('/api/bears/:id', (req, res) => {
db('bears')
.where({id: req.params.id})
.del()
.then(() => {
res.status(200).json({message: "bear deleted"})
})
.catch(() => {
res.status(500).json({error: "error"})
})
});
// ============== PUT Endpoint
server.put('/api/bears/:id', async (req, res) => {
try {
const count = await db('bears')
.where({ id: req.params.id })
.update(req.body);
if (count > 0) {
const bear = await db('bears')
.where({ id: req.params.id })
.first();
res.status(200).json(bear);
} else {
res.status(404).json({ message: 'Records not found' });
}
} catch (error) {}
});
const port = 3300;
server.listen(port, function() {
console.log(`\n **** Web API Listening on http://localhost:${port} **** \n`);
});