101 lines
3.3 KiB
Rust
101 lines
3.3 KiB
Rust
use crate::{
|
|
client::ChurchApiClient,
|
|
error::Result,
|
|
models::{Bulletin, NewBulletin, PaginationParams, ApiListResponse, ApiVersion},
|
|
};
|
|
|
|
pub async fn get_bulletins(client: &ChurchApiClient, active_only: bool) -> Result<Vec<Bulletin>> {
|
|
let path = if active_only {
|
|
"/bulletins?active=true"
|
|
} else {
|
|
"/bulletins"
|
|
};
|
|
|
|
let response: ApiListResponse<Bulletin> = client.get_api_list(path).await?;
|
|
Ok(response.data.items)
|
|
}
|
|
|
|
pub async fn get_current_bulletin(client: &ChurchApiClient) -> Result<Option<Bulletin>> {
|
|
match client.get_api("/bulletins/current").await {
|
|
Ok(bulletin) => Ok(Some(bulletin)),
|
|
Err(crate::error::ChurchApiError::NotFound) => Ok(None),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub async fn get_bulletin(client: &ChurchApiClient, id: &str) -> Result<Option<Bulletin>> {
|
|
let path = format!("/bulletins/{}", id);
|
|
|
|
match client.get_api(&path).await {
|
|
Ok(bulletin) => Ok(Some(bulletin)),
|
|
Err(crate::error::ChurchApiError::NotFound) => Ok(None),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub async fn create_bulletin(client: &ChurchApiClient, bulletin: NewBulletin) -> Result<String> {
|
|
client.post_api("/bulletins", &bulletin).await
|
|
}
|
|
|
|
pub async fn get_next_bulletin(client: &ChurchApiClient) -> Result<Option<Bulletin>> {
|
|
match client.get_api("/bulletins/next").await {
|
|
Ok(bulletin) => Ok(Some(bulletin)),
|
|
Err(crate::error::ChurchApiError::NotFound) => Ok(None),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
// V2 API methods
|
|
pub async fn get_bulletins_v2(client: &ChurchApiClient, params: Option<PaginationParams>) -> Result<ApiListResponse<Bulletin>> {
|
|
let mut path = "/bulletins".to_string();
|
|
|
|
if let Some(params) = params {
|
|
let mut query_params = Vec::new();
|
|
|
|
if let Some(page) = params.page {
|
|
query_params.push(("page", page.to_string()));
|
|
}
|
|
|
|
if let Some(per_page) = params.per_page {
|
|
query_params.push(("per_page", per_page.to_string()));
|
|
}
|
|
|
|
if !query_params.is_empty() {
|
|
let query_string = query_params
|
|
.iter()
|
|
.map(|(k, v)| format!("{}={}", k, urlencoding::encode(v)))
|
|
.collect::<Vec<_>>()
|
|
.join("&");
|
|
path.push_str(&format!("?{}", query_string));
|
|
}
|
|
}
|
|
|
|
let url = client.build_url_with_version(&path, ApiVersion::V2);
|
|
let response: ApiListResponse<Bulletin> = client.get(&url).await?;
|
|
|
|
if response.success {
|
|
Ok(response)
|
|
} else {
|
|
Err(crate::error::ChurchApiError::Api(
|
|
response.error
|
|
.or(response.message)
|
|
.unwrap_or_else(|| "Unknown API error".to_string())
|
|
))
|
|
}
|
|
}
|
|
|
|
pub async fn get_current_bulletin_v2(client: &ChurchApiClient) -> Result<Option<Bulletin>> {
|
|
match client.get_api_with_version("/bulletins/current", ApiVersion::V2).await {
|
|
Ok(bulletin) => Ok(Some(bulletin)),
|
|
Err(crate::error::ChurchApiError::NotFound) => Ok(None),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub async fn get_next_bulletin_v2(client: &ChurchApiClient) -> Result<Option<Bulletin>> {
|
|
match client.get_api_with_version("/bulletins/next", ApiVersion::V2).await {
|
|
Ok(bulletin) => Ok(Some(bulletin)),
|
|
Err(crate::error::ChurchApiError::NotFound) => Ok(None),
|
|
Err(e) => Err(e),
|
|
}
|
|
} |