accounts.ts 1.3 KB

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