forked from Bavid-Dowie/Iterator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
187 lines (167 loc) · 4.98 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
179
180
181
182
183
184
185
186
187
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const { genToken, hashPassword, checkPassword, restrict } = require('./services/auth')
const app = express()
app.use(cors())
app.use("/", express.static("./build/"));
app.use(bodyParser.json())
const {User, Article} = require ('./models')
const PORT = process.env.PORT || 3001
app.get('/', (req, res) => res.send('Sup yo'))
const buildAuthResponse = (user) => {
const token_data = {
id: user.id,
username: user.username,
name: user.name,
email: user.email,
bio: user.bio,
photo: user.photo
};
const token = genToken(token_data);
return { token }
}
app.get('/users', async (req, res) => {
try {
const allUsers = await User.findAll({raw: true})
res.json(allUsers)
} catch (error) {
res.status(500).json({
message: error.message
})
}
})
app.get('/articles', async (req, res) => {
try {
const allArticles = await Article.findAll({raw: true})
res.json(allArticles)
} catch (error) {
res.status(500).json({
message: error.message
})
}
})
app.get('/users/:username', async (req, res) => {
try {
const username = req.params.username
const oneUser = await User.findAll({where: {username: username}})
if (!oneUser) throw Error ('User not found')
res.json(oneUser)
} catch (error) {
res.status(500).json({
message: error.message
})
}
})
app.post('/users/register', async (req, res) => {
try {
const password_digest = await hashPassword(req.body.password);
const newUser = await User.create({
username: req.body.username,
name: req.body.name,
email: req.body.email,
bio: req.body.bio,
photo: req.body.photo,
password_digest
})
const respData = buildAuthResponse(newUser);
res.json(respData)
} catch (error) {
res.status(500).json({message: error.message})
}
})
app.post('/users/login', async (req, res) => {
try {
const user = await User.findOne({
where: {
username: req.body.username
}
})
if (await checkPassword(req.body.password, user.password_digest)) {
const respData = buildAuthResponse(user)
res.json(respData)
} else {
res.status(401).send('Invalid Credentials')
}
} catch (error) {
res.status(500).json({message: error.message})
}
})
app.post('/articles', async (req, res) => {
try {
const newArticle = await Article.create(req.body)
res.json(newArticle)
} catch (error) {
res.status(500).json({message: error.message})
}
})
app.get('/articles/:id', async (req, res) => {
try {
const articleId = parseInt(req.params.id)
const oneArticle = await Article.findByPk(articleId)
res.json(oneArticle)
} catch (error) {
res.status(500).json({message: error.message})
}
})
app.get('/userarticles/:userId', async (req, res) => {
try {
const userId = parseInt(req.params.userId)
const userAritcles = await Article.findAll({where: {userId: userId}})
res.json(userAritcles)
} catch (error) {
res.status(500).json({message: error.message})
}
})
app.put('/users/:id', async (req, res) => {
try {
const userId = req.params.id
const updatedUser = {
name: req.body.name,
email: req.body.email,
username: req.body.username,
bio: req.body.bio,
photo: req.body.photo
}
const user = await User.update(updatedUser, {where: {id: userId}})
res.json(user)
} catch (error) {
res.status(500).json({message: error.message})
}
})
app.put('/articles/:id', async (req, res) => {
try {
const articleId = parseInt(req.params.id)
const updateArticle = {
title: req.body.title,
author: req.body.author,
content: req.body.content,
userId: req.body.userId
}
const article = await Article.update(updateArticle, {where: {id: articleId}})
res.json(article)
} catch (error) {
res.status(500)
}
})
app.delete('/users/:id', async (req, res) => {
try {
const id = req.params.id;
const user = await User.destroy({ where: {id: id} });
res.json(user);
} catch (error) {
console.error(error);
res.status(500).json({ message: error.message});
}
});
app.delete('/articles/:id', async (req, res) => {
try {
const id = req.params.id;
const article = await Article.destroy({ where: {id: id} });
res.json(article);
} catch (error) {
console.error(error);
res.status(500).json({ message: error.message});
}
});
app.listen(PORT, () => console.log(`Iterator listening on port ${PORT}`))