model.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import projects from '../_sampleData.js'
  2. class ProjectMock {
  3. constructor (data) {
  4. for (let prop in data) {
  5. this[prop] = data[prop]
  6. }
  7. this.modelName = 'project'
  8. // use this switch to simulate save errors.
  9. this._saveError = false
  10. // use this switch to simulate remove errors.
  11. this._removeError = false
  12. }
  13. static get schema () { return { obj: { __history: {} } } }
  14. static findOne (criteria, callback) {
  15. const { _id, tag } = criteria
  16. let foundProject
  17. if (typeof _id !== 'undefined') {
  18. foundProject = projects.find(project => {
  19. return (_id.equals(project._id))
  20. })
  21. } else if (typeof tag !== 'undefined') {
  22. foundProject = projects.find(project => {
  23. return (tag === project.tag && project.history.head === true)
  24. })
  25. }
  26. if (foundProject) {
  27. // return the project
  28. callback(undefined, foundProject)
  29. } else {
  30. // else return 404
  31. callback(Error('item not found'), undefined)
  32. }
  33. }
  34. static find (criteria, options, callback) {
  35. let { limit, skip } = options
  36. const onlyHead = criteria['history.head']
  37. const projectsFiltered = projects.filter(project => {
  38. return project.history.head
  39. })
  40. // apply criteria
  41. const projectsSlice = projectsFiltered.slice(skip, skip + limit)
  42. if (projectsSlice) {
  43. // return the array
  44. callback(undefined, projectsSlice)
  45. } else {
  46. // if nothing can be shown, return 404
  47. callback(Error('no items found'), undefined)
  48. }
  49. }
  50. save () {
  51. throw Error('save not implemented.')
  52. }
  53. remove () {
  54. throw Error('remove not implemented.')
  55. }
  56. }
  57. export default ProjectMock