MockChatApi.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const AbstractMockApi = require('./AbstractMockApi')
  2. class MockChatApi extends AbstractMockApi {
  3. reset() {
  4. this.projects = {}
  5. }
  6. getGlobalMessages(req, res) {
  7. res.json(this.projects[req.params.project_id] || [])
  8. }
  9. sendGlobalMessage(req, res) {
  10. const projectId = req.params.project_id
  11. const message = {
  12. id: Math.random().toString(),
  13. content: req.body.content,
  14. timestamp: Date.now(),
  15. user_id: req.body.user_id,
  16. }
  17. this.projects[projectId] = this.projects[projectId] || []
  18. this.projects[projectId].push(message)
  19. res.json(Object.assign({ room_id: projectId }, message))
  20. }
  21. destroyProject(req, res) {
  22. const projectId = req.params.project_id
  23. delete this.projects[projectId]
  24. res.sendStatus(204)
  25. }
  26. applyRoutes() {
  27. this.app.get('/project/:project_id/messages', (req, res) =>
  28. this.getGlobalMessages(req, res)
  29. )
  30. this.app.post('/project/:project_id/messages', (req, res) =>
  31. this.sendGlobalMessage(req, res)
  32. )
  33. this.app.delete('/project/:project_id', (req, res) =>
  34. this.destroyProject(req, res)
  35. )
  36. }
  37. }
  38. module.exports = MockChatApi
  39. // type hint for the inherited `instance` method
  40. /**
  41. * @function instance
  42. * @memberOf MockChatApi
  43. * @static
  44. * @returns {MockChatApi}
  45. */