all files / collab/ DocumentServer.js

0% Statements 0/21
0% Branches 0/6
0% Functions 0/8
0% Lines 0/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                                                                                                                       
/*
  DocumentServer module. Can be bound to an express instance
*/
class DocumentServer {
 
  constructor(params) {
    this.configurator = params.configurator
    this.engine = this.configurator.getDocumentEngine()
    // TODO: make path configurable through configurator
    this.path = '/api/documents'
  }
 
  /*
    Attach this server to an express instance
  */
  bind(app) {
    app.post(this.path, this._createDocument.bind(this))
    app.get(this.path + '/:id', this._getDocument.bind(this))
    app.delete(this.path + '/:id', this._deleteDocument.bind(this))
  }
 
  /*
    Create a new document, given a documentId and initial change
  */
  _createDocument(req, res, next) {
    const { documentId, change } = req.body
    this.engine.createDocument(documentId, change, function(err, version) {
      if (err) return next(err)
      res.json(version)
    })
  }
 
  /*
    Get a document with given document id
  */
  _getDocument(req, res, next) {
    const documentId = req.params.id
    this.engine.getDocument(documentId, function(err, jsonDoc, version) {
      if (err) return next(err)
      res.json({
        data: jsonDoc,
        version: version
      })
    })
  }
 
  /*
    Remove a document with given document id
  */
  _deleteDocument(req, res, next) {
    let documentId = req.params.id
    this.engine.deleteDocument(documentId, function(err, result) {
      if (err) return next(err)
      res.json(result)
    })
  }
}
 
export default DocumentServer