diff --git a/Cargo.toml b/Cargo.toml index e44ebaf..be7312c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,5 @@ anyhow = "1.0" dotenv = "0.15" sea-orm-cli = "1.0.1" sea-orm = "1.0.1" +serde = { version = "1.0.210", features = ["derive"] } thiserror = "1.0" diff --git a/README.md b/README.md index c3f958d..3d2fd2f 100644 --- a/README.md +++ b/README.md @@ -113,5 +113,5 @@ sea-orm-cli migrate up ### Génération des entitées ```bash -sea-orm-cli generate entity -o entity/src/entities +sea-orm-cli generate entity -o entity/src/entities --with-serde both ``` \ No newline at end of file diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index 8fe4ba0..0ceef47 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [dependencies] anyhow = "1.0.89" -axum = "0.7.6" +axum = { version = "0.7.6", features = ["macros"] } listenfd = "1.0.1" tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] } @@ -15,6 +15,7 @@ sea-orm = { workspace = true, features = [ "runtime-tokio-rustls", "macros", ] } +serde.workspace = true thiserror.workspace = true entity = { path = "../../entity" } diff --git a/crates/backend/src/api/debug.rs b/crates/backend/src/api/debug.rs new file mode 100644 index 0000000..6234239 --- /dev/null +++ b/crates/backend/src/api/debug.rs @@ -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, DbErr> { + DebugEntity::find().all(db).await +} + +async fn add_random_debug_entry(State(AppState { db_connection }): State) { + 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, +} + +#[axum::debug_handler] +async fn debug( + State(AppState { db_connection }): State, +) -> Result, 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 { + axum::Router::new() + .route("/", routing::get(debug)) + .route("/add_random", routing::post(add_random_debug_entry)) +} diff --git a/crates/backend/src/api/mod.rs b/crates/backend/src/api/mod.rs new file mode 100644 index 0000000..8aaa3bd --- /dev/null +++ b/crates/backend/src/api/mod.rs @@ -0,0 +1,9 @@ +use axum::Router; + +use crate::AppState; + +mod debug; + +pub fn get_routes() -> Router { + Router::new().nest("/debug", debug::get_routes()) +} diff --git a/crates/backend/src/lib.rs b/crates/backend/src/lib.rs index 150174a..c9423b9 100644 --- a/crates/backend/src/lib.rs +++ b/crates/backend/src/lib.rs @@ -7,6 +7,7 @@ use thiserror::Error; use ::utils::config::{load_config, ConfigError}; +mod api; mod db; #[derive(Error, Debug)] @@ -27,10 +28,13 @@ pub struct AppState { pub async fn get_router() -> Result { let db_connection = db::get_connection().await?; + let state: AppState = AppState { db_connection }; Ok(Router::new() .route("/", get(|| async { "Hello, world!" })) - .fallback(fallback)) + .merge(api::get_routes()) + .fallback(fallback) + .with_state(state)) } async fn fallback(uri: Uri) -> (StatusCode, String) { diff --git a/entity/Cargo.toml b/entity/Cargo.toml index d41373f..6a75cc0 100644 --- a/entity/Cargo.toml +++ b/entity/Cargo.toml @@ -9,6 +9,7 @@ path = "src/lib.rs" [dependencies] sea-orm.workspace = true +serde.workspace = true [dev-dependencies] sea-orm-cli.workspace = true diff --git a/entity/src/entities/debug.rs b/entity/src/entities/debug.rs index 0a0b0a9..d08379e 100644 --- a/entity/src/entities/debug.rs +++ b/entity/src/entities/debug.rs @@ -1,8 +1,9 @@ //! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1 use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] #[sea_orm(table_name = "debug")] pub struct Model { #[sea_orm(primary_key)]