diff --git a/migrations/20241223234406_routing.down.sql b/migrations/20241223234406_routing.down.sql new file mode 100644 index 0000000..a510791 --- /dev/null +++ b/migrations/20241223234406_routing.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS "routing"; \ No newline at end of file diff --git a/migrations/20241223234406_routing.up.sql b/migrations/20241223234406_routing.up.sql new file mode 100644 index 0000000..ddd20df --- /dev/null +++ b/migrations/20241223234406_routing.up.sql @@ -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 +); \ No newline at end of file diff --git a/src/database.rs b/src/database.rs index 2546635..77160b7 100644 --- a/src/database.rs +++ b/src/database.rs @@ -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; diff --git a/src/database/routing.rs b/src/database/routing.rs new file mode 100644 index 0000000..ae0bc40 --- /dev/null +++ b/src/database/routing.rs @@ -0,0 +1,89 @@ +use sqlx::{Pool, Postgres}; + +use crate::feature::routing::Routing; + +pub async fn create( + endpoint: &String, + database_connection: &Pool, +) -> Result { + 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) -> Result { + 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, +) -> Result { + 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, +) -> Result { + 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) -> Result, sqlx::Error> { + sqlx::query_as!( + Routing, + r#" + SELECT * FROM "routing" + "#, + ) + .fetch_all(database_connection) + .await +} + +pub async fn delete_all(database_connection: &Pool) -> Result, sqlx::Error> { + sqlx::query_as!( + Routing, + r#" + DELETE FROM "routing" + RETURNING * + "#, + ) + .fetch_all(database_connection) + .await +} diff --git a/src/feature.rs b/src/feature.rs index 77e11d4..1039cde 100644 --- a/src/feature.rs +++ b/src/feature.rs @@ -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; diff --git a/src/feature/routing.rs b/src/feature/routing.rs new file mode 100644 index 0000000..80938bd --- /dev/null +++ b/src/feature/routing.rs @@ -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, + ) -> Result { + routing::create(endpoint, database_connection).await + } + + pub async fn read( + id: &i64, + database_connection: &Pool, + ) -> Result { + routing::read(id, database_connection).await + } + + pub async fn update( + id: &i64, + endpoint: &String, + database_connection: &Pool, + ) -> Result { + routing::update(id, endpoint, database_connection).await + } + + pub async fn delete( + id: &i64, + database_connection: &Pool, + ) -> Result { + routing::delete(id, database_connection).await + } + + pub async fn read_all( + database_connection: &Pool, + ) -> Result, sqlx::Error> { + routing::read_all(database_connection).await + } + + pub async fn delete_all( + database_connection: &Pool, + ) -> Result, sqlx::Error> { + routing::delete_all(database_connection).await + } +} diff --git a/src/routing.rs b/src/routing.rs index dc0bbd5..6eff30b 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -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) -> 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) } diff --git a/src/routing/routing.rs b/src/routing/routing.rs new file mode 100644 index 0000000..2bae7e4 --- /dev/null +++ b/src/routing/routing.rs @@ -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) -> Router { + Router::new() + .route("/:id", get(read)) + .route("/", get(read_all)) + .with_state(app_state) +} + +async fn read(State(app_state): State, Path(id): Path) -> 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) -> 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())), + ), + } +}