Question.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { appApi } from 'src/boot/axios.js'
  2. import APIRepository from '../classes/APIRepository.js'
  3. import { Question, QuestionList } from 'src/models/Question.js'
  4. export default class QuestionAPI extends APIRepository {
  5. constructor() {
  6. super('questions', appApi)
  7. this.APIAdresses = {
  8. base: '/lma/questions',
  9. byId: (id) => '/lma/questions/' + id
  10. }
  11. this.CacheList = {
  12. base: this.name + this.APIAdresses.base
  13. }
  14. this.restUrl = (id) => this.APIAdresses.base + '/' + id
  15. /* Setting the callback functions for the CRUD operations. */
  16. this.setCrudCallbacks({
  17. get: (response) => { return new Question(response.data) },
  18. post: (response) => { return new Question(response.data) },
  19. put: (response) => { return new Question(response.data) },
  20. delete: (response) => { return response.data }
  21. })
  22. }
  23. index(data) {
  24. return this.sendRequest({
  25. apiMethod: 'get',
  26. api: this.api,
  27. request: this.APIAdresses.base,
  28. data: this.getNormalizedSendData({
  29. category: null, // Number
  30. per_page: 10, // Number
  31. page: 1 // Number
  32. }, data),
  33. resolveCallback: (response) => {
  34. const paginate = response.data
  35. const results = response.data.results
  36. delete paginate.results
  37. return {
  38. list: new QuestionList(results),
  39. paginate
  40. // {
  41. // "count": 1,
  42. // "num_pages": 1,
  43. // "per_page": 10,
  44. // "current": 1,
  45. // "next": null,
  46. // "previous": null,
  47. // }
  48. }
  49. },
  50. rejectCallback: (error) => {
  51. return error
  52. }
  53. })
  54. }
  55. get(id) {
  56. return this.sendRequest({
  57. apiMethod: 'get',
  58. api: this.api,
  59. request: this.APIAdresses.byId(id),
  60. resolveCallback: (response) => {
  61. return new Question(response.data)
  62. },
  63. rejectCallback: (error) => {
  64. return error
  65. }
  66. })
  67. }
  68. }