From aeab0d9447bbdc4e743f12b189b9df47e52b8b32 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 14 Jun 2026 18:54:55 -0400 Subject: [PATCH] Hardned add contact endpoint --- src/caller/mod.rs | 71 ++++++++++++++++++++++++++---------- src/repo/mod.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 20 deletions(-) diff --git a/src/caller/mod.rs b/src/caller/mod.rs index 9839b1a..1c04d29 100644 --- a/src/caller/mod.rs +++ b/src/caller/mod.rs @@ -76,29 +76,60 @@ pub mod contact { let mut response = super::response::AddContactResponse::default(); if payload.is_valid() { - let mut contact = textsender_models::contact::Contact { - firstname: payload.firstname, - lastname: payload.lastname, - phone_number: payload.phone_number, - 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)) + 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) => { - eprintln!("Error: {err:?}"); - response.message = String::from("Contact not created"); + 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::INTERNAL_SERVER_ERROR, - axum::Json(response), - ) + (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 { diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 7434ddb..32c5e61 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -26,4 +26,97 @@ pub mod contact { Err(_) => Err(sqlx::Error::RowNotFound), } } + + pub async fn get( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result { + match sqlx::query( + r#" + SELECT id, firstname, lastname, nickname, phone_number, user_id FROM "contacts" + WHERE + id = $1; + "#, + ) + .bind(id) + .fetch_one(pool) + .await + { + Ok(row) => { + let id: uuid::Uuid = row.try_get("id")?; + + let phone_number: String = row.try_get("phone_number")?; + let user_id: uuid::Uuid = row.try_get("user_id")?; + Ok(textsender_models::contact::Contact { + id: Some(id), + firstname: match row.try_get("firstname") { + Ok(val) => val, + Err(err) => { + eprintln!("Error: {err:?}"); + None + } + }, + lastname: match row.try_get("lastname") { + Ok(val) => val, + Err(err) => { + eprintln!("Error: {err:?}"); + None + } + }, + nickname: match row.try_get("nickname") { + Ok(val) => val, + Err(err) => { + eprintln!("Error: nickname has no value for contacts record {err:?}"); + None + } + }, + phone_number: phone_number, + user_id: Some(user_id), + }) + } + Err(_) => Err(sqlx::Error::RowNotFound), + } + } + + pub async fn get_with_user_id_and_phone( + pool: &sqlx::PgPool, + user_id: &uuid::Uuid, + phone_number: &str, + ) -> Result { + match sqlx::query( + r#" + SELECT id, firstname, lastname, nickname, phone_number, user_id FROM "contacts" + WHERE + phone_number = $1 AND user_id = $2; + "#, + ) + .bind(phone_number) + .bind(user_id) + .fetch_one(pool) + .await + { + Ok(row) => { + println!("In here"); + let id: uuid::Uuid = row.try_get("id")?; + let firstname: String = row.try_get("firstname")?; + let lastname: String = row.try_get("lastname")?; + let nickname: String = row.try_get("nickname")?; + let phone_number: String = row.try_get("phone_number")?; + let user_id: uuid::Uuid = row.try_get("user_id")?; + println!("Found"); + Ok(textsender_models::contact::Contact { + id: Some(id), + firstname: Some(firstname), + lastname: Some(lastname), + nickname: Some(nickname), + phone_number: phone_number, + user_id: Some(user_id), + }) + } + Err(err) => match err { + sqlx::Error::RowNotFound => Err(err), + _ => Err(sqlx::Error::WorkerCrashed), + }, + } + } }