councils.ts 1.6 KB

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