
Some checks failed
iOS UniFFI Build / build-ios (push) Has been cancelled
- Add api.rs with all web-specific API functions - Add admin_login and validate_admin_token methods to ChurchApiClient - Add image fields to EventSubmission model - Add submit_event_with_image and submit_event_multipart functions - Update test files for new EventSubmission fields This consolidates the web church-core into the main crate to achieve single source of truth.
196 lines
6.3 KiB
Rust
196 lines
6.3 KiB
Rust
use crate::{
|
|
client::ChurchApiClient,
|
|
error::Result,
|
|
models::{Event, NewEvent, EventUpdate, EventSubmission, PaginationParams, ApiListResponse, ApiVersion},
|
|
};
|
|
|
|
pub async fn get_events(client: &ChurchApiClient, params: Option<PaginationParams>) -> Result<ApiListResponse<Event>> {
|
|
let mut path = "/events".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 let Some(sort) = ¶ms.sort {
|
|
query_params.push(("sort", sort.clone()));
|
|
}
|
|
|
|
if let Some(filter) = ¶ms.filter {
|
|
query_params.push(("filter", filter.clone()));
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
|
|
client.get_api_list(&path).await
|
|
}
|
|
|
|
pub async fn get_upcoming_events(client: &ChurchApiClient, limit: Option<u32>) -> Result<Vec<Event>> {
|
|
let mut path = "/events/upcoming".to_string();
|
|
|
|
if let Some(limit) = limit {
|
|
path.push_str(&format!("?limit={}", limit));
|
|
}
|
|
|
|
client.get_api(&path).await
|
|
}
|
|
|
|
pub async fn get_event(client: &ChurchApiClient, id: &str) -> Result<Option<Event>> {
|
|
let path = format!("/events/{}", id);
|
|
|
|
match client.get_api(&path).await {
|
|
Ok(event) => Ok(Some(event)),
|
|
Err(crate::error::ChurchApiError::NotFound) => Ok(None),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub async fn create_event(client: &ChurchApiClient, event: NewEvent) -> Result<String> {
|
|
client.post_api("/events", &event).await
|
|
}
|
|
|
|
pub async fn update_event(client: &ChurchApiClient, id: &str, update: EventUpdate) -> Result<()> {
|
|
let path = format!("/events/{}", id);
|
|
client.put_api(&path, &update).await
|
|
}
|
|
|
|
pub async fn delete_event(client: &ChurchApiClient, id: &str) -> Result<()> {
|
|
let path = format!("/events/{}", id);
|
|
client.delete_api(&path).await
|
|
}
|
|
|
|
pub async fn get_featured_events(client: &ChurchApiClient, limit: Option<u32>) -> Result<Vec<Event>> {
|
|
let mut path = "/events/featured".to_string();
|
|
|
|
if let Some(limit) = limit {
|
|
path.push_str(&format!("?limit={}", limit));
|
|
}
|
|
|
|
client.get_api(&path).await
|
|
}
|
|
|
|
pub async fn get_events_by_category(client: &ChurchApiClient, category: &str, limit: Option<u32>) -> Result<Vec<Event>> {
|
|
let mut path = format!("/events/category/{}", category);
|
|
|
|
if let Some(limit) = limit {
|
|
path.push_str(&format!("?limit={}", limit));
|
|
}
|
|
|
|
client.get_api(&path).await
|
|
}
|
|
|
|
pub async fn get_events_by_date_range(
|
|
client: &ChurchApiClient,
|
|
start_date: &str,
|
|
end_date: &str
|
|
) -> Result<Vec<Event>> {
|
|
let path = format!("/events/range?start={}&end={}",
|
|
urlencoding::encode(start_date),
|
|
urlencoding::encode(end_date)
|
|
);
|
|
|
|
client.get_api(&path).await
|
|
}
|
|
|
|
pub async fn search_events(client: &ChurchApiClient, query: &str, limit: Option<u32>) -> Result<Vec<Event>> {
|
|
let mut path = format!("/events/search?q={}", urlencoding::encode(query));
|
|
|
|
if let Some(limit) = limit {
|
|
path.push_str(&format!("&limit={}", limit));
|
|
}
|
|
|
|
client.get_api(&path).await
|
|
}
|
|
|
|
pub async fn upload_event_image(client: &ChurchApiClient, event_id: &str, image_data: Vec<u8>, filename: String) -> Result<String> {
|
|
let path = format!("/events/{}/image", event_id);
|
|
client.upload_file(&path, image_data, filename, "image".to_string()).await
|
|
}
|
|
|
|
// V2 API methods
|
|
pub async fn get_events_v2(client: &ChurchApiClient, params: Option<PaginationParams>) -> Result<ApiListResponse<Event>> {
|
|
let mut path = "/events".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 let Some(sort) = ¶ms.sort {
|
|
query_params.push(("sort", sort.clone()));
|
|
}
|
|
|
|
if let Some(filter) = ¶ms.filter {
|
|
query_params.push(("filter", filter.clone()));
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
|
|
client.get_api_list_with_version(&path, ApiVersion::V2).await
|
|
}
|
|
|
|
pub async fn get_upcoming_events_v2(client: &ChurchApiClient, limit: Option<u32>) -> Result<Vec<Event>> {
|
|
let mut path = "/events/upcoming".to_string();
|
|
|
|
if let Some(limit) = limit {
|
|
path.push_str(&format!("?limit={}", limit));
|
|
}
|
|
|
|
client.get_api_with_version(&path, ApiVersion::V2).await
|
|
}
|
|
|
|
pub async fn get_featured_events_v2(client: &ChurchApiClient, limit: Option<u32>) -> Result<Vec<Event>> {
|
|
let mut path = "/events/featured".to_string();
|
|
|
|
if let Some(limit) = limit {
|
|
path.push_str(&format!("?limit={}", limit));
|
|
}
|
|
|
|
client.get_api_with_version(&path, ApiVersion::V2).await
|
|
}
|
|
|
|
pub async fn get_event_v2(client: &ChurchApiClient, id: &str) -> Result<Option<Event>> {
|
|
let path = format!("/events/{}", id);
|
|
|
|
match client.get_api_with_version(&path, ApiVersion::V2).await {
|
|
Ok(event) => Ok(Some(event)),
|
|
Err(crate::error::ChurchApiError::NotFound) => Ok(None),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub async fn submit_event(client: &ChurchApiClient, submission: EventSubmission) -> Result<String> {
|
|
client.post_api("/events/submit", &submission).await
|
|
}
|
|
|
|
pub async fn submit_event_with_image(client: &ChurchApiClient, submission: EventSubmission, image_data: Option<(Vec<u8>, String)>) -> Result<String> {
|
|
client.submit_event_multipart(&submission, image_data).await
|
|
} |