|
@@ -0,0 +1,108 @@
|
|
|
+const router = require('express').Router()
|
|
|
+import { Event } from '../db/models'
|
|
|
+import { Op } from 'sequelize'
|
|
|
+
|
|
|
+router.get('/', async (req: any, res: any, next: any) => {
|
|
|
+ try {
|
|
|
+ Event.findAllWithIncludes().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) => {
|
|
|
+ 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) => {
|
|
|
+ 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) => {
|
|
|
+ try {
|
|
|
+ //Event.findByPk(req.params.id).then((account:any)=>res.json(account.delete())
|
|
|
+ } catch (err) {
|
|
|
+ next(err)
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+module.exports = router
|