1
0

proposals.ts 1.3 KB

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