|
1 | 1 | const register = (app, pool) => {
|
2 | 2 |
|
3 |
| - |
4 |
| -app.get('/v1/categories/count', (req, res) => { |
5 |
| - pool.query( |
6 |
| - 'SELECT COUNT(*) AS counter FROM categories', |
7 |
| - (err, rows, fields) => { |
8 |
| - if (err) { |
9 |
| - throw err |
10 |
| - } |
11 |
| - if (rows.length === 0) { |
12 |
| - res.status(404).end() |
13 |
| - return |
| 3 | + app.get('/v1/categories/count', (req, res) => { |
| 4 | + pool.query( |
| 5 | + 'SELECT COUNT(*) AS counter FROM categories', |
| 6 | + (err, rows, fields) => { |
| 7 | + if (err) { |
| 8 | + throw err |
| 9 | + } |
| 10 | + if (rows.length === 0) { |
| 11 | + res.status(404).end() |
| 12 | + return |
| 13 | + } |
| 14 | + res.json(rows[0]) |
14 | 15 | }
|
15 |
| - res.json(rows[0]) |
16 |
| - } |
17 |
| - ) |
18 |
| -}) |
| 16 | + ) |
| 17 | + }) |
19 | 18 |
|
20 |
| -app.get('/v1/collections/:collectionId/categories/count', (req, res) => { |
21 |
| - pool.query( |
22 |
| - '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', |
23 |
| - { "collectionId": req.params.collectionId }, |
24 |
| - (err, rows, fields) => { |
25 |
| - if (err) { |
26 |
| - throw err |
27 |
| - } |
28 |
| - if (rows.length === 0) { |
29 |
| - res.status(404).end() |
30 |
| - return |
| 19 | + app.get('/v1/collections/:collectionId/categories/count', (req, res) => { |
| 20 | + pool.query( |
| 21 | + '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', |
| 22 | + { "collectionId": req.params.collectionId }, |
| 23 | + (err, rows, fields) => { |
| 24 | + if (err) { |
| 25 | + throw err |
| 26 | + } |
| 27 | + if (rows.length === 0) { |
| 28 | + res.status(404).end() |
| 29 | + return |
| 30 | + } |
| 31 | + res.json(rows[0]) |
31 | 32 | }
|
32 |
| - res.json(rows[0]) |
33 |
| - } |
34 |
| - ) |
35 |
| -}) |
| 33 | + ) |
| 34 | + }) |
36 | 35 |
|
37 |
| -app.get('/v1/categories', (req, res) => { |
38 |
| - pool.query( |
39 |
| - 'SELECT id , name , name_ru , slug FROM categories LIMIT :limit', |
40 |
| - { "limit": req.query.limit }, |
41 |
| - (err, rows, fields) => { |
42 |
| - if (err) { |
43 |
| - throw err |
| 36 | + app.get('/v1/categories', (req, res) => { |
| 37 | + pool.query( |
| 38 | + 'SELECT id , name , name_ru , slug FROM categories LIMIT :limit', |
| 39 | + { "limit": req.query.limit }, |
| 40 | + (err, rows, fields) => { |
| 41 | + if (err) { |
| 42 | + throw err |
| 43 | + } |
| 44 | + res.json(rows) |
44 | 45 | }
|
45 |
| - res.json(rows) |
46 |
| - } |
47 |
| - ) |
48 |
| -}) |
| 46 | + ) |
| 47 | + }) |
49 | 48 |
|
50 |
| -app.post('/v1/categories', (req, res) => { |
51 |
| - pool.query( |
52 |
| - 'INSERT INTO categories ( name , name_ru , slug , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :name_ru , :slug , NOW() , :user_id , NOW() , :user_id )', |
53 |
| - { "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id }, |
54 |
| - (err, rows, fields) => { |
55 |
| - if (err) { |
56 |
| - throw err |
| 49 | + app.post('/v1/categories', (req, res) => { |
| 50 | + pool.query( |
| 51 | + 'INSERT INTO categories ( name , name_ru , slug , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :name_ru , :slug , NOW() , :user_id , NOW() , :user_id )', |
| 52 | + { "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id }, |
| 53 | + (err, rows, fields) => { |
| 54 | + if (err) { |
| 55 | + throw err |
| 56 | + } |
| 57 | + res.sendStatus(204) |
57 | 58 | }
|
58 |
| - res.sendStatus(204) |
59 |
| - } |
60 |
| - ) |
61 |
| -}) |
| 59 | + ) |
| 60 | + }) |
62 | 61 |
|
63 |
| -app.get('/v1/categories/:categoryId', (req, res) => { |
64 |
| - pool.query( |
65 |
| - 'SELECT id , name , name_ru , slug FROM categories WHERE id = :categoryId', |
66 |
| - { "categoryId": req.params.categoryId }, |
67 |
| - (err, rows, fields) => { |
68 |
| - if (err) { |
69 |
| - throw err |
| 62 | + app.get('/v1/categories/:categoryId', (req, res) => { |
| 63 | + pool.query( |
| 64 | + 'SELECT id , name , name_ru , slug FROM categories WHERE id = :categoryId', |
| 65 | + { "categoryId": req.params.categoryId }, |
| 66 | + (err, rows, fields) => { |
| 67 | + if (err) { |
| 68 | + throw err |
| 69 | + } |
| 70 | + if (rows.length === 0) { |
| 71 | + res.status(404).end() |
| 72 | + return |
| 73 | + } |
| 74 | + res.json(rows[0]) |
70 | 75 | }
|
71 |
| - if (rows.length === 0) { |
72 |
| - res.status(404).end() |
73 |
| - return |
74 |
| - } |
75 |
| - res.json(rows[0]) |
76 |
| - } |
77 |
| - ) |
78 |
| -}) |
| 76 | + ) |
| 77 | + }) |
79 | 78 |
|
80 |
| -app.put('/v1/categories/:categoryId', (req, res) => { |
81 |
| - pool.query( |
82 |
| - 'UPDATE categories SET name = :name , name_ru = :name_ru , slug = :slug , updated_at = NOW() , updated_by = :user_id WHERE id = :categoryId', |
83 |
| - { "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id, "categoryId": req.params.categoryId }, |
84 |
| - (err, rows, fields) => { |
85 |
| - if (err) { |
86 |
| - throw err |
| 79 | + app.put('/v1/categories/:categoryId', (req, res) => { |
| 80 | + pool.query( |
| 81 | + 'UPDATE categories SET name = :name , name_ru = :name_ru , slug = :slug , updated_at = NOW() , updated_by = :user_id WHERE id = :categoryId', |
| 82 | + { "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id, "categoryId": req.params.categoryId }, |
| 83 | + (err, rows, fields) => { |
| 84 | + if (err) { |
| 85 | + throw err |
| 86 | + } |
| 87 | + res.sendStatus(204) |
87 | 88 | }
|
88 |
| - res.sendStatus(204) |
89 |
| - } |
90 |
| - ) |
91 |
| -}) |
| 89 | + ) |
| 90 | + }) |
92 | 91 |
|
93 |
| -app.delete('/v1/categories/:categoryId', (req, res) => { |
94 |
| - pool.query( |
95 |
| - 'DELETE FROM categories WHERE id = :categoryId', |
96 |
| - { "categoryId": req.params.categoryId }, |
97 |
| - (err, rows, fields) => { |
98 |
| - if (err) { |
99 |
| - throw err |
| 92 | + app.delete('/v1/categories/:categoryId', (req, res) => { |
| 93 | + pool.query( |
| 94 | + 'DELETE FROM categories WHERE id = :categoryId', |
| 95 | + { "categoryId": req.params.categoryId }, |
| 96 | + (err, rows, fields) => { |
| 97 | + if (err) { |
| 98 | + throw err |
| 99 | + } |
| 100 | + res.sendStatus(204) |
100 | 101 | }
|
101 |
| - res.sendStatus(204) |
102 |
| - } |
103 |
| - ) |
104 |
| -}) |
105 |
| - |
| 102 | + ) |
| 103 | + }) |
106 | 104 |
|
107 | 105 | }
|
108 | 106 |
|
|
0 commit comments