2021-02-25 07:22:24 -05:00
|
|
|
const AbstractMockApi = require('./AbstractMockApi')
|
|
|
|
|
|
|
|
class MockDocstoreApi extends AbstractMockApi {
|
|
|
|
reset() {
|
|
|
|
this.docs = {}
|
|
|
|
}
|
|
|
|
|
2021-04-06 07:13:16 -04:00
|
|
|
createLegacyDeletedDoc(projectId, docId) {
|
|
|
|
if (!this.docs[projectId]) {
|
|
|
|
this.docs[projectId] = {}
|
|
|
|
}
|
|
|
|
this.docs[projectId][docId] = {
|
|
|
|
lines: [],
|
|
|
|
version: 1,
|
|
|
|
ranges: {},
|
2021-04-27 03:52:58 -04:00
|
|
|
deleted: true,
|
2021-04-06 07:13:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 05:15:00 -04:00
|
|
|
getDeletedDocs(projectId) {
|
|
|
|
return Object.entries(this.docs[projectId] || {})
|
|
|
|
.filter(([_, doc]) => doc.deleted)
|
|
|
|
.map(([docId, doc]) => {
|
|
|
|
return { _id: docId, name: doc.name }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
applyRoutes() {
|
|
|
|
this.app.post('/project/:projectId/doc/:docId', (req, res) => {
|
|
|
|
const { projectId, docId } = req.params
|
|
|
|
const { lines, version, ranges } = req.body
|
|
|
|
if (this.docs[projectId] == null) {
|
|
|
|
this.docs[projectId] = {}
|
|
|
|
}
|
|
|
|
if (this.docs[projectId][docId] == null) {
|
|
|
|
this.docs[projectId][docId] = {}
|
|
|
|
}
|
|
|
|
const { version: oldVersion, deleted } = this.docs[projectId][docId]
|
|
|
|
this.docs[projectId][docId] = { lines, version, ranges, deleted }
|
|
|
|
if (this.docs[projectId][docId].rev == null) {
|
|
|
|
this.docs[projectId][docId].rev = 0
|
|
|
|
}
|
|
|
|
this.docs[projectId][docId].rev += 1
|
|
|
|
this.docs[projectId][docId]._id = docId
|
|
|
|
res.json({
|
|
|
|
modified: oldVersion !== version,
|
2021-04-27 03:52:58 -04:00
|
|
|
rev: this.docs[projectId][docId].rev,
|
2021-02-25 07:22:24 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.get('/project/:projectId/doc', (req, res) => {
|
|
|
|
res.json(Object.values(this.docs[req.params.projectId] || {}))
|
|
|
|
})
|
|
|
|
|
2021-04-06 05:15:16 -04:00
|
|
|
this.app.get('/project/:projectId/doc-deleted', (req, res) => {
|
|
|
|
res.json(this.getDeletedDocs(req.params.projectId))
|
|
|
|
})
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.get('/project/:projectId/doc/:docId', (req, res) => {
|
|
|
|
const { projectId, docId } = req.params
|
|
|
|
const doc = this.docs[projectId][docId]
|
|
|
|
if (!doc || (doc.deleted && !req.query.include_deleted)) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else {
|
|
|
|
res.json(doc)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.get('/project/:projectId/doc/:docId/deleted', (req, res) => {
|
|
|
|
const { projectId, docId } = req.params
|
|
|
|
if (!this.docs[projectId] || !this.docs[projectId][docId]) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else {
|
|
|
|
res.json({ deleted: Boolean(this.docs[projectId][docId].deleted) })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-06 05:15:00 -04:00
|
|
|
this.app.patch('/project/:projectId/doc/:docId', (req, res) => {
|
2021-02-25 07:22:24 -05:00
|
|
|
const { projectId, docId } = req.params
|
|
|
|
if (!this.docs[projectId]) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else if (!this.docs[projectId][docId]) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else {
|
2021-04-06 05:15:00 -04:00
|
|
|
Object.assign(this.docs[projectId][docId], req.body)
|
2021-02-25 07:22:24 -05:00
|
|
|
res.sendStatus(204)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.post('/project/:projectId/destroy', (req, res) => {
|
|
|
|
const { projectId } = req.params
|
|
|
|
delete this.docs[projectId]
|
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MockDocstoreApi
|
|
|
|
|
|
|
|
// type hint for the inherited `instance` method
|
|
|
|
/**
|
|
|
|
* @function instance
|
|
|
|
* @memberOf MockDocstoreApi
|
|
|
|
* @static
|
|
|
|
* @returns {MockDocstoreApi}
|
|
|
|
*/
|