blob: 816ac71fc76c5b5c133cea7cde64e091319bef06 [file] [log] [blame]
223010143d966302025-06-07 22:54:40 +08001import axios from 'axios';
2import type { CategoryDTO, Category, WorkResponse } from './otherType';
3
4const API_BASE_URL = '/api';
5
6class CategoryAPI {
7 /**
8 * 获取全部分区(树形结构)
9 * @returns 返回分区的树形结构数据
10 */
11 static getCategoryTree(): Promise<CategoryDTO[]> {
12 return axios.get<CategoryDTO[]>(`${API_BASE_URL}/categories`)
13 .then(response => response.data);
14 }
15
16 /**
17 * 获取单个分区详情
18 * @param id 分区ID
19 * @returns 返回单个分区的详细信息
20 */
21 static getCategory(id: number): Promise<Category> {
22 return axios.get<Category>(`${API_BASE_URL}/categories/${id}`)
23 .then(response => response.data);
24 }
25
26 /**
27 * 创建新分区
28 * @param category 分区数据(不包含ID)
29 * @returns 返回创建成功的消息
30 */
31 static createCategory(category: Omit<Category, 'id'>): Promise<string> {
32 return axios.post<string>(`${API_BASE_URL}/categories`, category)
33 .then(response => response.data);
34 }
35
36 /**
37 * 更新分区信息
38 * @param id 分区ID
39 * @param category 更新后的分区数据
40 * @returns 返回更新成功的消息
41 */
42 static updateCategory(id: number, category: Partial<Category>): Promise<string> {
43 return axios.put<string>(`${API_BASE_URL}/categories/${id}`, category)
44 .then(response => response.data);
45 }
46
47 /**
48 * 删除分区
49 * @param id 分区ID
50 * @returns 返回删除成功的消息
51 */
52 static deleteCategory(id: number): Promise<string> {
53 return axios.delete<string>(`${API_BASE_URL}/categories/${id}`)
54 .then(response => response.data);
55 }
56
57 /**
58 * 获取分区下的作品列表(带分页)
59 * @param categoryId 分区ID
60 * @param params 分页和排序参数
61 * @returns 返回作品列表和分页信息
62 */
63 static getWorksByCategory(
64 categoryId: number,
65 params?: {
66 page?: number;
67 size?: number;
68 sort?: string;
69 }
70 ): Promise<WorkResponse> {
71 return axios.get<WorkResponse>(`${API_BASE_URL}/works`, {
72 params: {
73 categoryId,
74 ...params
75 }
76 }).then(response => response.data);
77 }
78
79 /**
80 * 获取子分区列表
81 * @param parentId 父分区ID
82 * @returns 返回子分区列表
83 */
84 static getSubCategories(parentId: number): Promise<CategoryDTO[]> {
85 return axios.get<CategoryDTO[]>(`${API_BASE_URL}/categories/${parentId}/children`)
86 .then(response => response.data);
87 }
88}
89
90export default CategoryAPI;