open_lark/service/cloud_docs/bitable/v1/app_table_view/
create.rs1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5 core::{
6 api_req::ApiRequest,
7 api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
8 constants::AccessTokenType,
9 http::Transport,
10 req_option::RequestOption,
11 SDKResult,
12 },
13 impl_executable_builder_owned,
14};
15
16use super::AppTableViewService;
17
18impl AppTableViewService {
19 pub async fn create(
21 &self,
22 request: CreateViewRequest,
23 option: Option<RequestOption>,
24 ) -> SDKResult<BaseResponse<CreateViewResponse>> {
25 let mut api_req = request.api_request;
26 api_req.http_method = Method::POST;
27 api_req.api_path = format!(
28 "/open-apis/bitable/v1/apps/{}/tables/{}/views",
29 request.app_token, request.table_id
30 );
31 api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
32 api_req.body = serde_json::to_vec(&CreateViewRequestBody { view: request.view })?;
33
34 let api_resp = Transport::request(api_req, &self.config, option).await?;
35 Ok(api_resp)
36 }
37}
38
39#[derive(Debug, Default)]
41pub struct CreateViewRequest {
42 api_request: ApiRequest,
43 app_token: String,
45 table_id: String,
47 view: ViewData,
49}
50
51impl CreateViewRequest {
52 pub fn builder() -> CreateViewRequestBuilder {
53 CreateViewRequestBuilder::default()
54 }
55
56 pub fn new(app_token: impl ToString, table_id: impl ToString, view: ViewData) -> Self {
58 Self {
59 api_request: ApiRequest::default(),
60 app_token: app_token.to_string(),
61 table_id: table_id.to_string(),
62 view,
63 }
64 }
65}
66
67#[derive(Default)]
68pub struct CreateViewRequestBuilder {
69 request: CreateViewRequest,
70}
71
72impl CreateViewRequestBuilder {
73 pub fn app_token(mut self, app_token: impl ToString) -> Self {
75 self.request.app_token = app_token.to_string();
76 self
77 }
78
79 pub fn table_id(mut self, table_id: impl ToString) -> Self {
81 self.request.table_id = table_id.to_string();
82 self
83 }
84
85 pub fn view(mut self, view: ViewData) -> Self {
87 self.request.view = view;
88 self
89 }
90
91 pub fn build(self) -> CreateViewRequest {
92 self.request
93 }
94}
95
96impl_executable_builder_owned!(
97 CreateViewRequestBuilder,
98 super::AppTableViewService,
99 CreateViewRequest,
100 BaseResponse<CreateViewResponse>,
101 create
102);
103
104#[derive(Debug, Clone, Default, Serialize, Deserialize)]
106pub struct ViewData {
107 pub view_name: String,
109 #[serde(skip_serializing_if = "Option::is_none")]
111 pub view_type: Option<String>,
112 #[serde(skip_serializing_if = "Option::is_none")]
114 pub property: Option<serde_json::Value>,
115}
116
117impl ViewData {
118 pub fn new(view_name: impl ToString) -> Self {
120 Self {
121 view_name: view_name.to_string(),
122 view_type: None,
123 property: None,
124 }
125 }
126
127 pub fn grid_view(view_name: impl ToString) -> Self {
129 Self {
130 view_name: view_name.to_string(),
131 view_type: Some("grid".to_string()),
132 property: None,
133 }
134 }
135
136 pub fn kanban_view(view_name: impl ToString) -> Self {
138 Self {
139 view_name: view_name.to_string(),
140 view_type: Some("kanban".to_string()),
141 property: None,
142 }
143 }
144
145 pub fn gallery_view(view_name: impl ToString) -> Self {
147 Self {
148 view_name: view_name.to_string(),
149 view_type: Some("gallery".to_string()),
150 property: None,
151 }
152 }
153
154 pub fn gantt_view(view_name: impl ToString) -> Self {
156 Self {
157 view_name: view_name.to_string(),
158 view_type: Some("gantt".to_string()),
159 property: None,
160 }
161 }
162
163 pub fn with_view_type(mut self, view_type: impl ToString) -> Self {
165 self.view_type = Some(view_type.to_string());
166 self
167 }
168
169 pub fn with_property(mut self, property: serde_json::Value) -> Self {
171 self.property = Some(property);
172 self
173 }
174}
175
176#[derive(Serialize)]
177struct CreateViewRequestBody {
178 view: ViewData,
179}
180
181#[derive(Deserialize, Debug)]
182pub struct CreateViewResponse {
183 pub view_id: String,
185}
186
187impl ApiResponseTrait for CreateViewResponse {
188 fn data_format() -> ResponseFormat {
189 ResponseFormat::Data
190 }
191}
192
193#[cfg(test)]
194mod tests {
195 use super::*;
196 use serde_json::json;
197
198 #[test]
199 fn test_create_view_request() {
200 let view = ViewData::grid_view("测试表格视图");
201 let request = CreateViewRequest::builder()
202 .app_token("bascnmBA*****yGehy8")
203 .table_id("tblsRc9GRRXKqhvW")
204 .view(view)
205 .build();
206
207 assert_eq!(request.app_token, "bascnmBA*****yGehy8");
208 assert_eq!(request.table_id, "tblsRc9GRRXKqhvW");
209 assert_eq!(request.view.view_name, "测试表格视图");
210 assert_eq!(request.view.view_type, Some("grid".to_string()));
211 }
212
213 #[test]
214 fn test_view_data_types() {
215 let grid_view = ViewData::grid_view("表格视图");
216 assert_eq!(grid_view.view_type, Some("grid".to_string()));
217
218 let kanban_view = ViewData::kanban_view("看板视图");
219 assert_eq!(kanban_view.view_type, Some("kanban".to_string()));
220
221 let gallery_view = ViewData::gallery_view("画册视图");
222 assert_eq!(gallery_view.view_type, Some("gallery".to_string()));
223
224 let gantt_view = ViewData::gantt_view("甘特视图");
225 assert_eq!(gantt_view.view_type, Some("gantt".to_string()));
226 }
227
228 #[test]
229 fn test_view_data_with_property() {
230 let view = ViewData::new("自定义视图")
231 .with_view_type("grid")
232 .with_property(json!({
233 "filter_info": {
234 "conditions": []
235 }
236 }));
237
238 assert_eq!(view.view_name, "自定义视图");
239 assert_eq!(view.view_type, Some("grid".to_string()));
240 assert!(view.property.is_some());
241 }
242}