From a7d246d4f766938ca04d51d90a8c6160dd2440d1 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: Tue, 17 Dec 2024 02:29:41 +0300 Subject: [PATCH] feat: :sparkles: contact routing --- src/database/contact.rs | 11 +++++ src/feature/contact.rs | 40 ++++++++++++++++++ src/routing.rs | 5 +++ src/routing/contact.rs | 93 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 src/routing/contact.rs diff --git a/src/database/contact.rs b/src/database/contact.rs index 1dd3e0d..1971515 100644 --- a/src/database/contact.rs +++ b/src/database/contact.rs @@ -64,3 +64,14 @@ pub async fn delete( .fetch_one(database_connection) .await } + +pub async fn read_all(database_connection: &Pool) -> Result, sqlx::Error> { + sqlx::query_as!( + Contact, + r#" + SELECT * FROM "contact" + "#, + ) + .fetch_all(database_connection) + .await +} diff --git a/src/feature/contact.rs b/src/feature/contact.rs index e51a239..d3a92f2 100644 --- a/src/feature/contact.rs +++ b/src/feature/contact.rs @@ -1,7 +1,47 @@ use serde::{Deserialize, Serialize}; +use sqlx::{Pool, Postgres}; + +use crate::database::contact; #[derive(Debug, Serialize, Deserialize)] pub struct Contact { pub id: i64, pub name: String, } + +impl Contact { + pub async fn create( + name: &String, + database_connection: &Pool, + ) -> Result { + contact::create(name, database_connection).await + } + + pub async fn read( + id: &i64, + database_connection: &Pool, + ) -> Result { + contact::read(id, database_connection).await + } + + pub async fn update( + id: &i64, + name: &String, + database_connection: &Pool, + ) -> Result { + contact::update(id, name, database_connection).await + } + + pub async fn delete( + id: &i64, + database_connection: &Pool, + ) -> Result { + contact::delete(id, database_connection).await + } + + pub async fn read_all( + database_connection: &Pool, + ) -> Result, sqlx::Error> { + contact::read_all(database_connection).await + } +} diff --git a/src/routing.rs b/src/routing.rs index d5e0160..8aa28db 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -1,5 +1,6 @@ pub mod comment; pub mod comment_interaction; +pub mod contact; pub mod interaction; pub mod permission; pub mod post; @@ -48,6 +49,10 @@ pub async fn route(State(app_state): State) -> Router { "/role_permissions", role_permission::route(axum::extract::State(app_state.clone())), ) + .nest( + "/contacts", + contact::route(axum::extract::State(app_state.clone())), + ) .layer(CorsLayer::permissive()) .with_state(app_state) } diff --git a/src/routing/contact.rs b/src/routing/contact.rs new file mode 100644 index 0000000..d404ad5 --- /dev/null +++ b/src/routing/contact.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::contact::Contact, AppState}; + +#[derive(Debug, Serialize, Deserialize)] +struct CreateContact { + name: String, +} + +#[derive(Debug, Serialize, Deserialize)] +struct UpdateContact { + 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_contact): Json, +) -> impl IntoResponse { + match Contact::create(&create_contact.name, &app_state.database_connection).await { + Ok(contact) => (StatusCode::CREATED, Json(serde_json::json!(contact))), + 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 Contact::read(&id, &app_state.database_connection).await { + Ok(contact) => (StatusCode::OK, Json(serde_json::json!(contact))), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +} + +async fn update( + State(app_state): State, + Json(update_contact): Json, +) -> impl IntoResponse { + match Contact::update( + &update_contact.id, + &update_contact.name, + &app_state.database_connection, + ) + .await + { + Ok(contact) => (StatusCode::ACCEPTED, Json(serde_json::json!(contact))), + 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 Contact::delete(&id, &app_state.database_connection).await { + Ok(contact) => (StatusCode::NO_CONTENT, Json(serde_json::json!(contact))), + 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 Contact::read_all(&app_state.database_connection).await { + Ok(contacts) => (StatusCode::OK, Json(serde_json::json!(contacts))), + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +}