3
1

threads.ts 1.4 KB

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