channels.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const router = require('express').Router()
  2. import { Channel } from '../db/models'
  3. import donate from '../donate'
  4. router.get('/', async (req: any, res: any, next: any) => {
  5. try {
  6. //Channel.findAllWithIncludes().then((p: any) => res.json(p))
  7. Channel.findAll().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. //Channel.findByIdWithIncludes(req.params.id).then((p: any) => res.json(p))
  15. Channel.findByPk(req.params.id).then((p: any) => res.json(p))
  16. } catch (err) {
  17. next(err)
  18. }
  19. })
  20. router.post('/', async (req: any, res: any, next: any) => {
  21. res.status(402).send(donate)
  22. try {
  23. Channel.create(req.body).then((channel: any) => res.json(channel))
  24. //Channel.findByIdWithIncludes(channel.id).then((p: any) => res.json(p))
  25. //)
  26. } catch (err) {
  27. next(err)
  28. }
  29. })
  30. router.put('/:id', async (req: any, res: any, next: any) => {
  31. res.status(402).send(donate)
  32. try {
  33. Channel.findByPk(req.params.id).then((channel: any) =>
  34. channel
  35. .update(req.body)
  36. .then(() =>
  37. Channel.findByIdWithIncludes(req.params.id).then((p: any) =>
  38. res.json(p)
  39. )
  40. )
  41. )
  42. } catch (err) {
  43. next(err)
  44. }
  45. })
  46. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  47. res.status(402).send(donate)
  48. try {
  49. //Channel.findByPk(req.params.id).then((channel:any)=>res.json(channel.delete())
  50. } catch (err) {
  51. next(err)
  52. }
  53. })
  54. module.exports = router