use crate::api::{SingleResponse, api_delete, api_post, api_put};
use crate::{
api::{ApiClient, ApiError},
models::{
AddTaskToSectionData, AddTaskToSectionRequest, Section, SectionCreateRequest,
SectionUpdateRequest, Task,
},
};
use futures_util::{StreamExt, pin_mut};
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: SingleResponse<Section> = 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> {
api_post(
client,
&format!("/projects/{project_gid}/sections"),
&request,
)
.await
}
pub async fn update_section(
client: &ApiClient,
section_gid: &str,
request: SectionUpdateRequest,
) -> Result<Section, ApiError> {
api_put(client, &format!("/sections/{section_gid}"), &request).await
}
pub async fn delete_section(client: &ApiClient, section_gid: &str) -> Result<(), ApiError> {
api_delete(client, &format!("/sections/{section_gid}")).await
}
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
}