proposals.ts 1.4 KB

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