-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
211 lines (163 loc) · 3.93 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// session 3 Nodejs
// to check if of the rquest and respnses go to Postman and make a post/delete/get/.. request with the link of your server and the appropiate path
// getting our libraries
const express = require('express')
const cors = require('cors')
const hawasProfile = {
firstName: 'Hawa',
lastName: 'Bah Bah',
preferences: {
foods: ['curry', 'chocolate'],
colour: 'blue',
number: 10
},
hoursOfSleep: 7.5
}
const db = {
profiles: {
1000: hawasProfile,
},
books: {
0: {
title: 'The hunger Games',
author: 'Suzanne Collins'
},
1: {
title: 'The fault in our stars',
author: 'John Green'
}
}
}
const app = express()
app.use(cors())
app.use(express.json()) // for parsing application/json
// GET /profiles
app.get('/profiles', (req, res) => {
res.json({
status: 'success',
data: db.profiles
})
})
app.get('/profiles/:userId', (req, res) => {
console.log(req.params.userId)
const matchingProfile = db.profiles[req.params.userId]
if (matchingProfile) {
res.json({
status: 'success',
data: matchingProfile
})
} else {
res.json({
message: "Couldn't find user with that id"
})
}
})
// POST /profiles
app.post('/profiles', (req, res) => {
// find the largest key and increment it
const existingIds = Object.keys(db.profiles)
const largestKey = Math.max(...existingIds)
const newKey = largestKey + 1
db.profiles[newKey] = req.body
res.json({
status: 'success',
message: `Created a profile with id of ${newKey}`
})
})
// DELETE profiles
app.delete('/profiles/1000', (req, res) => {
delete db.profiles[1000]
res.json({
message: 'The profile 1000 has been deleted'
})
})
app.delete('/profiles/:userId', (req, res) => {
const idToDelete =[req.params.userId]
delete db.profiles[idToDelete]
res.json({
message: 'The profile has been deleted'
})
})
//PUT
app.put('/profiles/:userId', (req, res) => {
const idToPut = req.params.userId
// updating
db.profiles[idToPut] = req.body
res.json({
message: 'The profile has been overwritten (put)'
})
})
app.patch('/profiles/:userId', (req, res) => {
const idToPatch = req.params.userId
// updating
db.profiles[idToPatch] = {...db.profiles[idToPatch],...req.body }
res.json({
message: 'The profile has been modified (path)'
})
})
app.get('/', (req, res) => {
res.json({
status: 'success',
message: 'welcome to the root of the book API',
data: db
})
})
//C POST Books
app.post('/books', (req,res)=>{
const existingBooks = Object.keys(db.books)
const largestKey = Math.max(...existingBooks)
const newKey = largestKey + 1
db.books[newKey] = req.body
res.json({
status: 'success',
message: `Created a book with id of ${newKey}`
})
})
// R GET books
app.get('/books', (req,res)=>{
res.json({
title: 'All the books',
data: db.books
})
})
app.get('/books/:bookId', (req,res)=>{
matchedBook = db.books[req.params.bookId]
if (matchedBook) {
res.json({
status: 'success',
data: matchedBook
})
} else {
res.json({
message: "Couldn't find book with that id"
})
}
})
// U PUT/PATCH books
app.put('/books/:bookId', (req, res) => {
// const bookToPut = db.books[req.params.bookId];
const idToPut = req.params.bookId
db.books[idToPut] = req.body
res.json({
status: 'succcess',
// message: 'the book ' + bookToPut + ' has been changed to ' + req.body
})
})
app.patch('/books/:bookId', (req, res) => {
const idToPatch = req.params.bookId
db.books[idToPatch] = {...db.books[idToPatch],...req.body}
res.json({
message: 'The book has been modified '
})
})
// D
app.delete('/books:bookId', (req, res) => {
delete db.books[req.params.bookId]
res.json({
message: 'the book has been deleted'
})
})
app.listen(4000, () => {
console.log('server is running!')
})
// run node index.js to start server