events.ts 3.3 KB

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