123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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']]
- router.get('/', async (req: any, res: any, next: any) => {
- try {
- //Event.findAllWithIncludes().then((a: any) => res.json(a))
- Event.findAll({ 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({ 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({ limit, offset, order }).then((a: any) => res.json(a))
- } catch (err) {
- next(err)
- }
- })
- router.get('/sections', async (req: any, res: any, next: any) => {
- try {
- let sections: String[] = []
- Event.findAll().then((events: any) => {
- events.forEach(
- ({ section }: any) =>
- sections.includes(section) || sections.push(section)
- )
- res.json(sections)
- })
- } catch (err) {
- next(err)
- }
- })
- router.get('/methods', async (req: any, res: any, next: any) => {
- try {
- let methods: String[] = []
- Event.findAll().then((events: any) => {
- events.forEach(
- ({ method }: any) => methods.includes(method) || methods.push(method)
- )
- res.json(methods)
- })
- } catch (err) {
- next(err)
- }
- })
- router.get('/:method/:key', async (req: any, res: any, next: any) => {
- try {
- const method = req.params.method.toLowerCase()
- const key = req.params.key.toLowerCase()
- Event.findWithIncludes({
- where: {
- [Op.or]: [
- { method: { [Op.iLike]: method } },
- { section: { [Op.iLike]: method } },
- ],
- data: { [Op.iLike]: `%${key}%` },
- },
- }).then((events: any) => res.json(events))
- } catch (err) {
- next(err)
- }
- })
- 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
|