blocks.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const router = require('express').Router()
  2. import { Block } from '../db/models'
  3. router.get('/', async (req: any, res: any, next: any) => {
  4. try {
  5. Block.findAll({ limit: 10000, order: [['id', 'DESC']] }).then((a: any) =>
  6. res.json(a)
  7. )
  8. //Block.findAllWithIncludes().then((a: any) => res.json(a))
  9. } catch (err) {
  10. next(err)
  11. }
  12. })
  13. router.get('/:id', async (req: any, res: any, next: any) => {
  14. try {
  15. Block.findByIdWithIncludes(req.params.id).then((a: any) => res.json(a))
  16. } catch (err) {
  17. next(err)
  18. }
  19. })
  20. router.post('/', async (req: any, res: any, next: any) => {
  21. try {
  22. Block.create(req.body).then((account: any) =>
  23. Block.findByIdWithIncludes(account.id).then((a: any) => res.json(a))
  24. )
  25. } catch (err) {
  26. next(err)
  27. }
  28. })
  29. router.put('/:id', async (req: any, res: any, next: any) => {
  30. try {
  31. Block.findByPk(req.params.id).then((account: any) =>
  32. account
  33. .update(req.body)
  34. .then(() =>
  35. Block.findByIdWithIncludes(req.params.id).then((a: any) =>
  36. res.json(a)
  37. )
  38. )
  39. )
  40. } catch (err) {
  41. next(err)
  42. }
  43. })
  44. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  45. try {
  46. //Block.findByPk(req.params.id).then((account:any)=>res.json(account.delete())
  47. } catch (err) {
  48. next(err)
  49. }
  50. })
  51. module.exports = router