bangumi_api/module/indice/
service.rs

1use anyhow::Result;
2use reqwest::Method;
3
4use crate::{
5    common::model::BangumiClient,
6    module::{model::Paged, subject::model::SubjectType},
7};
8
9use super::model::{
10    Index, IndexBasicInfo, IndexSubject, IndexSubjectAddInfo, IndexSubjectEditInfo,
11};
12
13impl BangumiClient {
14    /// 创建新索引
15    ///
16    /// 用于在Bangumi平台创建一个新的索引(通常是主题性的条目集合)
17    ///
18    /// # 返回
19    /// 返回创建成功的索引详情
20    pub async fn add_index(&self) -> Result<Index> {
21        // 构建创建索引的API URL
22        let url = format!("{}/v0/indices", self.base_path);
23
24        // 创建POST请求构建器
25        let request_builder = self.request_builder(Method::POST, &url);
26
27        // 发送请求并解析响应为Index结构体
28        let res = self.request_send(request_builder).await?.json().await?;
29
30        Ok(res)
31    }
32
33    /// 获取索引详情
34    ///
35    /// 根据索引ID查询指定索引的完整信息,包括标题、描述、统计数据等
36    ///
37    /// # 参数
38    /// - `index_id`: 索引ID(必需,指定要查询的索引)
39    ///
40    /// # 返回
41    /// 返回包含索引详细信息的Index结构体
42    pub async fn get_index(&self, index_id: u32) -> Result<Index> {
43        // 构建索引详情API URL
44        let url = format!("{}/v0/indices/{index_id}", self.base_path);
45
46        // 创建GET请求构建器
47        let request_builder = self.request_builder(Method::GET, &url);
48
49        // 发送请求并解析响应为Index结构体
50        let res = self.request_send(request_builder).await?.json().await?;
51
52        Ok(res)
53    }
54
55    /// 编辑索引基本信息
56    ///
57    /// 更新指定索引的标题和描述等基本信息
58    ///
59    /// # 参数
60    /// - `index_id`: 索引ID(必需,指定要编辑的索引)
61    /// - `payload`: 可选,包含要更新的标题和描述信息
62    ///
63    /// # 返回
64    /// 返回更新后的索引详情
65    pub async fn edit_index(
66        &self,
67        index_id: u32,
68        payload: Option<IndexBasicInfo>,
69    ) -> Result<Index> {
70        // 构建索引编辑API URL
71        let url = format!("{}/v0/indices/{index_id}", self.base_path);
72
73        // 创建PUT请求构建器并添加请求体
74        let mut request_builder = self.request_builder(Method::PUT, &url);
75        request_builder = request_builder.json(&payload);
76
77        // 发送请求并解析响应为更新后的Index结构体
78        let res = self.request_send(request_builder).await?.json().await?;
79
80        Ok(res)
81    }
82
83    /// 获取索引中的条目列表
84    ///
85    /// 查询指定索引包含的所有条目,支持按条目类型筛选和分页
86    ///
87    /// # 参数
88    /// - `index_id`: 索引ID(必需,指定要查询的索引)
89    /// - `r#type`: 可选,按条目类型筛选(如动画、书籍等)
90    /// - `limit`: 可选,每页返回的结果数量
91    /// - `offset`: 可选,结果偏移量(用于分页)
92    ///
93    /// # 返回
94    /// 返回包含条目列表的分页结果
95    pub async fn get_index_subjects(
96        &self,
97        index_id: u32,
98        r#type: Option<SubjectType>,
99        limit: Option<u32>,
100        offset: Option<u32>,
101    ) -> Result<Paged<IndexSubject>> {
102        // 构建索引条目列表API URL
103        let url = format!("{}/v0/indices/{index_id}/subjects", self.base_path);
104
105        // 创建GET请求构建器
106        let mut request_builder = self.request_builder(Method::GET, &url);
107
108        // 添加条目类型筛选参数
109        if let Some(ref param_value) = r#type {
110            request_builder = request_builder.query(&[("type", &param_value)]);
111        }
112        // 添加分页参数
113        if let Some(ref param_value) = limit {
114            request_builder = request_builder.query(&[("limit", &param_value)]);
115        }
116        if let Some(ref param_value) = offset {
117            request_builder = request_builder.query(&[("offset", &param_value)]);
118        }
119
120        // 发送请求并解析响应为分页的Subject列表
121        let res = self.request_send(request_builder).await?.json().await?;
122
123        Ok(res)
124    }
125
126    /// 向索引添加条目
127    ///
128    /// 向指定索引中添加新条目,并可设置排序权重和备注
129    ///
130    /// # 参数
131    /// - `index_id`: 索引ID(必需,指定目标索引)
132    /// - `payload`: 可选,包含要添加的条目ID、排序权重和备注
133    ///
134    /// # 返回
135    /// 操作成功返回空结果,失败返回错误
136    pub async fn add_index_subject(
137        &self,
138        index_id: u32,
139        payload: Option<IndexSubjectAddInfo>,
140    ) -> Result<()> {
141        // 构建索引条目添加API URL
142        let url = format!("{}/v0/indices/{index_id}/subjects", self.base_path);
143
144        // 创建POST请求构建器并添加请求体
145        let mut request_builder = self.request_builder(Method::POST, &url);
146        request_builder = request_builder.json(&payload);
147
148        // 发送请求并忽略响应内容
149        let _res = self.request_send(request_builder).await?;
150
151        Ok(())
152    }
153
154    /// 编辑索引中的条目信息
155    ///
156    /// 修改指定索引中已有条目的排序权重和备注
157    ///
158    /// # 参数
159    /// - `index_id`: 索引ID(必需,指定目标索引)
160    /// - `subject_id`: 条目ID(必需,指定要编辑的条目)
161    /// - `payload`: 可选,包含要更新的排序权重和备注
162    ///
163    /// # 返回
164    /// 操作成功返回空结果,失败返回错误
165    pub async fn edit_index_subject(
166        &self,
167        index_id: u32,
168        subject_id: u32,
169        payload: Option<IndexSubjectEditInfo>,
170    ) -> Result<()> {
171        // 构建索引条目编辑API URL
172        let url = format!(
173            "{}/v0/indices/{index_id}/subjects/{subject_id}",
174            self.base_path
175        );
176
177        // 创建PUT请求构建器并添加请求体
178        let mut request_builder = self.request_builder(Method::PUT, &url);
179        request_builder = request_builder.json(&payload);
180
181        // 发送请求并忽略响应内容
182        let res = self.request_send(request_builder).await?.json().await?;
183
184        Ok(res)
185    }
186
187    /// 从索引中删除条目
188    ///
189    /// 将指定条目从索引中移除
190    ///
191    /// # 参数
192    /// - `index_id`: 索引ID(必需,指定目标索引)
193    /// - `subject_id`: 条目ID(必需,指定要删除的条目)
194    ///
195    /// # 返回
196    /// 操作成功返回空结果,失败返回错误
197    pub async fn delete_index_subject(&self, index_id: u32, subject_id: u32) -> Result<()> {
198        // 构建索引条目删除API URL
199        let url = format!(
200            "{}/v0/indices/{index_id}/subjects/{subject_id}",
201            self.base_path
202        );
203
204        // 创建DELETE请求构建器
205        let request_builder = self.request_builder(Method::DELETE, &url);
206
207        // 发送请求并忽略响应内容
208        let res = self.request_send(request_builder).await?.json().await?;
209
210        Ok(res)
211    }
212
213    /// 收藏索引
214    ///
215    /// 将指定索引添加到当前用户的收藏列表
216    ///
217    /// # 参数
218    /// - `index_id`: 索引ID(必需,指定要收藏的索引)
219    ///
220    /// # 返回
221    /// 操作成功返回空结果,失败返回错误
222    pub async fn collect_index(&self, index_id: u32) -> Result<()> {
223        // 构建索引收藏API URL
224        let url = format!("{}/v0/indices/{index_id}/collect", self.base_path);
225
226        // 创建POST请求构建器
227        let request_builder = self.request_builder(Method::POST, &url);
228
229        // 发送请求并忽略响应内容
230        let _res = self.request_send(request_builder).await?;
231
232        Ok(())
233    }
234
235    /// 取消收藏索引
236    ///
237    /// 将指定索引从当前用户的收藏列表中移除
238    ///
239    /// # 参数
240    /// - `index_id`: 索引ID(必需,指定要取消收藏的索引)
241    ///
242    /// # 返回
243    /// 操作成功返回空结果,失败返回错误
244    pub async fn uncollect_index(&self, index_id: u32) -> Result<()> {
245        // 构建取消索引收藏API URL
246        let url = format!("{}/v0/indices/{index_id}/collect", self.base_path);
247
248        // 创建DELETE请求构建器
249        let request_builder = self.request_builder(Method::DELETE, &url);
250
251        // 发送请求并忽略响应内容
252        let _res = self.request_send(request_builder).await?;
253
254        Ok(())
255    }
256}