1
0

posts.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const router = require('express').Router()
  2. import { Post } from '../db/models'
  3. import donate from '../donate'
  4. router.get('/', async (req: any, res: any, next: any) => {
  5. try {
  6. Post.findAll().then((p: any) => res.json(p))
  7. //Post.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. Post.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. Post.create(req.body).then((post: any) =>
  23. Post.findByIdWithIncludes(post.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. Post.findByPk(req.params.id).then((post: any) =>
  33. post
  34. .update(req.body)
  35. .then(() =>
  36. Post.findByIdWithIncludes(req.params.id).then((p: any) => res.json(p))
  37. )
  38. )
  39. } catch (err) {
  40. next(err)
  41. }
  42. })
  43. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  44. res.status(402).send(donate)
  45. try {
  46. //Post.findByPk(req.params.id).then((post:any)=>res.json(post.delete())
  47. } catch (err) {
  48. next(err)
  49. }
  50. })
  51. module.exports = router