You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. const fs = require('fs')
  2. const express = require('express')
  3. const router = express.Router()
  4. const db = JSON.parse(fs.readFileSync('./api/db/docs.json', 'utf8'))
  5. // Get doc list.
  6. router.get('/', (req, res) => {
  7. const q = req.query.q
  8. if (q) {
  9. // res.json(db.filter(item => item.text.toLowerCase().includes(q.toLowerCase())))
  10. res.json(db)
  11. } else {
  12. res.json(db)
  13. }
  14. })
  15. // Create a doc.
  16. router.post('/', (req, res) => {
  17. const doc = {
  18. id: db.results.reduce((x, y) => { return x.id > y.id ? x : y }).id + 1,
  19. text: req.body.text
  20. }
  21. res.json(doc)
  22. })
  23. // Upload a file.
  24. router.post('/upload', (req, res) => {
  25. const doc = {
  26. id: db.results.reduce((x, y) => { return x.id > y.id ? x : y }).id + 1,
  27. text: 'Uploaded Document',
  28. meta: JSON.stringify({}),
  29. annotations: []
  30. }
  31. db.results.push(doc)
  32. res.json(doc)
  33. })
  34. // Download a file.
  35. router.get('/download', (req, res) => {
  36. res.json(db)
  37. })
  38. // Update a document partially.
  39. router.patch('/:docId', (req, res) => {
  40. const docIndex = db.results.findIndex(item => item.id === parseInt(req.params.docId, 10))
  41. if (docIndex !== -1) {
  42. Object.assign(db.results[docIndex], req.body)
  43. res.json(db.results[docIndex])
  44. } else {
  45. res.status(404).json({ detail: 'Not found.' })
  46. }
  47. })
  48. // Get a doc.
  49. router.get('/:docId', (req, res) => {
  50. const doc = db.results.find(item => item.id === parseInt(req.params.docId, 10))
  51. if (doc) {
  52. res.json(doc)
  53. } else {
  54. res.status(404).json({ detail: 'Not found.' })
  55. }
  56. })
  57. // Update a doc.
  58. router.put('/:docId', (req, res) => {
  59. const docIndex = db.results.findIndex(item => item.id === parseInt(req.params.docId, 10))
  60. if (docIndex !== -1) {
  61. db.results[docIndex] = req.body
  62. res.json(db.results[docIndex])
  63. } else {
  64. res.status(404).json({ detail: 'Not found.' })
  65. }
  66. })
  67. // Delete a doc.
  68. router.delete('/:docId', (req, res, next) => {
  69. const doc = db.results.find(item => item.id === parseInt(req.params.docId, 10))
  70. if (doc) {
  71. db.results = db.results.filter(item => item.id !== parseInt(req.params.docId, 10))
  72. res.json(doc)
  73. } else {
  74. res.status(404).json({ detail: 'Not found.' })
  75. }
  76. })
  77. // Add an annotation.
  78. router.post('/:docId/annotations', (req, res, next) => {
  79. const doc = db.results.find(item => item.id === parseInt(req.params.docId, 10))
  80. if (doc) {
  81. const annotation = {
  82. id: Math.floor(Math.random() * 10000),
  83. label: req.body.label,
  84. start_offset: req.body.start_offset,
  85. end_offset: req.body.end_offset,
  86. user: 1,
  87. document: parseInt(req.params.docId, 10),
  88. text: req.body.text
  89. }
  90. doc.annotations.push(annotation)
  91. res.json(annotation)
  92. } else {
  93. res.status(404).json({ detail: 'Not found.' })
  94. }
  95. })
  96. // Delete an annotation.
  97. router.delete('/:docId/annotations/:annotationId', (req, res, next) => {
  98. const doc = db.results.find(item => item.id === parseInt(req.params.docId, 10))
  99. const docIndex = db.results.findIndex(item => item.id === parseInt(req.params.docId, 10))
  100. if (doc) {
  101. const annotation = doc.annotations.find(item => item.id === parseInt(req.params.annotationId, 10))
  102. doc.annotations = doc.annotations.filter(item => item.id !== parseInt(req.params.annotationId, 10))
  103. db.results[docIndex] = doc
  104. res.json(annotation)
  105. } else {
  106. res.status(404).json({ detail: 'Not found.' })
  107. }
  108. })
  109. // Update an annotation.
  110. router.patch('/:docId/annotations/:annotationId', (req, res, next) => {
  111. const docIndex = db.results.findIndex(item => item.id === parseInt(req.params.docId, 10))
  112. if (docIndex !== -1) {
  113. const doc = db.results[docIndex]
  114. const annotationIndex = doc.annotations.findIndex(item => item.id === parseInt(req.params.annotationId, 10))
  115. Object.assign(db.results[docIndex].annotations[annotationIndex], req.body)
  116. res.json(db.results[docIndex].annotations[annotationIndex])
  117. } else {
  118. res.status(404).json({ detail: 'Not found.' })
  119. }
  120. })
  121. module.exports = router