-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
149 lines (140 loc) · 5.17 KB
/
handler.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
const { nanoid } = require('nanoid');
const {
clienterror,
notfound,
successwithdata,
success,
successcreated,
successwithdataANDcount,
} = require('./response');
const connection = require('./connection');
const { senderror } = require('./senderror');
async function geterror(request, h) {
try {
const query = 'SELECT * FROM error ORDER BY createdaterror DESC';
const { languageCode } = request.params;
const setLang = languageCode === 'id' ? 'id' : 'en';
const [getallerror] = await connection.query(query);
if (request.i18n.setLocale(setLang)) {
return h.response(successwithdataANDcount(
getallerror.length,
request.i18n.__('status-finish'),
request.i18n.__('message-get-error-success'),
getallerror
)).code(200);
}
} catch (error) {
senderror(request, h, error, "geterror");
}
}
async function addBook(request, h) {
try {
const id = nanoid(50);
const createdat = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
const data = {
idbook: id,
...request.payload,
createdat: createdat,
updatedat: createdat,
};
const query = 'INSERT INTO books SET ?';
const { languageCode } = request.params;
const setLang = languageCode === 'id' ? 'id' : 'en';
const [adddata] = await connection.query(query, data);
if (request.i18n.setLocale(setLang)) {
if (adddata.affectedRows > 0) {
return h.response(
successcreated(request.i18n.__('status-finish'), request.i18n.__('message-add-success'))
).code(201);
} else {
return h.response(clienterror(request.i18n.__('message-add-fail'))).code(400);
}
}
} catch (error) {
senderror(request, h, error, "addBook");
}
}
async function getAllBooks(request, h) {
try {
const query = 'SELECT * FROM books';
const { languageCode } = request.params;
const setLang = languageCode === 'id' ? 'id' : 'en';
const [getalldata] = await connection.query(query);
if (request.i18n.setLocale(setLang)) {
return h.response(
successwithdataANDcount(getalldata.length, request.i18n.__('status-finish'), request.i18n.__('message-get-data-success'), getalldata)
).code(201);
};
} catch (error) {
senderror(request, h, error, "getAllBooks");
}
}
async function getBookById(request, h) {
try {
const { idbook, languageCode } = request.params;
const query = 'SELECT * FROM books WHERE idbook = ?';
const setLang = languageCode === 'id' ? 'id' : 'en';
const [getdatabyid] = await connection.query(query, idbook);
if (request.i18n.setLocale(setLang)) {
if (getdatabyid.length > 0) {
return h.response(successwithdata(request.i18n.__('status-finish'), request.i18n.__('message-get-data-success'), getdatabyid[0])).code(200);
} else {
return h.response(notfound(request.i18n.__('status-error'), request.i18n.__('message-get-by-id-fail'))).code(404);
}
}
} catch (error) {
senderror(request, h, error, "getBookById");
}
}
async function editBook(request, h) {
try {
const { idbook, languageCode } = request.params;
const updatedatbook = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
const data = { ...request.payload, updatedat: updatedatbook };
const setLang = languageCode === 'id' ? 'id' : 'en';
const query = 'UPDATE books SET ? WHERE idbook = ?';
const [editdata] = await connection.query(query, [data, idbook]);
if (request.i18n.setLocale(setLang)) {
if (editdata.affectedRows > 0) {
return h.response(success(request.i18n.__('status-finish'), request.i18n.__('message-update-success'))).code(200);
} else {
return h.response(notfound(request.i18n.__('status-error'), request.i18n.__('message-update-fail'))).code(404);
}
}
} catch (error) {
senderror(request, h, error, "editBook");
}
}
async function deleteBook(request, h) {
try {
const { idbook, languageCode } = request.params;
const query = 'DELETE FROM books WHERE idbook = ?';
const setLang = languageCode === 'id' ? 'id' : 'en';
const [deletedata] = await connection.query(query, idbook);
if (request.i18n.setLocale(setLang)) {
if (deletedata.affectedRows > 0) {
return h.response(success(request.i18n.__('status-finish'), request.i18n.__('message-delete-success'))).code(200);
} else {
return h.response(notfound(request.i18n.__('status-error'), request.i18n.__('message-delete-fail'))).code(404);
}
}
} catch (error) {
senderror(request, h, error, "deleteBook");
}
}
function routesOthers(req, res) {
const setLang = req.path.split('/')[1] === 'id' ? 'id' : 'en';
if (req.i18n.setLocale(setLang)) {
return res.response(notfound(req.i18n.__('status-error'), req.i18n.__('not-found'))).code(404);
}
};
const part = {
geterrorbook: geterror,
addpublicbook: addBook,
getallpublicbook: getAllBooks,
getpublicbookbyid: getBookById,
editpublicbook: editBook,
deletepublicbook: deleteBook,
besides: routesOthers,
};
module.exports = part;