feat: ✨ role routing
This commit is contained in:
parent
00d6bd5b93
commit
d0187b1b42
7 changed files with 172 additions and 6 deletions
|
@ -19,16 +19,13 @@ pub async fn create(
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn read(
|
pub async fn read(id: &i64, database_connection: &Pool<Postgres>) -> Result<Role, sqlx::Error> {
|
||||||
name: &String,
|
|
||||||
database_connection: &Pool<Postgres>,
|
|
||||||
) -> Result<Role, sqlx::Error> {
|
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
Role,
|
Role,
|
||||||
r#"
|
r#"
|
||||||
SELECT * FROM "role" WHERE "name" = $1
|
SELECT * FROM "role" WHERE "id" = $1
|
||||||
"#,
|
"#,
|
||||||
name
|
id
|
||||||
)
|
)
|
||||||
.fetch_one(database_connection)
|
.fetch_one(database_connection)
|
||||||
.await
|
.await
|
||||||
|
@ -64,3 +61,14 @@ pub async fn delete(id: &i64, database_connection: &Pool<Postgres>) -> Result<Ro
|
||||||
.fetch_one(database_connection)
|
.fetch_one(database_connection)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<Role>, sqlx::Error> {
|
||||||
|
sqlx::query_as!(
|
||||||
|
Role,
|
||||||
|
r#"
|
||||||
|
SELECT * FROM "role"
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_all(database_connection)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,42 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::{Pool, Postgres};
|
||||||
|
|
||||||
|
use crate::database::role;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Role {
|
pub struct Role {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Role {
|
||||||
|
pub async fn create(
|
||||||
|
name: &String,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Role, sqlx::Error> {
|
||||||
|
role::create(name, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read(id: &i64, database_connection: &Pool<Postgres>) -> Result<Role, sqlx::Error> {
|
||||||
|
role::read(id, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update(
|
||||||
|
id: &i64,
|
||||||
|
name: &String,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Role, sqlx::Error> {
|
||||||
|
role::update(id, name, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete(
|
||||||
|
id: &i64,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Role, sqlx::Error> {
|
||||||
|
role::delete(id, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<Role>, sqlx::Error> {
|
||||||
|
role::read_all(database_connection).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod feature;
|
pub mod feature;
|
||||||
|
pub mod routing;
|
||||||
|
pub mod server;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
use sqlx::{Pool, Postgres};
|
use sqlx::{Pool, Postgres};
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -1,4 +1,17 @@
|
||||||
|
use rust_forum::{
|
||||||
|
database::{establish_connection, set_database_up},
|
||||||
|
server::start_server,
|
||||||
|
AppState,
|
||||||
|
};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
||||||
|
let app_state = AppState {
|
||||||
|
database_connection: establish_connection().await,
|
||||||
|
};
|
||||||
|
set_database_up(&app_state.database_connection).await;
|
||||||
|
|
||||||
|
start_server(app_state).await;
|
||||||
}
|
}
|
||||||
|
|
24
src/routing.rs
Normal file
24
src/routing.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
pub mod role;
|
||||||
|
|
||||||
|
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Router};
|
||||||
|
use tower_http::cors::CorsLayer;
|
||||||
|
|
||||||
|
use crate::{database, AppState};
|
||||||
|
|
||||||
|
pub async fn route(State(app_state): State<AppState>) -> Router {
|
||||||
|
Router::new()
|
||||||
|
.route("/", get(alive))
|
||||||
|
.nest(
|
||||||
|
"/role",
|
||||||
|
role::route(axum::extract::State(app_state.clone())),
|
||||||
|
)
|
||||||
|
.layer(CorsLayer::permissive())
|
||||||
|
.with_state(app_state)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn alive(State(app_state): State<AppState>) -> impl IntoResponse {
|
||||||
|
match database::is_alive(&app_state.database_connection).await {
|
||||||
|
true => StatusCode::OK,
|
||||||
|
false => StatusCode::SERVICE_UNAVAILABLE,
|
||||||
|
}
|
||||||
|
}
|
72
src/routing/role.rs
Normal file
72
src/routing/role.rs
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
response::IntoResponse,
|
||||||
|
routing::{delete, get, patch, post},
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{feature::role::Role, AppState};
|
||||||
|
|
||||||
|
pub fn route(State(app_state): State<AppState>) -> Router<AppState> {
|
||||||
|
Router::new()
|
||||||
|
.route("/:name", post(create))
|
||||||
|
.route("/:id", get(read))
|
||||||
|
.route("/:id/:name", patch(update))
|
||||||
|
.route("/:id", delete(_delete))
|
||||||
|
.route("/", get(read_all))
|
||||||
|
.with_state(app_state)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create(Path(name): Path<String>, State(app_state): State<AppState>) -> impl IntoResponse {
|
||||||
|
match Role::create(&name, &app_state.database_connection).await {
|
||||||
|
Ok(role) => (StatusCode::CREATED, Json(serde_json::json!(role))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read(Path(id): Path<i64>, State(app_state): State<AppState>) -> impl IntoResponse {
|
||||||
|
match Role::read(&id, &app_state.database_connection).await {
|
||||||
|
Ok(role) => (StatusCode::OK, Json(serde_json::json!(role))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(
|
||||||
|
Path((id, name)): Path<(i64, String)>,
|
||||||
|
State(app_state): State<AppState>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match Role::update(&id, &name, &app_state.database_connection).await {
|
||||||
|
Ok(role) => (StatusCode::ACCEPTED, Json(serde_json::json!(role))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn _delete(Path(id): Path<i64>, State(app_state): State<AppState>) -> impl IntoResponse {
|
||||||
|
match Role::delete(&id, &app_state.database_connection).await {
|
||||||
|
Ok(role) => (StatusCode::NO_CONTENT, Json(serde_json::json!(role))),
|
||||||
|
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 Role::read_all(&app_state.database_connection).await {
|
||||||
|
Ok(roles) => (StatusCode::OK, Json(serde_json::json!(roles))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
12
src/server.rs
Normal file
12
src/server.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
|
use crate::{AppState, ServerConfig};
|
||||||
|
|
||||||
|
pub async fn start_server(app_state: AppState) {
|
||||||
|
let server_config = ServerConfig::default();
|
||||||
|
|
||||||
|
let router = crate::routing::route(axum::extract::State(app_state)).await;
|
||||||
|
let listener = TcpListener::bind(&server_config.address).await.unwrap();
|
||||||
|
println!("\n\thttp://{}", server_config.address);
|
||||||
|
axum::serve(listener, router).await.unwrap()
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue