-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (158 loc) · 5.29 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
let env = require('dotenv').config()
const express = require('express')
const { check, validationResult } = require('express-validator');
const bodyParser = require('body-parser')
var cors = require('cors')
const db = require('./database.js');
const { isValidObjectId } = require('mongoose');
const app = express()
const port = 3000
app.use(cors()) //TODO This might not be best practices. Investigate and consider more specific routing.
var jsonParser = bodyParser.json();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(jsonParser);
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
})
/**
* While multiple walls aren't really a thing, this is fine.
*/
app.get('/debug', async (req, res) => {
let content = await db.getWall();
res.send(content);
});
/**
* Get a list of walls: title, color/icon, ID.
*/
app.get("/wall", async (req, res) => {
let content = await db.getWallList();
res.send(content);
});
/**
* Get a particular wall. Wall data (like title, bg color, icon, ID) as well as the collection of embedded panels.
* param :wallId should be string of the ObjectID used
*/
app.get("/wall/:wallId", async (req, res) => {
const wallId = req.params.wallId;
let wallObject = await db.getWall(wallId);
//Get Panels
let sortedPanels = [];
for (let i = 0; i < wallObject.panelOrder.length; i++) {
let panel = wallObject.panels[wallObject.panelOrder[i]];
sortedPanels.push(panel);
}
wallObject["panels"] = sortedPanels;
res.send(wallObject);
});
/**
* Get a particular panel from a wall. Used to reset a modified (but not yet saved) panel.
*/
app.get("/wall/:wallId/:panelId", async (req, res) => {
const wallId = req.params.wallId;
const panelId = Number(req.params.panelId);
let content = await db.getPanel(wallId, panelId);
res.send(content);
});
/**
* Create a new wall. Body should include wall data (title, icon, iconColor, bgColor).
*/
app.post("/wall",
check("title").isLength({ min: 10 }).trim().escape(),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
let wallData = {
title: "Untitled Wall",
icon: null,
iconColor: "white",
bgColor: "#333333"
};
if (req.body.title != undefined)
wallData.title = req.body.title;
Object.keys(req.body).forEach((key) => {
if (key in wallData) {
wallData[key] = req.body[key];
}
});
let newWall = await db.createWall(wallData)
res.send(newWall);
});
/**
* Create a new panel on wall. Body should include panel data (sizing, type, body, title/subtitle)
*/
app.post("/wall/:wallId",
check("title").isLength({ min: 1 }).trim().escape(),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const wallId = String(req.params.wallId);
let panelData = {
title: "New Panel",
subtitle: "",
body: "",
bottomText: "",
width: "small",
height: "height",
type: "neutral"
};
Object.keys(req.body).forEach((key) => {
if (key in panelData) {
panelData[key] = req.body[key];
}
});
let newPanel = await db.createPanel(wallId, panelData);
res.send(newPanel);
});
/**
* Update the wall data. May be called in two spots (wall editor and on the general wall view page) to handle wall data as well
* as panel layouts.
*/
app.put("/wall/:wallId", async (req, res) => {
const validWallFields = ["title", "icon", "iconColor", "bgColor", "panelOrder"];
const wallId = String(req.params.wallId);
let wallUpdateData = {};
Object.keys(req.body).forEach((key) => {
if (validWallFields.includes(key))
wallUpdateData[key] = req.body[key];
});
let result = await db.updateWall(wallId, wallUpdateData);
res.send(result);
});
/**
* Update a particular panel on a particular wall. Might have (sub)title, body text, sizing, type changes.
*/
app.put("/wall/:wallId/:panelId", async (req, res) => {
const validPanelFields = ["id", "title", "subtitle", "body", "bottomText", "width", "height", "type"];
const wallId = String(req.params.wallId);
const panelId = Number(req.params.panelId);
let panelUpdateData = {};
Object.keys(req.body).forEach((key) => {
if (validPanelFields.includes(key))
panelUpdateData[key] = req.body[key];
});
let result = await db.updatePanel(wallId, panelId, panelUpdateData);
res.send(result);
});
/**
* Delete an entire wall.
*/
app.delete("/wall/:wallId", async (req, res) => {
const wallId = String(req.params.wallId);
let result = await db.deleteWall(wallId);
res.send(result);
});
/**
* Delete a particular panel on a wall.
*/
app.delete("/wall/:wallId/:panelId", async (req, res) => {
const wallId = String(req.params.wallId);
const panelId = String(req.params.panelId);
let result = await db.deletePanel(wallId, panelId);
return res.json(result);
});