From 0f09dd6a827391f34ea80f4c1d0939a0f670bd76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Sun, 15 Dec 2024 21:07:58 +0300 Subject: [PATCH] feat: :sparkles: interaction routing --- src/database/interaction.rs | 19 ++++++-- src/feature/interaction.rs | 40 ++++++++++++++++ src/routing.rs | 5 ++ src/routing/interaction.rs | 93 +++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 src/routing/interaction.rs diff --git a/src/database/interaction.rs b/src/database/interaction.rs index 76b231e..3e41d64 100644 --- a/src/database/interaction.rs +++ b/src/database/interaction.rs @@ -20,15 +20,15 @@ pub async fn create( } pub async fn read( - name: &String, + id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( Interaction, r#" - SELECT * FROM "interaction" WHERE "name" = $1 + SELECT * FROM "interaction" WHERE "id" = $1 "#, - name + id ) .fetch_one(database_connection) .await @@ -67,3 +67,16 @@ pub async fn delete( .fetch_one(database_connection) .await } + +pub async fn read_all( + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + Interaction, + r#" + SELECT * FROM "interaction" + "#, + ) + .fetch_all(database_connection) + .await +} diff --git a/src/feature/interaction.rs b/src/feature/interaction.rs index 6064701..e5c4cf8 100644 --- a/src/feature/interaction.rs +++ b/src/feature/interaction.rs @@ -1,7 +1,47 @@ use serde::{Deserialize, Serialize}; +use sqlx::{Pool, Postgres}; + +use crate::database::interaction; #[derive(Debug, Serialize, Deserialize)] pub struct Interaction { pub id: i64, pub name: String, } + +impl Interaction { + pub async fn create( + name: &String, + database_connection: &Pool, + ) -> Result { + interaction::create(name, database_connection).await + } + + pub async fn read( + id: &i64, + database_connection: &Pool, + ) -> Result { + interaction::read(id, database_connection).await + } + + pub async fn update( + id: &i64, + name: &String, + database_connection: &Pool, + ) -> Result { + interaction::update(id, name, database_connection).await + } + + pub async fn delete( + id: &i64, + database_connection: &Pool, + ) -> Result { + interaction::delete(id, database_connection).await + } + + pub async fn read_all( + database_connection: &Pool, + ) -> Result, sqlx::Error> { + interaction::read_all(database_connection).await + } +} diff --git a/src/routing.rs b/src/routing.rs index 7dc243b..6d12e1f 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -1,4 +1,5 @@ pub mod comment; +pub mod interaction; pub mod post; pub mod role; pub mod user; @@ -27,6 +28,10 @@ pub async fn route(State(app_state): State) -> Router { "/comments", comment::route(axum::extract::State(app_state.clone())), ) + .nest( + "/interactions", + interaction::route(axum::extract::State(app_state.clone())), + ) .layer(CorsLayer::permissive()) .with_state(app_state) } diff --git a/src/routing/interaction.rs b/src/routing/interaction.rs new file mode 100644 index 0000000..fb16ae6 --- /dev/null +++ b/src/routing/interaction.rs @@ -0,0 +1,93 @@ +use axum::{ + extract::{Path, State}, + http::StatusCode, + response::IntoResponse, + routing::{delete, get, patch, post}, + Json, Router, +}; +use serde::{Deserialize, Serialize}; + +use crate::{feature::interaction::Interaction, AppState}; + +#[derive(Debug, Serialize, Deserialize)] +struct CreateInteraction { + name: String, +} + +#[derive(Debug, Serialize, Deserialize)] +struct UpdateInteraction { + id: i64, + name: String, +} + +pub fn route(State(app_state): State) -> Router { + Router::new() + .route("/", post(create)) + .route("/:id", get(read)) + .route("/", patch(update)) + .route("/:id", delete(delete_)) + .route("/", get(read_all)) + .with_state(app_state) +} + +async fn create( + State(app_state): State, + Json(create_interaction): Json, +) -> impl IntoResponse { + match Interaction::create(&create_interaction.name, &app_state.database_connection).await { + Ok(interaction) => (StatusCode::CREATED, Json(serde_json::json!(interaction))), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +} + +async fn read(State(app_state): State, Path(id): Path) -> impl IntoResponse { + match Interaction::read(&id, &app_state.database_connection).await { + Ok(interaction) => (StatusCode::OK, Json(serde_json::json!(interaction))), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +} + +async fn update( + State(app_state): State, + Json(update_interaction): Json, +) -> impl IntoResponse { + match Interaction::update( + &update_interaction.id, + &update_interaction.name, + &app_state.database_connection, + ) + .await + { + Ok(interaction) => (StatusCode::ACCEPTED, Json(serde_json::json!(interaction))), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +} + +async fn delete_(State(app_state): State, Path(id): Path) -> impl IntoResponse { + match Interaction::delete(&id, &app_state.database_connection).await { + Ok(interaction) => (StatusCode::NO_CONTENT, Json(serde_json::json!(interaction))), + 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 Interaction::read_all(&app_state.database_connection).await { + Ok(interactions) => (StatusCode::OK, Json(serde_json::json!(interactions))), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +}