const router = require("express").Router(); import { Event } from "../db/models"; import { Op } from "sequelize"; import donate from "../donate"; const limit = 10000; const order = [["id", "DESC"]]; const raw = true; router.get("/", async (req: any, res: any, next: any) => { try { //Event.findAllWithIncludes().then((a: any) => res.json(a)) Event.findAll({ raw, limit, order }).then((a: any) => res.json(a)); } catch (err) { next(err); } }); router.get("/offset/:offset", async (req: any, res: any, next: any) => { try { const { offset } = req.params; Event.findAll({ raw, limit, offset, order }).then((a: any) => res.json(a)); } catch (err) { next(err); } }); router.get("/page/:page", async (req: any, res: any, next: any) => { try { const { page } = req.params; const offset = limit * page; Event.findAll({ raw, limit, offset, order }).then((a: any) => res.json(a)); } catch (err) { next(err); } }); router.get("/sections", (req: any, res: any, next: any) => { Event.aggregate("section", "DISTINCT", { plain: false }).then( (methods: { DISTINCT: string }[]) => res.json(methods.map((e) => e.DISTINCT)) ); }); router.get("/methods/:method", (req: any, res: any, next: any) => { Event.findWithIncludes({ raw, where: { method: { [Op.iLike]: req.params.method } }, }).then((events: any) => res.json(events)); }); router.get("/methods", (req: any, res: any, next: any) => { Event.aggregate("method", "DISTINCT", { plain: false }).then( (methods: { DISTINCT: string }[]) => res.json(methods.map((e) => e.DISTINCT)) ); }); router.get("/:method/:key", (req: any, res: any, next: any) => { const method = req.params.method.toLowerCase(); const key = req.params.key.toLowerCase(); Event.findWithIncludes({ raw, where: { method: { [Op.iLike]: method } }, }).then((events: any) => res.json(events)); }); router.get("/:id", async (req: any, res: any, next: any) => { try { if (req.params.id > 0) { const event = await Event.findByIdWithIncludes(req.params.id); return res.json(event); } const needle = req.params.id.toLowerCase(); const cond = { [Op.iLike]: `%${needle}%` }; Event.findWithIncludes({ where: { [Op.or]: [{ method: cond }, { section: cond }, { data: cond }] }, }).then((events: any) => res.json(events)); } catch (err) { next(err); } }); router.post("/", async (req: any, res: any, next: any) => { res.status(402).send(donate); try { Event.create(req.body).then((account: any) => Event.findByIdWithIncludes(account.id).then((a: any) => res.json(a)) ); } catch (err) { next(err); } }); router.put("/:id", async (req: any, res: any, next: any) => { res.status(402).send(donate); try { Event.findByPk(req.params.id).then((account: any) => account .update(req.body) .then(() => Event.findByIdWithIncludes(req.params.id).then((a: any) => res.json(a) ) ) ); } catch (err) { next(err); } }); router.post("/:id/delete", async (req: any, res: any, next: any) => { res.status(402).send(donate); try { //Event.findByPk(req.params.id).then((account:any)=>res.json(account.delete()) } catch (err) { next(err); } }); module.exports = router;