|
| 1 | +import { json, Router } from 'express'; |
| 2 | +import { Task } from 'interface'; |
| 3 | +import pg from 'pg'; |
| 4 | +import { z } from 'zod'; |
| 5 | + |
| 6 | +const TaskArray = z.array(Task); |
| 7 | + |
| 8 | +export function taskRouter(client: pg.Pool, router = Router()) { |
| 9 | + router.use(json()); |
| 10 | + |
| 11 | + router.get('/', async (req, res) => { |
| 12 | + const response = await client.query( |
| 13 | + 'SELECT * FROM tasks LIMIT $1::integer', |
| 14 | + [req.query.limit || 10], |
| 15 | + ); |
| 16 | + |
| 17 | + const rows = await TaskArray.parseAsync(response.rows); |
| 18 | + |
| 19 | + res.status(200).json(rows); |
| 20 | + }); |
| 21 | + |
| 22 | + router.get('/:id', async (req, res) => { |
| 23 | + const response = await client.query( |
| 24 | + 'SELECT * FROM tasks WHERE id = $1::integer', |
| 25 | + [req.params.id], |
| 26 | + ); |
| 27 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 28 | + const [element] = response.rows; |
| 29 | + if (!element) { |
| 30 | + res.status(404).json({ message: 'Not found' }); |
| 31 | + return; |
| 32 | + } |
| 33 | + const row = await Task.parseAsync(element); |
| 34 | + res.status(200).json(row); |
| 35 | + }); |
| 36 | + |
| 37 | + router.delete('/:id', async (req, res) => { |
| 38 | + const response = await client.query( |
| 39 | + 'DELETE FROM tasks WHERE id = $1::integer', |
| 40 | + [req.params.id], |
| 41 | + ); |
| 42 | + res.status(200).json({ rowsDeleted: response.rowCount }); |
| 43 | + }); |
| 44 | + |
| 45 | + router.post('/:id', async (req, res) => { |
| 46 | + const task = await Task.parseAsync(req.body); |
| 47 | + const response = await client.query( |
| 48 | + 'UPDATE tasks SET name = $1::text, etag = etag + 1 WHERE id = $2::integer AND etag = $3::integer RETURNING etag', |
| 49 | + [task.name, req.params.id, task.etag], |
| 50 | + ); |
| 51 | + |
| 52 | + if (response.rowCount === 0) { |
| 53 | + res.status(409).json({ message: 'Conflict: etag does not match' }); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const returnedTask = await Task.parseAsync({ |
| 58 | + ...req.body, |
| 59 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access |
| 60 | + etag: response.rows[0].etag, |
| 61 | + }); |
| 62 | + res.status(200).json(returnedTask); |
| 63 | + }); |
| 64 | + |
| 65 | + router.post('/', async (req, res) => { |
| 66 | + const task = await Task.parseAsync(req.body); |
| 67 | + const result = await client.query( |
| 68 | + 'INSERT INTO tasks (name, etag) VALUES ($1::text, $2::text) RETURNING id', |
| 69 | + [task.name, task.etag], |
| 70 | + ); |
| 71 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access |
| 72 | + const response = { ...req.body, id: result.rows[0].id }; |
| 73 | + res.status(201).send(await Task.parseAsync(response)); |
| 74 | + }); |
| 75 | + |
| 76 | + router.get('/debug/populate', async (req, res) => { |
| 77 | + const n = req.query.n; |
| 78 | + let amount = 10; |
| 79 | + if (typeof n === 'string') { |
| 80 | + amount = parseInt(n, 10); |
| 81 | + } |
| 82 | + |
| 83 | + const promises = Array(amount) |
| 84 | + .fill(0) |
| 85 | + .map((_, i) => { |
| 86 | + return client.query('INSERT INTO tasks (name) VALUES ($1::text)', [ |
| 87 | + `Task ${i}`, |
| 88 | + ]); |
| 89 | + }); |
| 90 | + |
| 91 | + const count = (await Promise.all(promises)) |
| 92 | + .map((r) => r.rowCount || 0) |
| 93 | + .reduce((a, b) => a + b, 0); |
| 94 | + |
| 95 | + res.status(200).json({ message: 'Populated', count }); |
| 96 | + }); |
| 97 | + return router; |
| 98 | +} |
0 commit comments