use axum::{
extract::{Path, State},
http::StatusCode,
Json,
};
use super::response::ApiResponse;
use crate::state::BosonState;
#[derive(Debug, serde::Serialize)]
pub struct TaskResponse {
pub name: String,
pub signature_json: String,
pub signature_hash: u64,
pub default_priority: i32,
pub default_pool: String,
}
pub async fn list_tasks(State(state): State<BosonState>) -> Json<ApiResponse<Vec<TaskResponse>>> {
let list: Vec<TaskResponse> = state
.boson
.registry()
.iter()
.map(|d| TaskResponse {
name: d.name.to_string(),
signature_json: d.signature_json.to_string(),
signature_hash: d.signature_hash,
default_priority: d.default_priority,
default_pool: d.default_pool.to_string(),
})
.collect();
Json(ApiResponse::ok(list))
}
pub async fn get_task(
State(state): State<BosonState>,
Path(name): Path<String>,
) -> (StatusCode, Json<ApiResponse<TaskResponse>>) {
state.boson.registry().get(&name).map_or_else(
|| {
(
StatusCode::NOT_FOUND,
Json(ApiResponse::err(format!("Task '{name}' not found"))),
)
},
|d| {
(
StatusCode::OK,
Json(ApiResponse::ok(TaskResponse {
name: d.name.to_string(),
signature_json: d.signature_json.to_string(),
signature_hash: d.signature_hash,
default_priority: d.default_priority,
default_pool: d.default_pool.to_string(),
})),
)
},
)
}