Working state
This commit is contained in:
parent
2da103d5b5
commit
466f8dd992
@ -2,6 +2,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use axum::{serve, Router};
|
use axum::{serve, Router};
|
||||||
use config::db::init_db;
|
use config::db::init_db;
|
||||||
|
use dotenvy::dotenv;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use crate::routes::user::controller::UserController;
|
use crate::routes::user::controller::UserController;
|
||||||
use crate::routes::user::service::UserService;
|
use crate::routes::user::service::UserService;
|
||||||
@ -12,6 +13,7 @@ mod routes;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
dotenv().ok();
|
||||||
let pool = init_db();
|
let pool = init_db();
|
||||||
|
|
||||||
let user_service = UserService::new(pool);
|
let user_service = UserService::new(pool);
|
||||||
|
@ -2,6 +2,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
|
use axum::response::IntoResponse;
|
||||||
use axum::{Json, Router};
|
use axum::{Json, Router};
|
||||||
use axum::routing::get;
|
use axum::routing::get;
|
||||||
use crate::base::controller::BaseController;
|
use crate::base::controller::BaseController;
|
||||||
@ -28,16 +29,9 @@ impl UserController {
|
|||||||
|
|
||||||
async fn get_users(
|
async fn get_users(
|
||||||
State(controller): State<Arc<UserController>>,
|
State(controller): State<Arc<UserController>>,
|
||||||
) -> Result<Json<Vec<User>>, (StatusCode, Json<String>)> {
|
) -> Result<impl IntoResponse, (StatusCode, Json<String>)> {
|
||||||
match controller.base_controller.get_all().await {
|
controller.base_controller.get_all().await.map_err(|err| {
|
||||||
Ok(users) => {
|
(StatusCode::INTERNAL_SERVER_ERROR, Json(err))
|
||||||
// Wrap the users in Json, which implements IntoResponse
|
})
|
||||||
Ok(Json(users))
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
// In case of error, return StatusCode with Json error message
|
|
||||||
Err((StatusCode::INTERNAL_SERVER_ERROR, Json(err.to_string())))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user