events.ts 2.8 KB

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