events.ts 3.4 KB

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