|
| 1 | +import { Request as ExpressRequest, Response, NextFunction } from 'express'; |
| 2 | +import { BAD_REQUEST, NOT_FOUND, INTERNAL_SERVER_ERROR } from 'http-status-codes'; |
| 3 | +import { HttpError } from '@boringcodes/utils/dist/error'; |
| 4 | + |
| 5 | +import { NAME } from './constants'; |
| 6 | + |
| 7 | +type Request = ExpressRequest & { |
| 8 | + readonly [NAME]: any, |
| 9 | +}; |
| 10 | + |
| 11 | +const list = async (_: Request, res: Response, next: NextFunction) => { |
| 12 | + try { |
| 13 | + // TODO: list things |
| 14 | + const things = []; |
| 15 | + |
| 16 | + res.send(things); |
| 17 | + } catch (err) { |
| 18 | + next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err)); |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +const count = async (_: Request, res: Response, next: NextFunction) => { |
| 23 | + try { |
| 24 | + // TODO: count things |
| 25 | + const count = 0; |
| 26 | + |
| 27 | + res.send({ count }); |
| 28 | + } catch (err) { |
| 29 | + next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err)); |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +const create = async (_: Request, res: Response, next: NextFunction) => { |
| 34 | + try { |
| 35 | + // TODO: create thing |
| 36 | + const thing = {}; |
| 37 | + |
| 38 | + res.send(thing); |
| 39 | + } catch (err) { |
| 40 | + next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err)); |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +const getById = async (req: Request, _: Response, next: NextFunction) => { |
| 45 | + if (!req.params.id) { |
| 46 | + next(new HttpError(BAD_REQUEST, 'Invalid resource Id')); |
| 47 | + |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + // TODO: get thing |
| 53 | + const thing = {}; |
| 54 | + |
| 55 | + if (!thing) { |
| 56 | + next(new HttpError(NOT_FOUND, 'Resource not found')); |
| 57 | + |
| 58 | + return; |
| 59 | + } |
| 60 | + // tslint:disable-next-line:no-object-mutation |
| 61 | + Object.assign(req, { [NAME]: thing }); |
| 62 | + |
| 63 | + next(); |
| 64 | + } catch (err) { |
| 65 | + next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err)); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +const get = (req: Request, res: Response) => { |
| 70 | + res.send(req[NAME]); |
| 71 | +}; |
| 72 | + |
| 73 | +const update = async (req: Request, res: Response, next: NextFunction) => { |
| 74 | + try { |
| 75 | + // TODO: update thing |
| 76 | + const thing = req[NAME]; |
| 77 | + |
| 78 | + res.send(thing); |
| 79 | + } catch (err) { |
| 80 | + next(err); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +const del = async (req: Request, res: Response, next: NextFunction) => { |
| 85 | + try { |
| 86 | + // TODO: delete thing |
| 87 | + const thing = req[NAME]; |
| 88 | + |
| 89 | + res.send(thing); |
| 90 | + } catch (err) { |
| 91 | + next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err)); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +export { |
| 96 | + list, |
| 97 | + create, |
| 98 | + count, |
| 99 | + getById, |
| 100 | + get, |
| 101 | + update, |
| 102 | + del, |
| 103 | +}; |
0 commit comments