course.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { ref, computed } from 'vue';
  2. import { defineStore } from 'pinia';
  3. import { AxiosWrapper } from '@/util/axios-wrapper';
  4. import _ from 'lodash';
  5. import type { IQueryType, IQueryResult, IQueryParams } from '@/util/types.util';
  6. const axios = new AxiosWrapper();
  7. const api = {
  8. url: `/ball/v1/api/course`
  9. };
  10. export const CourseStore = defineStore('course', () => {
  11. const count = ref(0);
  12. const doubleCount = computed(() => count.value * 2);
  13. function increment() {
  14. count.value++;
  15. }
  16. const query = async ({ skip = 0, limit = undefined, ...info }: IQueryParams = {}): Promise<IQueryResult> => {
  17. let cond: IQueryType = {};
  18. if (skip) cond.skip = skip;
  19. if (limit) cond.limit = limit;
  20. cond = { ...cond, ...info };
  21. const res = await axios.$get(`${api.url}`, cond);
  22. return res;
  23. };
  24. const ranking = async ({ ...info }: IQueryParams = {}): Promise<IQueryResult> => {
  25. const res = await axios.$get(`${api.url}/ranking`, info);
  26. return res;
  27. };
  28. const fetch = async (payload: any): Promise<IQueryResult> => {
  29. const res = await axios.$get(`${api.url}/${payload}`);
  30. return res;
  31. };
  32. const create = async (payload: any): Promise<IQueryResult> => {
  33. const res = await axios.$post(`${api.url}`, payload);
  34. return res;
  35. };
  36. const update = async (payload: any): Promise<IQueryResult> => {
  37. const id = _.get(payload, 'id', _.get(payload, '_id'));
  38. const res = await axios.$post(`${api.url}/${id}`, payload);
  39. return res;
  40. };
  41. const del = async (payload: any): Promise<IQueryResult> => {
  42. const res = await axios.$delete(`${api.url}/${payload}`);
  43. return res;
  44. };
  45. return {
  46. count,
  47. doubleCount,
  48. increment,
  49. query,
  50. ranking,
  51. fetch,
  52. create,
  53. update,
  54. del
  55. };
  56. });