Skip to content

Commit

Permalink
Revamp nest
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas committed Jul 30, 2024
1 parent d516231 commit 6e2c781
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
15 changes: 11 additions & 4 deletions api/src/routers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use axum::routing::{get, post};
use axum::Router;

pub mod root;
pub mod user;

use app::state::AppState;
use root::create_root_router;
use user::create_user_router;
use root::root_get;
use user::{users_get, users_id_get, users_post};

pub fn create_router(state: AppState) -> Router {
let root_router = Router::new().route("/", get(root_get));
let user_router = Router::new()
.route("/", post(users_post).get(users_get))
.route("/:id", get(users_id_get));

Router::new()
.nest("/users", create_user_router(state.clone()))
.nest("/", create_root_router(state))
.nest("/", root_router)
.nest("/users", user_router)
.with_state(state)
}
8 changes: 2 additions & 6 deletions api/src/routers/root.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axum::{extract::State, routing::get, Router};
use axum::extract::State;
use sea_orm::{ConnectionTrait, Statement};

use app::state::AppState;
Expand All @@ -12,7 +12,7 @@ use crate::error::ApiError;
(status = 200, description = "Hello world", body = String)
)
)]
async fn root(state: State<AppState>) -> Result<String, ApiError> {
pub(super) async fn root_get(state: State<AppState>) -> Result<String, ApiError> {
let result = state
.conn
.query_one(Statement::from_string(
Expand All @@ -24,7 +24,3 @@ async fn root(state: State<AppState>) -> Result<String, ApiError> {

result.unwrap().try_get_by(0).map_err(|e| e.into())
}

pub fn create_root_router(state: AppState) -> Router {
Router::new().route("/", get(root)).with_state(state)
}
15 changes: 3 additions & 12 deletions api/src/routers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use axum::{
extract::{Path, Query, State},
http::StatusCode,
response::IntoResponse,
routing::{get, post},
Router,
};
use sea_orm::TryIntoModel;

Expand All @@ -28,7 +26,7 @@ use crate::extractor::{Json, Valid};
(status = 500, description = "Internal server error", body = ApiErrorResponse),
)
)]
async fn users_post(
pub(super) async fn users_post(
state: State<AppState>,
Valid(Json(params)): Valid<Json<CreateUserParams>>,
) -> Result<impl IntoResponse, ApiError> {
Expand All @@ -51,7 +49,7 @@ async fn users_post(
(status = 500, description = "Internal server error", body = ApiErrorResponse),
)
)]
async fn users_get(
pub(super) async fn users_get(
state: State<AppState>,
query: Option<Query<UserQuery>>,
) -> Result<impl IntoResponse, ApiError> {
Expand All @@ -74,7 +72,7 @@ async fn users_get(
(status = 500, description = "Internal server error", body = ApiErrorResponse),
)
)]
async fn users_id_get(
pub(super) async fn users_id_get(
state: State<AppState>,
Path(id): Path<i32>,
) -> Result<impl IntoResponse, ApiError> {
Expand All @@ -83,10 +81,3 @@ async fn users_id_get(
user.map(|user| Json(UserSchema::from(user)))
.ok_or_else(|| UserError::NotFound.into())
}

pub fn create_user_router(state: AppState) -> Router {
Router::new()
.route("/", post(users_post).get(users_get))
.route("/:id", get(users_id_get))
.with_state(state)
}
2 changes: 1 addition & 1 deletion doc/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ use utoipa::OpenApi;
use api::routers::root::*;

#[derive(OpenApi)]
#[openapi(paths(root))]
#[openapi(paths(root_get))]
pub(super) struct RootApi;

0 comments on commit 6e2c781

Please sign in to comment.