From 04b39ba9c9a2740246fb6d4ef08c0f4f1fd76b80 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: Mon, 16 Dec 2024 01:26:22 +0300 Subject: [PATCH] feat: :sparkles: comment_interaction routing --- src/feature/comment_interaction.rs | 49 +++++++++++ src/routing.rs | 5 ++ src/routing/comment_interaction.rs | 134 +++++++++++++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 src/routing/comment_interaction.rs diff --git a/src/feature/comment_interaction.rs b/src/feature/comment_interaction.rs index d31b1e1..e97d773 100644 --- a/src/feature/comment_interaction.rs +++ b/src/feature/comment_interaction.rs @@ -1,5 +1,8 @@ use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; +use sqlx::{Pool, Postgres}; + +use crate::database::comment_interaction; #[derive(Debug, Serialize, Deserialize)] pub struct CommentInteraction { @@ -8,3 +11,49 @@ pub struct CommentInteraction { pub interaction_id: i64, pub user_id: i64, } + +impl CommentInteraction { + pub async fn create( + comment_creation_time: &DateTime, + user_id: &i64, + interaction_id: &i64, + database_connection: &Pool, + ) -> Result { + comment_interaction::create( + comment_creation_time, + user_id, + interaction_id, + database_connection, + ) + .await + } + + pub async fn read( + interaction_time: &DateTime, + database_connection: &Pool, + ) -> Result { + comment_interaction::read(interaction_time, database_connection).await + } + + pub async fn update( + interaction_time: &DateTime, + interaction_id: &i64, + database_connection: &Pool, + ) -> Result { + comment_interaction::update(interaction_time, interaction_id, database_connection).await + } + + pub async fn delete( + interaction_time: &DateTime, + database_connection: &Pool, + ) -> Result { + comment_interaction::delete(interaction_time, database_connection).await + } + + pub async fn read_all_for_comment( + comment_creation_time: &DateTime, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + comment_interaction::read_all_for_comment(comment_creation_time, database_connection).await + } +} diff --git a/src/routing.rs b/src/routing.rs index fd40c63..f019448 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -1,4 +1,5 @@ pub mod comment; +pub mod comment_interaction; pub mod interaction; pub mod post; pub mod post_interaction; @@ -37,6 +38,10 @@ pub async fn route(State(app_state): State) -> Router { "/post_interactions", post_interaction::route(axum::extract::State(app_state.clone())), ) + .nest( + "/comment_interactions", + comment_interaction::route(axum::extract::State(app_state.clone())), + ) .layer(CorsLayer::permissive()) .with_state(app_state) } diff --git a/src/routing/comment_interaction.rs b/src/routing/comment_interaction.rs new file mode 100644 index 0000000..c6ada14 --- /dev/null +++ b/src/routing/comment_interaction.rs @@ -0,0 +1,134 @@ +use axum::{ + extract::{Path, State}, + http::StatusCode, + response::IntoResponse, + routing::{delete, get, patch, post}, + Json, Router, +}; +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; + +use crate::{feature::comment_interaction::CommentInteraction, AppState}; + +#[derive(Debug, Serialize, Deserialize)] +struct CreateCommentInteraction { + pub comment_creation_time: DateTime, + pub interaction_id: i64, + pub user_id: i64, +} + +#[derive(Debug, Serialize, Deserialize)] +struct UpdateCommentInteraction { + pub interaction_time: DateTime, + pub comment_creation_time: DateTime, + pub interaction_id: i64, + pub user_id: i64, +} + +pub fn route(State(app_state): State) -> Router { + Router::new() + .route("/", post(create)) + .route("/:interaction_time", get(read)) + .route("/", patch(update)) + .route("/:interaction_time", delete(delete_)) + .route( + "/comments/:comment_creation_time", + get(read_all_for_comment), + ) + .with_state(app_state) +} + +async fn create( + State(app_state): State, + Json(create_comment_interaction): Json, +) -> impl IntoResponse { + match CommentInteraction::create( + &create_comment_interaction.comment_creation_time, + &create_comment_interaction.user_id, + &create_comment_interaction.interaction_id, + &app_state.database_connection, + ) + .await + { + Ok(comment_interaction) => ( + StatusCode::CREATED, + Json(serde_json::json!(comment_interaction)), + ), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +} + +async fn read( + State(app_state): State, + Path(interaction_time): Path>, +) -> impl IntoResponse { + match CommentInteraction::read(&interaction_time, &app_state.database_connection).await { + Ok(comment_interaction) => (StatusCode::OK, Json(serde_json::json!(comment_interaction))), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +} + +async fn update( + State(app_state): State, + Json(update_comment_interaction): Json, +) -> impl IntoResponse { + match CommentInteraction::update( + &update_comment_interaction.interaction_time, + &update_comment_interaction.interaction_id, + &app_state.database_connection, + ) + .await + { + Ok(comment_interaction) => ( + StatusCode::ACCEPTED, + Json(serde_json::json!(comment_interaction)), + ), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +} + +async fn delete_( + State(app_state): State, + Path(interaction_time): Path>, +) -> impl IntoResponse { + match CommentInteraction::delete(&interaction_time, &app_state.database_connection).await { + Ok(comment_interaction) => ( + StatusCode::NO_CONTENT, + Json(serde_json::json!(comment_interaction)), + ), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +} + +async fn read_all_for_comment( + State(app_state): State, + Path(comment_creation_time): Path>, +) -> impl IntoResponse { + match CommentInteraction::read_all_for_comment( + &comment_creation_time, + &app_state.database_connection, + ) + .await + { + Ok(comment_interactions) => ( + StatusCode::OK, + Json(serde_json::json!(comment_interactions)), + ), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +}