Browse Source

backend: API routes (blocks, eras, events)

Joystream Stats 4 years ago
parent
commit
aacf22d466
3 changed files with 216 additions and 0 deletions
  1. 54 0
      server/api/blocks.ts
  2. 54 0
      server/api/eras.ts
  3. 108 0
      server/api/events.ts

+ 54 - 0
server/api/blocks.ts

@@ -0,0 +1,54 @@
+const router = require('express').Router()
+import { Block } from '../db/models'
+
+router.get('/', async (req: any, res: any, next: any) => {
+  try {
+    Block.findAllWithIncludes().then((a: any) => res.json(a))
+  } catch (err) {
+    next(err)
+  }
+})
+
+router.get('/:id', async (req: any, res: any, next: any) => {
+  try {
+    Block.findByIdWithIncludes(req.params.id).then((a: any) => res.json(a))
+  } catch (err) {
+    next(err)
+  }
+})
+
+router.post('/', async (req: any, res: any, next: any) => {
+  try {
+    Block.create(req.body).then((account: any) =>
+      Block.findByIdWithIncludes(account.id).then((a: any) => res.json(a))
+    )
+  } catch (err) {
+    next(err)
+  }
+})
+
+router.put('/:id', async (req: any, res: any, next: any) => {
+  try {
+    Block.findByPk(req.params.id).then((account: any) =>
+      account
+        .update(req.body)
+        .then(() =>
+          Block.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 {
+    //Block.findByPk(req.params.id).then((account:any)=>res.json(account.delete())
+  } catch (err) {
+    next(err)
+  }
+})
+
+module.exports = router

+ 54 - 0
server/api/eras.ts

@@ -0,0 +1,54 @@
+const router = require('express').Router()
+import { Era } from '../db/models'
+
+router.get('/', async (req: any, res: any, next: any) => {
+  try {
+    Era.findAllWithIncludes().then((a: any) => res.json(a))
+  } catch (err) {
+    next(err)
+  }
+})
+
+router.get('/:id', async (req: any, res: any, next: any) => {
+  try {
+    Era.findByIdWithIncludes(req.params.id).then((a: any) => res.json(a))
+  } catch (err) {
+    next(err)
+  }
+})
+
+router.post('/', async (req: any, res: any, next: any) => {
+  try {
+    Era.create(req.body).then((account: any) =>
+      Era.findByIdWithIncludes(account.id).then((a: any) => res.json(a))
+    )
+  } catch (err) {
+    next(err)
+  }
+})
+
+router.put('/:id', async (req: any, res: any, next: any) => {
+  try {
+    Era.findByPk(req.params.id).then((account: any) =>
+      account
+        .update(req.body)
+        .then(() =>
+          Era.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 {
+    //Era.findByPk(req.params.id).then((account:any)=>res.json(account.delete())
+  } catch (err) {
+    next(err)
+  }
+})
+
+module.exports = router

+ 108 - 0
server/api/events.ts

@@ -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