blocks.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.findAllWithIncludes().then((a: any) => res.json(a))
  6. } catch (err) {
  7. next(err)
  8. }
  9. })
  10. router.get('/:id', async (req: any, res: any, next: any) => {
  11. try {
  12. Block.findByIdWithIncludes(req.params.id).then((a: any) => res.json(a))
  13. } catch (err) {
  14. next(err)
  15. }
  16. })
  17. router.post('/', async (req: any, res: any, next: any) => {
  18. try {
  19. Block.create(req.body).then((account: any) =>
  20. Block.findByIdWithIncludes(account.id).then((a: any) => res.json(a))
  21. )
  22. } catch (err) {
  23. next(err)
  24. }
  25. })
  26. router.put('/:id', async (req: any, res: any, next: any) => {
  27. try {
  28. Block.findByPk(req.params.id).then((account: any) =>
  29. account
  30. .update(req.body)
  31. .then(() =>
  32. Block.findByIdWithIncludes(req.params.id).then((a: any) =>
  33. res.json(a)
  34. )
  35. )
  36. )
  37. } catch (err) {
  38. next(err)
  39. }
  40. })
  41. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  42. try {
  43. //Block.findByPk(req.params.id).then((account:any)=>res.json(account.delete())
  44. } catch (err) {
  45. next(err)
  46. }
  47. })
  48. module.exports = router