-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (94 loc) · 2.6 KB
/
index.js
File metadata and controls
111 lines (94 loc) · 2.6 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
const express = require("express");
const bodyParser = require("body-parser");
const User = require("./models/User.js");
const Post = require("./models/Post.js");
const PostSerializer = require("./serializers/PostSerializer");
const postSerializer = new PostSerializer();
const postsSerializer = new PostSerializer(many=true);
const PORT = 8080;
const app = express();
app.use(bodyParser.json());
app.get("/users", async (req, res) => {
try {
const users = await User.query.all();
return res.json(users);
} catch (err) {
console.error(err);
return res.json(err)
}
});
app.get("^/users/:id([0-9]+)", async (req, res) => {
const {id} = req.params;
const user = await User.query.get(id);
if (user) {
return res.json(user);
}
return res.status(404).json();
});
app.post("/users", async (req, res) => {
const user = await User.create(req.body);
await user.save();
return res.json(user);
});
app.patch("/users/:id([0-9]+)", async (req, res) => {
const {id} = req.params;
let user = await User.query.get(id);
if (user) {
Object.assign(user, req.body);
user = await user.save();
return res.json(user);
}
return res.status(404).json();
});
app.delete("/users/:id([0-9]+)", async (req, res) => {
const {id} = req.params;
const user = await User.query.get(id);
if (user) {
await user.delete();
return res.json(user);
}
return res.status(404).json();
});
app.get("/posts", async(req, res) => {
const posts = await Post.query.all();
const response = postsSerializer.dump(posts);
return res.json(response);
});
app.get("^/posts/:id([0-9]+)", async(req, res) => {
const {id} = req.params;
const post = await Post.query.get(id);
if (post) {
const response = postSerializer.dump(post);
return res.json(response);
}
return res.status(404).json();
});
app.post("/posts", async(req, res) => {
const post = await Post.create(req.body);
await post.save();
const response = postSerializer.dump(post);
return res.json(response);
});
app.patch("^/posts/:id([0-9]+)", async (req, res) => {
const {id} = req.params;
let post = await Post.query.get(id);
if (post) {
Object.assign(post, req.body);
post = await post.save();
const response = postSerializer.dump(post);
return res.json(response);
}
return res.status(404).json();
});
app.delete("^/posts/:id([0-9]+)", async (req, res) => {
const {id} = req.params;
const post = await Post.query.get(id);
if (post) {
await post.delete();
return res.json(post);
}
return res.status(404).json();
})
app.listen(PORT, () => {
console.log("listening on port", PORT);
});