blocks.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const router = require('express').Router()
  2. import { Block } from '../db/models'
  3. import donate from '../donate'
  4. const limit = 50000
  5. const order = [['id', 'DESC']]
  6. router.get('/', async (req: any, res: any, next: any) => {
  7. try {
  8. Block.findAll({ limit, order }).then((a: any) => res.json(a))
  9. //Block.findAllWithIncludes().then((a: any) => res.json(a))
  10. } catch (err) {
  11. next(err)
  12. }
  13. })
  14. router.get('/offset/:offset', async (req: any, res: any, next: any) => {
  15. try {
  16. const { offset } = req.params
  17. Block.findAll({ limit, offset: offset, order }).then((a: any) =>
  18. res.json(a)
  19. )
  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. Block.findAll({ limit, offset, order }).then((a: any) => res.json(a))
  29. } catch (err) {
  30. next(err)
  31. }
  32. })
  33. router.get('/:id', async (req: any, res: any, next: any) => {
  34. try {
  35. Block.findByIdWithIncludes(req.params.id).then((a: any) => res.json(a))
  36. } catch (err) {
  37. next(err)
  38. }
  39. })
  40. router.post('/', async (req: any, res: any, next: any) => {
  41. res.status(402).send(donate)
  42. try {
  43. Block.create(req.body).then((account: any) =>
  44. Block.findByIdWithIncludes(account.id).then((a: any) => res.json(a))
  45. )
  46. } catch (err) {
  47. next(err)
  48. }
  49. })
  50. router.put('/:id', async (req: any, res: any, next: any) => {
  51. res.status(402).send(donate)
  52. try {
  53. Block.findByPk(req.params.id).then((account: any) =>
  54. account
  55. .update(req.body)
  56. .then(() =>
  57. Block.findByIdWithIncludes(req.params.id).then((a: any) =>
  58. res.json(a)
  59. )
  60. )
  61. )
  62. } catch (err) {
  63. next(err)
  64. }
  65. })
  66. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  67. res.status(402).send(donate)
  68. try {
  69. //Block.findByPk(req.params.id).then((account:any)=>res.json(account.delete())
  70. } catch (err) {
  71. next(err)
  72. }
  73. })
  74. module.exports = router