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.

79 lines
2.2 KiB

  1. const fs = require('fs')
  2. const bodyParser = require('body-parser')
  3. const express = require('express')
  4. const router = express.Router()
  5. let db = JSON.parse(fs.readFileSync('./api/db/projects.json', 'utf8'))
  6. router.use(bodyParser.json())
  7. router.use(bodyParser.urlencoded({ extended: true }))
  8. // Get project list.
  9. router.get('/', (req, res) => {
  10. const q = req.query.q
  11. if (q) {
  12. res.json(db.filter(item => item.name.toLowerCase().includes(q.toLowerCase())))
  13. } else {
  14. res.json(db)
  15. }
  16. })
  17. // Create a project.
  18. router.post('/', (req, res) => {
  19. const project = {
  20. id: db.reduce((x, y) => { return x.id > y.id ? x : y }).id + 1,
  21. name: req.body.name,
  22. description: req.body.description,
  23. guideline: 'Please write annotation guideline.',
  24. users: [1],
  25. project_type: req.body.project_type,
  26. image: '/static/assets/images/cats/text_classification.jpg',
  27. updated_at: '2019-08-21T02:49:48.790813Z',
  28. randomize_document_order: false
  29. }
  30. db.push(project)
  31. res.json(project)
  32. })
  33. // Get a project.
  34. router.get('/:projectId', (req, res) => {
  35. const project = db.find(item => item.id === parseInt(req.params.projectId))
  36. if (project) {
  37. res.json(project)
  38. } else {
  39. res.status(404).json({ detail: 'Not found.' })
  40. }
  41. })
  42. // Update a project.
  43. router.put('/:projectId', (req, res) => {
  44. const projectIndex = db.findIndex(item => item.id === parseInt(req.params.projectId))
  45. if (projectIndex !== -1) {
  46. db[projectIndex] = req.body
  47. res.json(db[projectIndex])
  48. } else {
  49. res.status(404).json({ detail: 'Not found.' })
  50. }
  51. })
  52. // Partially update a project user.
  53. router.patch('/:projectId', (req, res) => {
  54. const projectIndex = db.findIndex(item => item.id === parseInt(req.params.projectId))
  55. if (projectIndex !== -1) {
  56. Object.assign(db[projectIndex], req.body)
  57. res.json(db[projectIndex])
  58. } else {
  59. res.status(404).json({ detail: 'Not found.' })
  60. }
  61. })
  62. // Delete a project.
  63. router.delete('/:projectId', (req, res, next) => {
  64. const project = db.find(item => item.id === parseInt(req.params.projectId))
  65. if (project) {
  66. db = db.filter(item => item.id !== parseInt(req.params.projectId))
  67. res.json(project)
  68. } else {
  69. res.status(404).json({ detail: 'Not found.' })
  70. }
  71. })
  72. module.exports = router