Unit.js 1.8 KB

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