Files
zhealth/backend/src/handlers/categories.rs

37 lines
956 B
Rust

//! Biomarker category API handlers.
use axum::{extract::State, http::StatusCode, Json};
use sea_orm::{DatabaseConnection, EntityTrait};
use serde::Serialize;
use crate::models::bio::biomarker_category;
/// Response for a biomarker category.
#[derive(Serialize)]
pub struct CategoryResponse {
pub id: i32,
pub name: String,
pub description: Option<String>,
}
/// GET /api/categories - List all biomarker categories.
pub async fn list_categories(
State(db): State<DatabaseConnection>,
) -> Result<Json<Vec<CategoryResponse>>, StatusCode> {
let categories = biomarker_category::Entity::find()
.all(&db)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let items: Vec<CategoryResponse> = categories
.into_iter()
.map(|c| CategoryResponse {
id: c.id,
name: c.name,
description: c.description,
})
.collect();
Ok(Json(items))
}