councils.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const router = require('express').Router()
  2. import { Council } from '../db/models'
  3. import donate from '../donate'
  4. router.get('/', async (req: any, res: any, next: any) => {
  5. try {
  6. Council.findAllWithIncludes().then((m: any) => res.json(m))
  7. } catch (err) {
  8. next(err)
  9. }
  10. })
  11. router.get('/:id', async (req: any, res: any, next: any) => {
  12. try {
  13. if (req.params.id > 0)
  14. Council.findByIdWithIncludes(req.params.id).then((m: any) => res.json(m))
  15. else
  16. Council.findWithIncludes({
  17. where: { handle: req.params.id },
  18. }).then((m: any) => res.json(m))
  19. } catch (err) {
  20. next(err)
  21. }
  22. })
  23. router.post('/', async (req: any, res: any, next: any) => {
  24. res.status(402).send(donate)
  25. try {
  26. Council.create(req.body).then((member: any) =>
  27. Council.findByIdWithIncludes(member.id).then((m: any) => res.json(m))
  28. )
  29. } catch (err) {
  30. next(err)
  31. }
  32. })
  33. router.put('/:id', async (req: any, res: any, next: any) => {
  34. res.status(402).send(donate)
  35. try {
  36. Council.findByPk(req.params.id).then((member: any) =>
  37. member
  38. .update(req.body)
  39. .then(() =>
  40. Council.findByIdWithIncludes(req.params.id).then((m: any) =>
  41. res.json(m)
  42. )
  43. )
  44. )
  45. } catch (err) {
  46. next(err)
  47. }
  48. })
  49. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  50. res.status(402).send(donate)
  51. try {
  52. //Council.findByPk(req.params.id).then((member:any)=>res.json(member.delete())
  53. } catch (err) {
  54. next(err)
  55. }
  56. })
  57. module.exports = router