diff --git a/src/caller/contact.rs b/src/caller/contact.rs new file mode 100644 index 0000000..fbf6bb3 --- /dev/null +++ b/src/caller/contact.rs @@ -0,0 +1,123 @@ +pub mod request { + use serde::{Deserialize, Serialize}; + use utoipa::ToSchema; + + #[derive(Debug, Deserialize, Serialize, ToSchema)] + pub struct AddContactRequest { + pub phone_number: String, + pub firstname: Option, + pub lastname: Option, + pub nickname: Option, + pub user_id: uuid::Uuid, + } + + impl AddContactRequest { + pub fn is_valid(&self) -> bool { + !self.phone_number.is_empty() || !self.user_id.is_nil() + } + } +} + +pub mod response { + use serde::{Deserialize, Serialize}; + use utoipa::ToSchema; + + #[derive(Debug, Default, Deserialize, Serialize, ToSchema)] + pub struct AddContactResponse { + pub message: String, + pub data: Vec, + } +} + +pub mod endpoint { + // use axum::{Json, response::IntoResponse}; + + use crate::repo::contact as contact_repo; + + /// Endpoint to create Contact + #[utoipa::path( + post, + path = super::super::endpoints::ADD_CONTACT, + request_body( + content = super::request::AddContactRequest, + description = "Data needed to create a Contact", + content_type = "application/json" + ), + responses( + (status = 201, description = "Contact created", body = super::response::AddContactResponse), + (status = 400, description = "Error", body = super::response::AddContactResponse), + (status = 500, description = "Error creating Contact", body = super::response::AddContactResponse) + ) +)] + pub async fn create_contact( + axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::AddContactResponse::default(); + + if payload.is_valid() { + match contact_repo::get_with_user_id_and_phone( + &pool, + &payload.user_id, + &payload.phone_number, + ) + .await + { + Ok(_) => { + println!("Already created"); + response.message = String::from("Already created"); + (axum::http::StatusCode::NOT_MODIFIED, axum::Json(response)) + } + Err(err) => { + match err { + sqlx::Error::RowNotFound => { + println!("Good to create"); + // Put code here + let mut contact = textsender_models::contact::Contact { + firstname: payload.firstname, + lastname: payload.lastname, + phone_number: payload.phone_number, + nickname: payload.nickname, + user_id: Some(payload.user_id), + ..Default::default() + }; + match contact_repo::insert(&pool, &contact).await { + Ok(id) => { + contact.id = Some(id); + response.message = + String::from(super::super::response::SUCCESSFUL); + response.data.push(contact); + + (axum::http::StatusCode::CREATED, axum::Json(response)) + } + Err(err) => { + eprintln!("Error: {err:?}"); + response.message = String::from("Contact not created"); + + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } + } + } + _ => { + eprintln!("Error: {err:?}"); + response.message = String::from("Something went wrong"); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } + } + } + } + } else { + response.message = String::from("Request body is not valid"); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } +} diff --git a/src/caller/mod.rs b/src/caller/mod.rs index 1c04d29..0c3523e 100644 --- a/src/caller/mod.rs +++ b/src/caller/mod.rs @@ -1,3 +1,5 @@ +pub mod contact; + pub mod endpoints { pub const ROOT: &str = "/"; @@ -13,129 +15,3 @@ pub mod response { pub async fn root() -> &'static str { "Hello, World!" } - -pub mod contact { - pub mod request { - use serde::{Deserialize, Serialize}; - use utoipa::ToSchema; - - #[derive(Debug, Deserialize, Serialize, ToSchema)] - pub struct AddContactRequest { - pub phone_number: String, - pub firstname: Option, - pub lastname: Option, - pub nickname: Option, - pub user_id: uuid::Uuid, - } - - impl AddContactRequest { - pub fn is_valid(&self) -> bool { - !self.phone_number.is_empty() || !self.user_id.is_nil() - } - } - } - - pub mod response { - use serde::{Deserialize, Serialize}; - use utoipa::ToSchema; - - #[derive(Debug, Default, Deserialize, Serialize, ToSchema)] - pub struct AddContactResponse { - pub message: String, - pub data: Vec, - } - } - - pub mod endpoint { - // use axum::{Json, response::IntoResponse}; - - use crate::repo::contact as contact_repo; - - /// Endpoint to create Contact - #[utoipa::path( - post, - path = super::super::endpoints::ADD_CONTACT, - request_body( - content = super::request::AddContactRequest, - description = "Data needed to create a Contact", - content_type = "application/json" - ), - responses( - (status = 201, description = "Contact created", body = super::response::AddContactResponse), - (status = 400, description = "Error", body = super::response::AddContactResponse), - (status = 500, description = "Error creating Contact", body = super::response::AddContactResponse) - ) - )] - pub async fn create_contact( - axum::Extension(pool): axum::Extension, - axum::Json(payload): axum::Json, - ) -> ( - axum::http::StatusCode, - axum::Json, - ) { - let mut response = super::response::AddContactResponse::default(); - - if payload.is_valid() { - match contact_repo::get_with_user_id_and_phone( - &pool, - &payload.user_id, - &payload.phone_number, - ) - .await - { - Ok(_) => { - println!("Already created"); - response.message = String::from("Already created"); - (axum::http::StatusCode::NOT_MODIFIED, axum::Json(response)) - } - Err(err) => { - match err { - sqlx::Error::RowNotFound => { - println!("Good to create"); - // Put code here - let mut contact = textsender_models::contact::Contact { - firstname: payload.firstname, - lastname: payload.lastname, - phone_number: payload.phone_number, - nickname: payload.nickname, - user_id: Some(payload.user_id), - ..Default::default() - }; - match contact_repo::insert(&pool, &contact).await { - Ok(id) => { - contact.id = Some(id); - response.message = - String::from(super::super::response::SUCCESSFUL); - response.data.push(contact); - - (axum::http::StatusCode::CREATED, axum::Json(response)) - } - Err(err) => { - eprintln!("Error: {err:?}"); - response.message = String::from("Contact not created"); - - ( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response), - ) - } - } - } - _ => { - eprintln!("Error: {err:?}"); - response.message = String::from("Something went wrong"); - ( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response), - ) - } - } - } - } - } else { - response.message = String::from("Request body is not valid"); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) - } - } - } -}