-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (109 loc) · 3.15 KB
/
Copy pathindex.js
File metadata and controls
120 lines (109 loc) · 3.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
const express = require("express");
const {sequelize, User, Post,Comment} = require("./models");
const app = express();
app.use(express.json());
app.post('/users', async (req, res) => {
const {name, email, role} = req.body;
console.log("POST /users");
try {
const user = await User.create({name, email, role});
return res.json(user);
} catch (e) {
console.log(e);
return res.status(500).json({"message": "Something went wrong"});
}
});
app.get('/users', async (req, res) => {
try {
const users = await User.findAll();
return res.json(users);
} catch (e) {
console.log(e);
return res.status(500).json({error: 'Something went wrong'});
}
});
app.get('/users/:uuid', async (req, res) => {
const uuid = req.params.uuid;
try {
const user = await User.findOne({
where: {uuid},
include: ['posts'],
});
return res.json(user);
} catch (e) {
console.log(e);
return res.status(500).json({error: 'Something went wrong'});
}
});
app.delete('/users/:uuid', async (req, res) => {
const uuid = req.params.uuid;
try {
const user = await User.findOne({
where: {uuid},
});
await user.destroy();
return res.json({message: "User Deleted!"});
} catch (e) {
console.log(e);
return res.status(500).json({error: 'Something went wrong'});
}
});
app.put('/users/:uuid', async (req, res) => {
const uuid = req.params.uuid;
const {name, email, role} = req.body;
try {
const user = await User.findOne({
where: {uuid},
});
user.name = name;
user.email = email;
user.role = role;
await user.save();
return res.json(user);
} catch (e) {
console.log(e);
return res.status(500).json({error: 'Something went wrong'});
}
});
app.post('/posts', async (req, res) => {
const {userId, body} = req.body;
try {
const user = await User.findOne({where: {id: userId}});
const post = await Post.create({body, userId: user.id});
return res.json(post);
} catch (e) {
console.log(e);
return res.status(500).json(e);
}
});
app.get('/posts', async (req, res) => {
try {
const posts = await Post.findAll({
include: [
{model: User, as: 'user'},
{model: Comment, as: 'comments', include: ['user']}
],
});
return res.json(posts);
} catch (e) {
console.log(e);
return res.status(500).json({error: e});
}
});
app.post('/posts/:id/comment', async (req, res) => {
const {body,userId} = req.body;
const {id} = req.params;
try {
const post = await Post.findOne({where: {id: id}});
const comment = await Comment.create({body, postId: post.id,userId:userId});
return res.json(comment);
} catch (e) {
console.log(e);
return res.status(500).json(e);
}
});
app.listen({port: 5001}, async () => {
console.log('Server Listening on 5000');
await sequelize.authenticate();
//{force:true}
});