categories.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const router = require('express').Router()
  2. import { Category } from '../db/models'
  3. router.get('/', async (req: any, res: any, next: any) => {
  4. try {
  5. Category.findAll().then((p: any) => res.json(p))
  6. //Category.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. Category.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. Category.create(req.body).then((category: any) =>
  21. Category.findByIdWithIncludes(category.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. Category.findByPk(req.params.id).then((category: any) =>
  30. category
  31. .update(req.body)
  32. .then(() =>
  33. Category.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. //Category.findByPk(req.params.id).then((category:any)=>res.json(category.delete())
  45. } catch (err) {
  46. next(err)
  47. }
  48. })
  49. module.exports = router