councils.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const router = require('express').Router()
  2. import { Council } from '../db/models'
  3. router.get('/', async (req: any, res: any, next: any) => {
  4. try {
  5. Council.findAll().then((m: any) => res.json(m))
  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. try {
  25. Council.create(req.body).then((member: any) =>
  26. Council.findByIdWithIncludes(member.id).then((m: any) => res.json(m))
  27. )
  28. } catch (err) {
  29. next(err)
  30. }
  31. })
  32. router.put('/:id', async (req: any, res: any, next: any) => {
  33. try {
  34. Council.findByPk(req.params.id).then((member: any) =>
  35. member
  36. .update(req.body)
  37. .then(() =>
  38. Council.findByIdWithIncludes(req.params.id).then((m: any) =>
  39. res.json(m)
  40. )
  41. )
  42. )
  43. } catch (err) {
  44. next(err)
  45. }
  46. })
  47. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  48. try {
  49. //Council.findByPk(req.params.id).then((member:any)=>res.json(member.delete())
  50. } catch (err) {
  51. next(err)
  52. }
  53. })
  54. module.exports = router