feat: implement session-based authentication and core API handlers for user management
This commit is contained in:
36
backend/src/handlers/diets.rs
Normal file
36
backend/src/handlers/diets.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
//! 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))
|
||||
}
|
||||
Reference in New Issue
Block a user