-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.js
194 lines (183 loc) · 5.16 KB
/
routes.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const parseBoolean = (value) => {
return value === 'true'
}
const register = (app, pool) => {
app.get('/v1/categories/count', (req, res, next) => {
pool.query(
"SELECT COUNT(*) AS counter FROM categories",
(err, rows, fields) => {
if (err) {
return next(err)
}
if (rows.length === 0) {
res.status(404).end()
return
}
res.json(rows[0])
}
)
})
app.get('/v1/collections/:collectionId/categories/count', (req, res, next) => {
pool.query(
`SELECT COUNT(DISTINCT s.category_id) AS counter
FROM collections_series cs
JOIN series s
ON s.id = cs.series_id
WHERE cs.collection_id = :collectionId`,
{
"collectionId": req.params.collectionId
},
(err, rows, fields) => {
if (err) {
return next(err)
}
if (rows.length === 0) {
res.status(404).end()
return
}
res.json(rows[0])
}
)
})
app.get('/v1/categories', (req, res, next) => {
pool.query(
`SELECT id
, name
, name_ru
, slug
, hidden
FROM categories`,
(err, rows, fields) => {
if (err) {
return next(err)
}
res.json(rows)
}
)
})
app.post('/v1/categories', (req, res, next) => {
pool.query(
`INSERT
INTO categories
( name
, name_ru
, slug
, hidden
, created_at
, created_by
, updated_at
, updated_by
)
VALUES
( :name
, :name_ru
, :slug
, :hidden
, CURRENT_TIMESTAMP
, :user_id
, CURRENT_TIMESTAMP
, :user_id
)`,
{
"name": req.body.name,
"name_ru": req.body.name_ru,
"slug": req.body.slug,
"hidden": req.body.hidden,
"user_id": req.body.user_id
},
(err, rows, fields) => {
if (err) {
return next(err)
}
res.sendStatus(204)
}
)
})
app.get('/v1/categories/search', (req, res, next) => {
pool.query(
`SELECT id
, name
, name_ru
, slug
, hidden
FROM categories
WHERE hidden = :hidden`,
{
"hidden": parseBoolean(req.query.hidden)
},
(err, rows, fields) => {
if (err) {
return next(err)
}
res.json(rows)
}
)
})
app.get('/v1/categories/:categoryId', (req, res, next) => {
pool.query(
`SELECT id
, name
, name_ru
, slug
, hidden
FROM categories
WHERE id = :categoryId`,
{
"categoryId": req.params.categoryId
},
(err, rows, fields) => {
if (err) {
return next(err)
}
if (rows.length === 0) {
res.status(404).end()
return
}
res.json(rows[0])
}
)
})
app.put('/v1/categories/:categoryId', (req, res, next) => {
pool.query(
`UPDATE categories
SET name = :name
, name_ru = :name_ru
, slug = :slug
, hidden = :hidden
, updated_at = CURRENT_TIMESTAMP
, updated_by = :user_id
WHERE id = :categoryId`,
{
"name": req.body.name,
"name_ru": req.body.name_ru,
"slug": req.body.slug,
"hidden": req.body.hidden,
"user_id": req.body.user_id,
"categoryId": req.params.categoryId
},
(err, rows, fields) => {
if (err) {
return next(err)
}
res.sendStatus(204)
}
)
})
app.delete('/v1/categories/:categoryId', (req, res, next) => {
pool.query(
`DELETE
FROM categories
WHERE id = :categoryId`,
{
"categoryId": req.params.categoryId
},
(err, rows, fields) => {
if (err) {
return next(err)
}
res.sendStatus(204)
}
)
})
}
exports.register = register