categories.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const router = require('express').Router()
  2. import { Category } from '../db/models'
  3. import donate from '../donate'
  4. router.get('/', async (req: any, res: any, next: any) => {
  5. try {
  6. Category.findAll().then((p: any) => res.json(p))
  7. //Category.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. Category.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. Category.create(req.body).then((category: any) =>
  23. Category.findByIdWithIncludes(category.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. Category.findByPk(req.params.id).then((category: any) =>
  33. category
  34. .update(req.body)
  35. .then(() =>
  36. Category.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. //Category.findByPk(req.params.id).then((category:any)=>res.json(category.delete())
  49. } catch (err) {
  50. next(err)
  51. }
  52. })
  53. module.exports = router