-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
95 lines (78 loc) · 1.97 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(bodyParser.json());
app.use(cors());
// Rough code to let us store notes in memory with out any DB complexity
const notes = {
1: {
id: 1,
title: 'Test Note',
body: 'This is a test note object that will be available by default',
created_at: Date.now(),
created_by: 'admin',
edit_history: [{
edited_at: Date.now(),
edited_by: 'A User',
}],
},
};
// Generates the next id from the global notes list.
const createNextId = () => Number.parseInt(Math.max(...Object.keys(notes).map((key) => Number.parseInt(key))), 10) + 1;
const addNote = ({ title, body, created_by }) => {
const id = createNextId();
notes[id] = {
id,
title,
body,
created_by,
created_at: Date.now(),
edit_history: [],
};
return notes[id];
};
const getNotes = () => notes;
const getNote = (id) => {
const note = getNotes()[id];
return note || null;
};
const updateNote = (id, update = {}) => {
const note = getNote(id);
const { title, body } = update;
Object.assign(note, { title, body });
if (update.edited_by) {
note.edit_history.push({
edited_by: update.edited_by,
edited_at: Date.now(),
});
}
return note;
};
const removeNote = (id) => {
if (getNote(id)) {
delete getNotes()[id];
return true;
}
return false;
};
app.post('/notes', (req, res) => {
const note = addNote(req.body);
res.json(note);
});
app.get('/notes', (req, res) => {
res.json(Object.values(getNotes()));
});
app.get('/notes/:id', (req, res) => {
const note = getNote(req.params.id);
res.json(note);
});
app.post('/notes/:id', (req, res) => {
const note = updateNote(req.params.id, req.body);
res.json(note);
});
app.delete('/notes/:id', (req, res) => {
const result = removeNote(req.params.id);
res.json(result);
});
app.listen(5000, () => console.log('Server running on port 5000'));