feat: fixup "add a DEBUG page to the UI with a database usage example"
This commit is contained in:
48
crates/backend/src/api/debug.rs
Normal file
48
crates/backend/src/api/debug.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use axum::{extract::State, routing, Json};
|
||||
use sea_orm::*;
|
||||
use serde::Serialize;
|
||||
|
||||
use ::entity::{debug, debug::Entity as DebugEntity};
|
||||
|
||||
use crate::{AppError, AppState};
|
||||
|
||||
// DATABASE DEBUG CONTROLLERS
|
||||
|
||||
async fn get_debug_entries(db: &DatabaseConnection) -> Result<Vec<debug::Model>, DbErr> {
|
||||
DebugEntity::find().all(db).await
|
||||
}
|
||||
|
||||
async fn add_random_debug_entry(State(AppState { db_connection }): State<AppState>) {
|
||||
let random_entry = debug::ActiveModel {
|
||||
title: Set("Random title".to_string()),
|
||||
text: Set("Random text".to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
random_entry.insert(&db_connection).await.unwrap();
|
||||
}
|
||||
|
||||
// API HANDLER
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct DebugResponse {
|
||||
db_ping_status: bool,
|
||||
entries: Vec<debug::Model>,
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn debug(
|
||||
State(AppState { db_connection }): State<AppState>,
|
||||
) -> Result<Json<DebugResponse>, AppError> {
|
||||
let db_ping_status = db_connection.ping().await.is_ok();
|
||||
let debug_entries = get_debug_entries(&db_connection).await?;
|
||||
Ok(Json(DebugResponse {
|
||||
db_ping_status,
|
||||
entries: debug_entries,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn get_routes() -> axum::Router<crate::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", routing::get(debug))
|
||||
.route("/add_random", routing::post(add_random_debug_entry))
|
||||
}
|
Reference in New Issue
Block a user