posts.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.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. Post.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. Post.create(req.body).then((post: any) =>
  22. Post.findByIdWithIncludes(post.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. Post.findByPk(req.params.id).then((post: any) =>
  32. post
  33. .update(req.body)
  34. .then(() =>
  35. Post.findByIdWithIncludes(req.params.id).then((p: any) => res.json(p))
  36. )
  37. )
  38. } catch (err) {
  39. next(err)
  40. }
  41. })
  42. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  43. res.status(402).send(donate)
  44. try {
  45. //Post.findByPk(req.params.id).then((post:any)=>res.json(post.delete())
  46. } catch (err) {
  47. next(err)
  48. }
  49. })
  50. module.exports = router