This repository was archived by the owner on Aug 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
195 lines (161 loc) · 5.42 KB
/
index.js
File metadata and controls
195 lines (161 loc) · 5.42 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const path = require('path');
const express = require('express');
const cors = require('cors');
const { database } = require('./database');
const server = express();
const port = 3001;
server.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
server.use(express.json());
server.use(cors())
//Create a query which will give back all the posts based on one of the following:
// title starts with a given a string -> title
// body contains a given string -> body
// I also wish to limit the number of items ->limit
// Examples:
// /posts?body=the&limit=2 --> will return all posts which contain the 'the' in the body,
// but no more than 2 posts will be returned
// /posts?title=Student --> will return all posts whose title starts with 'Student'
// /posts?limit = 2 --> will return all posts, but the maximum 2
server.get('/posts', (req, res) => {
const {title, body, limit} = req.query;
let foundPosts = database.posts;
if (title !== undefined) {
foundPosts = database.posts.filter(post => post.title.startsWith(title))
}
if (body !== undefined) {
foundPosts = database.posts.filter(post => post.body.includes(body))
}
if (limit !== undefined) {
foundPosts = foundPosts.slice(0,Number(limit));
}
res.json(foundPosts);
});
server.get('/posts/:postId', (req, res) => {
const { postId } = req.params;
const post = database.posts.find(p => p.id === parseInt(postId));
if (post !== undefined) {
res.json(post)
} else {
res.status(404).send();
}
});
//1. retrieve all the posts of a given user
//What is the URL of this endpoint?
// Relation: The user has posts, and not another way around,
// and not just one it can be many (plural)
// so the endpoint will be: /users/{user_id}/posts
server.get('/users/:userId/posts', (req, res) => {
const { userId } = req.params;
const posts = database.posts.filter(p => p.user_id === parseInt(userId));
if (posts !== undefined && posts.length > 0) {
res.json(posts);
} else {
res.status(404).send();
}
});
//2. retrieve one single post of a given user
//What is the URL of this endpoint?
//Relation: The user has posts, and we need one of them (singular)
//so the endpoint will be: /users/{user_id}/posts/{post_id}
server.get('/users/:userId/posts/:postId', (req, res) => {
const { userId, postId } = req.params;
const posts = database.posts.filter(p => p.user_id === parseInt(userId));
const post = posts.find(p => p.id === parseInt(postId));
if (post !== undefined) {
res.json(post);
} else {
res.status(404).send();
}
});
//2. (Advanced) I wish to create a new post, the user_id has to be checked
//What is the URL of this endpoint?
server.post('/posts', (req, res) => {
let newPost = {
title : req.body.title,
body : req.body.body,
user_id : req.body.user_id,
created_at : new Date().toISOString()
}
if (validatePost(newPost)) { //if the user exists
let newId = database.posts[database.posts.length -1].id + 1; //create the new id
newPost.id = newId; //set the new post's id
database.posts.push(newPost); //store in the array
res.status(200).send();
} else {
res.status(400).send();
}
});
function validatePost(post) {
if (
post !== undefined &&
post.title !== undefined &&
post.title.length !== 0 &&
post.body !== undefined &&
post.body.length !== 0 &&
post.user_id !== undefined &&
database.users.find(u => u.id === parseInt(post.user_id)) !== undefined
)
return true;
return false;
}
//3. I wish to delete one single post based on its id
//What is the URL of this endpoint?
server.delete('/posts/:postId', (req, res) => {
const { postId } = req.params;
let index = database.posts.findIndex(post => post.id === parseInt(postId))
if (index !== -1) {
database.posts.splice(index,1)
res.status(200).send();
} else {
res.status(404).send();
}
});
//4. (Advanced) I wish to delete a user only if it doesn't have a post
//What is the URL of this endpoint?
// in order to be able to test it, you need to delete some posts first
server.delete('/users/:userId', (req, res) => {
const { userId } = req.params;
const userPosts = database.posts.filter(post => post.user_id === parseInt(userId));
if (userPosts.length === 0) {
let index = database.users.findIndex(user => user.id === parseInt(userId));
database.users.splice(index,1)
res.status(200).send();
} else {
res.status(400).send();
}
});
//This is the teacher led demo for the update/PUT
server.put('/posts/:postId', (req, res) => {
const { postId } = req.params;
let newPost = {
title : req.body.title,
body: req.body.body
}
let post = database.posts.find(p => p.id === parseInt(postId));
if (post !== undefined) {
if (newPost.title != undefined && newPost.body !== undefined) {
post.title = newPost.title;
post.body = newPost.body;
post.updated_at = new Date().toISOString();
res.json(post);
} else {
res.status(400).send();
}
} else {
res.status(404).send();
}
});
server.get('/users', (req, res) => {
res.json(database.users)
});
//Exercise: based on the PUT above implement an update enpoint for the users
//Requirements:
// - 404 is the user doesn't exist
// - 400 if the data validation is failed
//1. URL ?
//2. Which fields can/should be updated?
//3. Possible validations?
server.listen(port, () => console.log(`[MockServer] listening at http://localhost:${port}`));