events.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const router = require('express').Router()
  2. import { Event } from '../db/models'
  3. import { Op } from 'sequelize'
  4. router.get('/', async (req: any, res: any, next: any) => {
  5. try {
  6. Event.findAllWithIncludes().then((a: any) => res.json(a))
  7. } catch (err) {
  8. next(err)
  9. }
  10. })
  11. router.get('/sections', async (req: any, res: any, next: any) => {
  12. try {
  13. let sections: String[] = []
  14. Event.findAll().then((events: any) => {
  15. events.forEach(
  16. ({ section }: any) =>
  17. sections.includes(section) || sections.push(section)
  18. )
  19. res.json(sections)
  20. })
  21. } catch (err) {
  22. next(err)
  23. }
  24. })
  25. router.get('/methods', async (req: any, res: any, next: any) => {
  26. try {
  27. let methods: String[] = []
  28. Event.findAll().then((events: any) => {
  29. events.forEach(
  30. ({ method }: any) => methods.includes(method) || methods.push(method)
  31. )
  32. res.json(methods)
  33. })
  34. } catch (err) {
  35. next(err)
  36. }
  37. })
  38. router.get('/:method/:key', async (req: any, res: any, next: any) => {
  39. try {
  40. const method = req.params.method.toLowerCase()
  41. const key = req.params.key.toLowerCase()
  42. Event.findWithIncludes({
  43. where: {
  44. [Op.or]: [
  45. { method: { [Op.iLike]: method } },
  46. { section: { [Op.iLike]: method } },
  47. ],
  48. data: { [Op.iLike]: `%${key}%` },
  49. },
  50. }).then((events: any) => res.json(events))
  51. } catch (err) {
  52. next(err)
  53. }
  54. })
  55. router.get('/:id', async (req: any, res: any, next: any) => {
  56. try {
  57. if (req.params.id > 0) {
  58. const event = await Event.findByIdWithIncludes(req.params.id)
  59. return res.json(event)
  60. }
  61. const needle = req.params.id.toLowerCase()
  62. const cond = { [Op.iLike]: `%${needle}%` }
  63. Event.findWithIncludes({
  64. where: { [Op.or]: [{ method: cond }, { section: cond }, { data: cond }] },
  65. }).then((events: any) => res.json(events))
  66. } catch (err) {
  67. next(err)
  68. }
  69. })
  70. router.post('/', async (req: any, res: any, next: any) => {
  71. try {
  72. Event.create(req.body).then((account: any) =>
  73. Event.findByIdWithIncludes(account.id).then((a: any) => res.json(a))
  74. )
  75. } catch (err) {
  76. next(err)
  77. }
  78. })
  79. router.put('/:id', async (req: any, res: any, next: any) => {
  80. try {
  81. Event.findByPk(req.params.id).then((account: any) =>
  82. account
  83. .update(req.body)
  84. .then(() =>
  85. Event.findByIdWithIncludes(req.params.id).then((a: any) =>
  86. res.json(a)
  87. )
  88. )
  89. )
  90. } catch (err) {
  91. next(err)
  92. }
  93. })
  94. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  95. try {
  96. //Event.findByPk(req.params.id).then((account:any)=>res.json(account.delete())
  97. } catch (err) {
  98. next(err)
  99. }
  100. })
  101. module.exports = router