feat: ✨ routing routing
This commit is contained in:
parent
502334ea79
commit
511e05bd54
8 changed files with 192 additions and 0 deletions
2
migrations/20241223234406_routing.down.sql
Normal file
2
migrations/20241223234406_routing.down.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
-- Add down migration script here
|
||||
DROP TABLE IF EXISTS "routing";
|
5
migrations/20241223234406_routing.up.sql
Normal file
5
migrations/20241223234406_routing.up.sql
Normal file
|
@ -0,0 +1,5 @@
|
|||
-- Add up migration script here
|
||||
CREATE TABLE IF NOT EXISTS "routing"(
|
||||
id BIGSERIAL PRIMARY KEY UNIQUE NOT NULL,
|
||||
endpoint VARCHAR(255) UNIQUE NOT NULL
|
||||
);
|
|
@ -7,6 +7,7 @@ pub mod post;
|
|||
pub mod post_interaction;
|
||||
pub mod role;
|
||||
pub mod role_permission;
|
||||
pub mod routing;
|
||||
pub mod user;
|
||||
pub mod user_contact;
|
||||
|
||||
|
|
89
src/database/routing.rs
Normal file
89
src/database/routing.rs
Normal file
|
@ -0,0 +1,89 @@
|
|||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::feature::routing::Routing;
|
||||
|
||||
pub async fn create(
|
||||
endpoint: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Routing, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Routing,
|
||||
r#"
|
||||
INSERT INTO "routing"(endpoint)
|
||||
VALUES ($1)
|
||||
RETURNING *
|
||||
"#,
|
||||
endpoint,
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read(id: &i64, database_connection: &Pool<Postgres>) -> Result<Routing, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Routing,
|
||||
r#"
|
||||
SELECT * FROM "routing" WHERE "id" = $1
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
id: &i64,
|
||||
endpoint: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Routing, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Routing,
|
||||
r#"
|
||||
UPDATE "routing" SET "endpoint" = $2 WHERE "id" = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
id,
|
||||
endpoint,
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Routing, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Routing,
|
||||
r#"
|
||||
DELETE FROM "routing" WHERE "id" = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<Routing>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Routing,
|
||||
r#"
|
||||
SELECT * FROM "routing"
|
||||
"#,
|
||||
)
|
||||
.fetch_all(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_all(database_connection: &Pool<Postgres>) -> Result<Vec<Routing>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Routing,
|
||||
r#"
|
||||
DELETE FROM "routing"
|
||||
RETURNING *
|
||||
"#,
|
||||
)
|
||||
.fetch_all(database_connection)
|
||||
.await
|
||||
}
|
|
@ -7,5 +7,6 @@ pub mod post;
|
|||
pub mod post_interaction;
|
||||
pub mod role;
|
||||
pub mod role_permission;
|
||||
pub mod routing;
|
||||
pub mod user;
|
||||
pub mod user_contact;
|
||||
|
|
53
src/feature/routing.rs
Normal file
53
src/feature/routing.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::database::routing;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Routing {
|
||||
pub id: i64,
|
||||
pub endpoint: String,
|
||||
}
|
||||
|
||||
impl Routing {
|
||||
pub async fn create(
|
||||
endpoint: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Routing, sqlx::Error> {
|
||||
routing::create(endpoint, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn read(
|
||||
id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Routing, sqlx::Error> {
|
||||
routing::read(id, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
id: &i64,
|
||||
endpoint: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Routing, sqlx::Error> {
|
||||
routing::update(id, endpoint, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Routing, sqlx::Error> {
|
||||
routing::delete(id, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn read_all(
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Vec<Routing>, sqlx::Error> {
|
||||
routing::read_all(database_connection).await
|
||||
}
|
||||
|
||||
pub async fn delete_all(
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Vec<Routing>, sqlx::Error> {
|
||||
routing::delete_all(database_connection).await
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ pub mod post;
|
|||
pub mod post_interaction;
|
||||
pub mod role;
|
||||
pub mod role_permission;
|
||||
pub mod routing;
|
||||
pub mod user;
|
||||
pub mod user_contact;
|
||||
|
||||
|
@ -58,6 +59,10 @@ pub async fn route(State(app_state): State<AppState>) -> Router {
|
|||
"/user_contacts",
|
||||
user_contact::route(axum::extract::State(app_state.clone())),
|
||||
)
|
||||
.nest(
|
||||
"/routings",
|
||||
routing::route(axum::extract::State(app_state.clone())),
|
||||
)
|
||||
.layer(CorsLayer::permissive())
|
||||
.with_state(app_state)
|
||||
}
|
||||
|
|
36
src/routing/routing.rs
Normal file
36
src/routing/routing.rs
Normal 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())),
|
||||
),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue