councils.ts 1.4 KB

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