49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
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))
|
|
}
|