3
1

channels.ts 1.4 KB

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