feat: routing routing

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-23 03:00:52 +03:00
parent 502334ea79
commit 511e05bd54
8 changed files with 192 additions and 0 deletions

36
src/routing/routing.rs Normal file
View file

@ -0,0 +1,36 @@
use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::get,
Json, Router,
};
use crate::{feature::routing::Routing, AppState};
pub fn route(State(app_state): State<AppState>) -> Router<AppState> {
Router::new()
.route("/:id", get(read))
.route("/", get(read_all))
.with_state(app_state)
}
async fn read(State(app_state): State<AppState>, Path(id): Path<i64>) -> impl IntoResponse {
match Routing::read(&id, &app_state.database_connection).await {
Ok(routing) => (StatusCode::OK, Json(serde_json::json!(routing))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all(State(app_state): State<AppState>) -> impl IntoResponse {
match Routing::read_all(&app_state.database_connection).await {
Ok(routings) => (StatusCode::OK, Json(serde_json::json!(routings))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}