-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
184 lines (152 loc) · 5.15 KB
/
Copy pathserver.js
File metadata and controls
184 lines (152 loc) · 5.15 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
import express from 'express'
import { bugService } from './services/bug.service.js'
import { userService } from './services/user.service.js'
import { authService } from './services/auth.service.js'
import { loggerService } from './services/logger.service.js'
import cookieParser from 'cookie-parser'
const app = express()
app.use(express.static('public'))
app.use(cookieParser())
app.use(express.json())
// Read
app.get('/api/bug', (req, res) => {
const queryOptions = parseQueryParams(req.query)
// const { txt = '', minSeverity = 0, pageIdx } = req.query
// const filterBy = { txt, minSeverity: +minSeverity, pageIdx }
bugService.query(queryOptions)
.then(bugs => res.send(bugs))
.catch(err => {
loggerService.error('Cannot get bugs', err)
res.status(500).send('Cannot load bugs')
})
})
function parseQueryParams(queryParams) {
const filterBy = {
txt: queryParams.txt || '',
minSeverity: +queryParams.minSeverity || 0,
labels: queryParams.labels || [],
}
const sortBy = {
sortField: queryParams.sortField || '',
sortDir: +queryParams.sortDir || 1,
}
const pagination = {
pageIdx: queryParams.pageIdx !== undefined ? +queryParams.pageIdx || 0 : queryParams.pageIdx,
pageSize: +queryParams.pageSize || 3,
}
return { filterBy, sortBy, pagination }
}
// Create
app.post('/api/bug/', (req, res) => {
const { title, description, severity, labels } = req.body
if (!title || severity !== undefined) res.status(400).send('Missing required fields')
const bug = {
title,
description,
severity: +severity || 1,
labels: labels || [],
}
bugService.save(bug)
.then(savedBug => res.send(savedBug))
.catch(err => {
loggerService.error('Cannot add bugs', err)
res.status(500).send('Cannot add bug')
})
})
// Update
app.put('/api/bug/:bugId', (req, res) => {
const { title, description, severity, labels, _id } = req.body
if (!_id || !title || severity === undefined) return res.status(400).send('Missing required fields')
const bug = {
_id,
title,
description,
severity: +severity,
labels: labels || [],
}
bugService.save(bug)
.then(savedBug => res.send(savedBug))
.catch(err => {
loggerService.error('Cannot update bugs', err)
res.status(500).send('Cannot update bug')
})
})
// Get/Read by id
app.get('/api/bug/:bugId', (req, res) => {
const { bugId } = req.params
let visitedBugs = req.cookies.visitedBugs || []
if (visitedBugs.length >= 3) return res.status(401).send('Wait for a bit')
if (!visitedBugs.includes(bugId)) visitedBugs.push(bugId)
console.log('User visited at the following bugs:', visitedBugs)
res.cookie('visitedBugs', visitedBugs, { maxAge: 7 * 1000 })
bugService.getById(bugId)
.then(bug => res.send(bug))
.catch(err => {
loggerService.error('Cannot get bug', err)
res.status(500).send('Cannot load bug')
})
})
//* Remove/Delete
app.delete('/api/bug/:bugId', (req, res) => {
const { bugId } = req.params
bugService.remove(bugId)
.then(() => res.send('Bug Removed'))
.catch(err => {
loggerService.error('Cannot remove bug', err)
res.status(500).send('Cannot remove bug')
})
})
//* ------------------- Auth API -------------------
app.post('/api/auth/signup', (req, res) => {
const credentials = req.body
console.log('credentials:', credentials)
userService.signup(credentials)
.then(user => {
const loginToken = authService.getLoginToken(user)
res.cookie('loginToken', loginToken)
res.send(user)
})
.catch(err => {
loggerService.error('Cannot signup', err)
res.status(401).send('Cannot signup')
})
})
app.post('/api/auth/login', (req, res) => {
const credentials = {
username: req.body.username,
password: req.body.password,
}
authService.checkLogin(credentials)
.then(user => {
const loginToken = authService.getLoginToken(user)
res.cookie('loginToken', loginToken)
res.send(user)
})
.catch(err => {
loggerService.error('Cannot login', err)
res.status(401).send('Cannot login')
})
})
app.post('/api/auth/logout', (req, res) => {
res.clearCookie('loginToken')
res.send('Logged out')
})
// User API
app.get('/api/user', (req, res) => {
userService.query()
.then(users => res.send(users))
.catch(err => {
loggerService.error('Cannot load users', err)
res.status(400).send('Cannot load users')
})
})
app.get('/api/user/:userId', (req, res) => {
const { userId } = req.params
userService.getById(userId)
.then(user => res.send(user))
.catch(err => {
loggerService.error('Cannot load user', err)
res.status(400).send('Cannot load user')
})
})
app.listen(3030, () => loggerService.info('Server ready at port 3030'))