-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
206 lines (191 loc) · 6.01 KB
/
server.js
File metadata and controls
206 lines (191 loc) · 6.01 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
195
196
197
198
199
200
201
202
203
204
205
206
const express = require('express');
const cors = require('cors');
const port = 5000;
const server = express();
server.use(express.json());
server.use(cors());
const sendUserError = (msg, res) => {
res.status(422);
res.json({ Error: msg });
return;
};
let nextId = 10;
const getNextId = () => {
return ++nextId;
};
// const gen = () => {
// const res = [];
// for(let i=0; i < 10; i++){
// nextId++;
// res.push({id: nextId, title: `This is a title for ${i + 1}`, content: `Lorem Ipsum is simply dummy
// text of the printing and typesetting industry. Lorem Ipsum has been the
// industry's standard dummy text ever since the 1500s, when an unknown printer
// took a galley of type and scrambled it to make a type specimen book.`});
// }
// return res;
// }
//
// const notes = gen();
let users = [
{
id: 1,
name: `Edward Manda`,
username: `emukupa`,
password: `secret`
},
{
id: 2,
name: `Donald Trump`,
username:`trump`,
password: `huge`
},
{
id: 1,
name: `Josh Knell`,
username: `josh`,
password: `password`
}
];
let notes = [
{
id: 1,
user: {
author: `Edward Manda`,
userId: 1,
},
title: "Javascript Lessons",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
},
{
id: 2,
user: {
author: `Edward Manda`,
userId: 1,
},
title: "CSS tricks",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
},
{
id: 3,
title: "Huge Moves",
user: {
author: `Donald Trump`,
userId: 2,
},
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
},
{
id: 4,
user: {
author: `Josh Knell`,
userId: 3,
},
title: "Master HTML",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
},
{
id: 5,
user: {
author: `Donald Trump`,
userId: 2,
},
title: "Talking about chaos",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
},
{
id: 6,
user: {
author: `Edward Manda`,
userId: 1,
},
title: "How to think",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
},
{
id: 7,
user: {
author: `Donald Trump`,
userId: 2,
},
title: "Think big",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
},
{
id: 8,
user: {
author: `John Knell`,
userId: 3,
},
title: "Laugh it out",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
},
{
id: 9,
user: {
author: `Donald Trump`,
userId: 2,
},
title: "How to win",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
},
{
id: 10,
user: {
author: `Donald Trump`,
userId: 2,
},
title: "Nick Naming",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
}
];
server.get('/notes', (req, res) => {
res.json(notes);
});
server.get('/notes/:id', (req, res) => {
const note = notes.filter(note => note.id.toString() === req.params.id)[0];
res.status(200).json(note);
});
server.post('/notes', (req, res) => {
const { title, content, user } = req.body;
const id = getNextId();
const newNote = { id:id, title:title, content:content, user: user };
notes.push(newNote);
nextId++;
res.json(notes);
});
server.put('/notes/:id', (req, res) => {
const { id } = req.params;
const noteIndex = notes.findIndex(f => f.id == id);
if (noteIndex > -1) {
const note = { ...notes[noteIndex], ...req.body };
notes = [
...notes.slice(0, noteIndex),
note,
...notes.slice(noteIndex + 1),
];
res.send(notes);
} else {
res.status(404).send({ msg: 'Note not found' });
}
});
// server.delete('/notes/:id', (req, res) => {
// const { id } = req.params;
// const foundNote = notes.find(note => (note.id).toString() === id.toString());
// console.log(id)
// if (foundNote) {
// const noteRemoved = { ...foundNote };
// notes = notes.filter(note => (note.id).toString() != id.toString());
// res.status(200).json({ noteRemoved });
// } else {
// sendUserError('No note by that ID exists in the note DB', res);
// }
// });
server.delete('/notes/:id', (req, res) => {
const { id } = req.params;
notes = notes.filter(note => note.id !== Number(id));
res.send(notes);
});
server.listen(port, err => {
if (err) console.log(err);
console.log(`server is listening on port ${port}`);
});