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