forked from pokers/backend_template
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbookShelfController.js
More file actions
97 lines (83 loc) · 2.07 KB
/
bookShelfController.js
File metadata and controls
97 lines (83 loc) · 2.07 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
const { BookShelfService, MissingRequestParameter } = require("../services");
// 책 등록
const registerBookController = async (ctx) => {
const bookShelfService = new BookShelfService();
const {
title,
content,
authors,
publisher,
translators,
thumbnail_url,
isbn,
total_page,
} = ctx.request.body;
// check if any of the book data is missing
if (
Object.values({
title,
content,
authors,
publisher,
translators,
thumbnail_url,
isbn,
total_page,
}).some((value) => !value)
) {
throw new MissingRequestParameter("Some of the book information");
}
const user_id = ctx.request.headers.user_id;
// check user_id value
if (!user_id) {
throw new MissingRequestParameter("user_id");
}
const bookData = {
user_id,
title,
content,
authors,
publisher,
translators,
thumbnail_url,
isbn,
total_page,
meta: {
requestId: ctx.state.requestId,
now: +new Date(),
},
};
ctx.body = await bookShelfService.registerBook(bookData);
};
// 책 삭제
const deleteBookController = async (ctx) => {
const bookShelfService = new BookShelfService();
const { bookId } = ctx.params;
// check if book_id is missing
if (!bookId) {
throw new MissingRequestParameter("bookId");
}
const user_id = ctx.request.headers.user_id;
// check user_id value
if (!user_id) {
throw new MissingRequestParameter("user_id");
}
const bookData = {
user_id,
book_id: bookId,
meta: {
requestId: ctx.state.requestId,
now: +new Date(),
},
};
const result = await bookShelfService.deleteBook(bookData);
if (result === 1) {
ctx.body = "SUCCESS";
} else {
ctx.body = "FAIL";
}
};
module.exports = {
registerBookController,
deleteBookController,
};