-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
178 lines (155 loc) · 5.88 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
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
/*
* This script runs the boneyard server, which is built with express.js.
* It serves the static resources plus a REST API for the data.
*/
const
express = require('express'),
bodyParser = require('body-parser'),
logger = require('morgan'),
app = express();
app.use(logger('tiny'));
// the project's public/ directory is the app root
app.use(express.static('public'));
// serve bootstrap from node_modules directly
app.use('/bootstrap', express.static('node_modules/bootstrap/dist'));
app.use('/api', bodyParser.json());
// the data source used by the API
// this is not saved anywhere so it only lives as long as the server process
const appData = {
boards: [
{ id: 1, title: "Boneyard testing board" }
],
lists: [
{ id: 1, board: 1, name: "List 1" },
{ id: 2, board: 1, name: "List 2" },
{ id: 3, board: 1, name: "List 3" }
],
cards: [
{ id: 1, list: 1, title: "Card 1" },
{ id: 2, list: 1, title: "Card 2" },
{ id: 3, list: 2, title: "Card 3" },
{ id: 4, list: 2, title: "Card 4" },
{ id: 5, list: 2, title: "Card 5" },
]
};
// GETs return the full hierarchy at the requested level and below
// get a board (the UI only supports one board) plus its lists and their cards
app.get('/api/boards/:id', (req, res) => {
const boardId = Number(req.params.id);
var board = appData.boards.filter((b) => b.id === boardId)[0];
if (board) {
board = { id: board.id, title: board.title }; // create clone
board.lists = appData.lists.filter((l) => l.board === boardId)
.map((l) => {
var list = { id: l.id, board: l.board, name: l.name }; // create clone
list.cards = appData.cards.filter((c) => c.list === l.id);
return list;
});
return res.json(board);
} else {
res.status(404).json({ error: `Boards item ${boardId} not found` });
}
});
// get a list and its cards
app.get('/api/lists/:id', (req, res) => {
const listId = Number(req.params.id);
var list = appData.lists.filter((l) => l.id === listId)[0];
if (list) {
list = { id: list.id, name: list.name, board: list.board }; // create clone
list.cards = appData.cards.filter((c) => c.list === list.id);
return res.json(list);
} else {
res.status(404).json({ error: `Lists item ${listId} not found` });
}
});
// get a card
app.get('/api/cards/:id', (req, res) => {
const cardId = Number(req.params.id);
var card = appData.cards.filter((c) => c.id === cardId)[0];
if (card) {
card = { id: card.id, title: card.title, list: card.list }; // create clone
return res.json(card);
} else {
res.status(404).json({ error: `Cards item ${cardId} not found` });
}
});
// create a new item
app.post('/api/:collection', (req, res) => {
const collection = req.params.collection;
if (!appData.hasOwnProperty(collection)) {
return res.status(404).json({ error: `Collection ${collection} unknown` });
}
var item;
if (collection === 'boards') {
item = {
id: appData.boards.reduce((p,c) => Math.max(p, c.id),0) + 1,
title: req.body.title
};
appData.boards.push(item);
} else if (collection === 'lists') {
item = {
id: appData.lists.reduce((p,c) => Math.max(p, c.id),0) + 1,
name: req.body.name,
board: req.body.board
};
appData.lists.push(item);
} else if (collection === 'cards') {
item = {
id: appData.cards.reduce((p,c) => Math.max(p, c.id),0) + 1,
title: req.body.title,
list: req.body.list
};
appData.cards.push(item);
}
return res.status(201).json(item);
});
// update an item
app.patch('/api/:collection/:id', (req, res) => {
const collection = req.params.collection;
const itemId = Number(req.params.id);
if (!appData.hasOwnProperty(collection)) {
return res.status(404).json({ error: `Collection ${collection} unknown` });
}
const item = appData[collection].filter((x) => x.id === itemId)[0];
if (!item) {
return res.status(404).json({ error: `${collection} item ${itemId} unknown` });
}
if (collection === 'boards') {
item.title = req.body.title || item.title;
} else if (collection === 'lists') {
item.name = req.body.name || item.name;
item.board = req.body.board || item.board;
} else if (collection === 'cards') {
item.title = req.body.title || item.title;
item.list = req.body.list || item.list;
}
return res.json(item);
});
// delete an item and all of its child records
app.delete('/api/:collection/:id', (req, res) => {
const collection = req.params.collection;
const itemId = Number(req.params.id);
if (!appData.hasOwnProperty(collection)) {
return res.status(404).json({ error: `Collection ${collection} unknown` });
}
if (collection === 'boards') {
// delete the board plus its lists and their cards
const listsToDelete = appData.lists.filter((l) => l.board === itemId)
.map((l) => l.id);
appData.cards = appData.cards.filter((c) => !listsToDelete.includes(c.list));
appData.lists = appData.lists.filter((l) => l.board !== itemId);
appData.boards = appData.boards.filter((b) => b.id !== itemId);
} else if (collection === 'lists') {
// delete a list and its cards
appData.cards = appData.cards.filter((c) => c.list !== itemId);
appData.lists = appData.lists.filter((l) => l.id !== itemId);
} else if (collection === 'cards') {
// delete a card
appData.cards = appData.cards.filter((c) => c.id !== itemId);
}
return res.status(204).send();
});
const port = process.env.PORT || 3000
app.listen(port, function () {
console.log('Example app listening on port', port);
});