use axum::Json; use axum::extract::{Path, State}; use crate::dto::ScheduledTaskResponse; use crate::error::ApiError; use crate::state::AppState; pub async fn list_scheduled_tasks( State(state): State, ) -> Result>, ApiError> { let tasks = state.scheduler.list_tasks().await; let responses: Vec = tasks .into_iter() .map(|t| ScheduledTaskResponse { id: t.id, name: t.name, schedule: t.schedule.display_string(), enabled: t.enabled, last_run: t.last_run.map(|dt| dt.to_rfc3339()), next_run: t.next_run.map(|dt| dt.to_rfc3339()), last_status: t.last_status, }) .collect(); Ok(Json(responses)) } pub async fn toggle_scheduled_task( State(state): State, Path(id): Path, ) -> Result, ApiError> { match state.scheduler.toggle_task(&id).await { Some(enabled) => Ok(Json(serde_json::json!({ "id": id, "enabled": enabled, }))), None => Err(ApiError(pinakes_core::error::PinakesError::NotFound( format!("scheduled task not found: {id}"), ))), } } pub async fn run_scheduled_task_now( State(state): State, Path(id): Path, ) -> Result, ApiError> { match state.scheduler.run_now(&id).await { Some(job_id) => Ok(Json(serde_json::json!({ "id": id, "job_id": job_id, }))), None => Err(ApiError(pinakes_core::error::PinakesError::NotFound( format!("scheduled task not found: {id}"), ))), } }