37 lines
876 B
Rust
37 lines
876 B
Rust
//! Diet API handlers.
|
|
|
|
use axum::{extract::State, http::StatusCode, Json};
|
|
use sea_orm::{DatabaseConnection, EntityTrait};
|
|
use serde::Serialize;
|
|
|
|
use crate::models::user::diet;
|
|
|
|
/// Response for a diet type.
|
|
#[derive(Serialize)]
|
|
pub struct DietResponse {
|
|
pub id: i32,
|
|
pub name: String,
|
|
pub description: Option<String>,
|
|
}
|
|
|
|
/// GET /api/diets - List all diet types for UI dropdown.
|
|
pub async fn list_diets(
|
|
State(db): State<DatabaseConnection>,
|
|
) -> Result<Json<Vec<DietResponse>>, StatusCode> {
|
|
let diets = diet::Entity::find()
|
|
.all(&db)
|
|
.await
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
let items: Vec<DietResponse> = diets
|
|
.into_iter()
|
|
.map(|d| DietResponse {
|
|
id: d.id,
|
|
name: d.name,
|
|
description: d.description,
|
|
})
|
|
.collect();
|
|
|
|
Ok(Json(items))
|
|
}
|