collection.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { getQuestionInfoByIds, CollectionAndWrong } from "@/api";
  2. /**收藏与错题列表模型 */
  3. export class CollectionModel {
  4. private collectionApi;
  5. constructor(apiType: CollectionAndWrongType.type) {
  6. this.collectionApi = new CollectionAndWrong(apiType);
  7. }
  8. /**获取列表 */
  9. async getList(params: CollectionAndWrongType.ListParams): Promise<{
  10. total: number;
  11. rows: Test.QuestionInfo[];
  12. collectionList: CollectionAndWrongType.QuestionRes[];
  13. }> {
  14. const collectionListRes = await this.collectionApi.getList(params);
  15. if (collectionListRes.rows.length > 0) {
  16. const ids = collectionListRes.rows.map((item) => item.questionId);
  17. const questions = await getQuestionInfoByIds({
  18. ids,
  19. });
  20. return {
  21. total: collectionListRes.total,
  22. rows: questions.rows,
  23. collectionList: collectionListRes.rows,
  24. };
  25. } else {
  26. return {
  27. total: collectionListRes.total,
  28. rows: [],
  29. collectionList: collectionListRes.rows,
  30. };
  31. }
  32. }
  33. /** 删除已收藏的问题 */
  34. async deletes(ids: number[], collectionList: CollectionAndWrongType.QuestionRes[]) {
  35. const deletesCollectionList = collectionList.filter((item) => {
  36. return ids.includes(item.questionId);
  37. });
  38. const deletesIds = deletesCollectionList.map((item) => item.id);
  39. const res = await this.collectionApi.deletes(deletesIds);
  40. return res;
  41. }
  42. /** 新增收藏或者错题 */
  43. async adds(params: CollectionAndWrongType.AddCullectionsParams) {
  44. const res = await this.collectionApi.adds(params);
  45. return res.data;
  46. }
  47. }