use crate::{
api::{ApiClient, ApiError},
models::{AddTaskToSectionData, AddTaskToSectionRequest, Section, SectionCreateRequest, Task},
};
use futures_util::{StreamExt, pin_mut};
use serde::Deserialize;
pub async fn list_sections(
client: &ApiClient,
project_gid: &str,
) -> Result<Vec<Section>, ApiError> {
let stream = client.paginate_with_limit::<Section>(
&format!("/projects/{project_gid}/sections"),
Vec::new(),
None,
);
pin_mut!(stream);
let mut sections = Vec::new();
while let Some(page) = stream.next().await {
let mut page = page?;
sections.append(&mut page);
}
Ok(sections)
}
pub async fn get_section(
client: &ApiClient,
section_gid: &str,
fields: Vec<String>,
) -> Result<Section, ApiError> {
let mut query = Vec::new();
if !fields.is_empty() {
query.push(("opt_fields".into(), fields.join(",")));
}
let response: SingleSectionResponse = client
.get_json_with_pairs(&format!("/sections/{section_gid}"), query)
.await?;
Ok(response.data)
}
pub async fn create_section(
client: &ApiClient,
project_gid: &str,
request: SectionCreateRequest,
) -> Result<Section, ApiError> {
let response: SingleSectionResponse = client
.post_json(&format!("/projects/{project_gid}/sections"), &request)
.await?;
Ok(response.data)
}
pub async fn get_section_tasks(
client: &ApiClient,
section_gid: &str,
fields: Vec<String>,
) -> Result<Vec<Task>, ApiError> {
let mut query = Vec::new();
if !fields.is_empty() {
query.push(("opt_fields".into(), fields.join(",")));
}
let stream =
client.paginate_with_limit::<Task>(&format!("/sections/{section_gid}/tasks"), query, None);
pin_mut!(stream);
let mut tasks = Vec::new();
while let Some(page) = stream.next().await {
let mut page = page?;
tasks.append(&mut page);
}
Ok(tasks)
}
pub async fn add_task_to_section(
client: &ApiClient,
section_gid: &str,
task_gid: String,
insert_before: Option<String>,
insert_after: Option<String>,
) -> Result<(), ApiError> {
let request = AddTaskToSectionRequest {
data: AddTaskToSectionData {
task: task_gid,
insert_before,
insert_after,
},
};
client
.post_void(&format!("/sections/{section_gid}/addTask"), &request)
.await
}
#[derive(Debug, Deserialize)]
struct SingleSectionResponse {
data: Section,
}