-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver_db.js
More file actions
116 lines (104 loc) · 4.01 KB
/
server_db.js
File metadata and controls
116 lines (104 loc) · 4.01 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
const Koa = require('koa');
const Router = require('koa-router');
const koaBody = require('koa-body');
const mongo = require('koa-mongo');
const app = new Koa();
const router = new Router();
app.use(mongo({
host: 'localhost',
port: 27017,
db: 'blog',
}))
app.use(koaBody());
router
.post('/article', async ctx => {
// 把資料分別存在 title、body、author 等變數
const { title } = ctx.request.body;
const { body } = ctx.request.body;
const { author } = ctx.request.body;
if (title && body && author) {
// 如果必填資料都有,就塞進 DB 裡面。然後依照文件回傳 201
const data = await ctx.db.collection('articles').insertOne({
title,
body,
author,
time: new Date(),
});
ctx.status = 201;
ctx.body = data.insertedId;
} else {
// 如果有欄位沒有填,就依照文件回傳 400
ctx.status = 400;
}
})
.put('/article/:id', async ctx => {
// 把資料分別存在 id、title、body、author 等變數
const id = ctx.params.id;
const { title } = ctx.request.body;
const { body } = ctx.request.body;
const { author } = ctx.request.body;
if (title && body && author) {
// 如果必填資料都有,就編輯文章
// 首先找出文章
const article = await ctx.db.collection('articles').findOne({_id: mongo.ObjectId(id)});
if (article) {
// 如果有文章的話就編輯,並依照文件回傳 204
article.title = title;
article.body = body;
article.author = author;
article.time = new Date();
await ctx.db.collection('articles').updateOne({_id: mongo.ObjectId(id)}, {$set: {
title,
body,
author,
time: new Date(),
}});
ctx.status = 204;
} else {
// 沒有找到的話就依照文件回傳 404
ctx.status = 404;
}
} else {
// 如果有欄位沒有填,就依照文件回傳 400
ctx.status = 400;
}
})
.get('/article/:id', async ctx => {
// 把資料分別存在 id 變數
const id = ctx.params.id;
if (id) {
// 首先找出文章
const article = await ctx.db.collection('articles').findOne({_id: mongo.ObjectId(id)});
if (article) {
// 如果有文章的話就依照文件回傳文章內容(預設就是狀態 200)
ctx.body = article;
} else {
// 沒有找到的話就依照文件回傳 404
ctx.status = 404;
}
} else {
// 如果沒送 id,文章就不存在,就依照文件回傳 404
ctx.status = 404;
}
})
.delete('/article/:id', async ctx => {
// 把資料分別存在 id 變數
const id = ctx.params.id;
if (id) {
// 首先找出文章
const article = await ctx.db.collection('articles').findOne({_id: mongo.ObjectId(id)});
if (article) {
// 如果有文章的話就刪除文章,然後依照文件回傳 204
await ctx.db.collection('articles').remove({_id: mongo.ObjectId(id)});
ctx.status = 204;
} else {
// 沒有找到的話就依照文件回傳 404
ctx.status = 404;
}
} else {
// 如果沒送 id,文章就不存在,就依照文件回傳 404
ctx.status = 404;
}
});
app.use(router.routes());
app.listen(3000);