diff --git a/src/caller/contact.rs b/src/caller/contact.rs index fbf6bb3..d1d296d 100644 --- a/src/caller/contact.rs +++ b/src/caller/contact.rs @@ -16,6 +16,12 @@ pub mod request { !self.phone_number.is_empty() || !self.user_id.is_nil() } } + + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct GetContactParams { + pub id: Option, + pub user_id: Option, + } } pub mod response { @@ -27,6 +33,16 @@ pub mod response { pub message: String, pub data: Vec, } + + pub use AddContactResponse as GetContactResponse; + + /* + #[derive(Debug, Default, Deserialize, Serialize, ToSchema)] + pub struct GetContactResponse { + pub message: String, + pub data: Vec, + } + */ } pub mod endpoint { @@ -120,4 +136,63 @@ pub mod endpoint { (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } + + // Endpoint to get songs + #[utoipa::path( + get, + path = super::super::endpoints::GET_CONTACT, + params( + ("id" = uuid::Uuid, Path, description = "Id of Contact"), + ("user_id" = uuid::Uuid, Path, description = "User Id associated with the Contact") + ), + responses( + (status = 200, description = "Songs found", body = super::response::GetContactResponse), + (status = 400, description = "Error getting songs", body = super::response::GetContactResponse) + ) + )] + pub async fn get_contacts( + axum::Extension(pool): axum::Extension, + axum::extract::Query(params): axum::extract::Query, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::GetContactResponse::default(); + + let contacts = match params.id { + Some(id) => match contact_repo::get(&pool, &id).await { + Ok(contact) => { + vec![contact] + } + Err(err) => { + eprintln!("Error: {err:?}"); + return ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ); + } + }, + None => match params.user_id { + Some(user_id) => match contact_repo::get_with_user_id(&pool, &user_id).await { + Ok(contacts) => contacts, + Err(err) => { + eprintln!("Error: {err:?}"); + return ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ); + } + }, + None => { + response.message = String::from("Invalid parameter"); + return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)); + } + }, + }; + + response.data = contacts; + response.message = String::from(super::super::response::SUCCESSFUL); + + (axum::http::StatusCode::OK, axum::Json(response)) + } } diff --git a/src/caller/mod.rs b/src/caller/mod.rs index 0c3523e..d0911be 100644 --- a/src/caller/mod.rs +++ b/src/caller/mod.rs @@ -5,6 +5,8 @@ pub mod endpoints { /// Constant for adding Contact endpoint pub const ADD_CONTACT: &str = "/api/v1/contact/new"; + /// Constant for getting Contact endpoint + pub const GET_CONTACT: &str = "/api/v1/contact"; } pub mod response { diff --git a/src/config/mod.rs b/src/config/mod.rs index 1bde5a1..ab9f851 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -89,6 +89,12 @@ pub mod init { crate::auth::auth::, )), ) + .route( + crate::caller::endpoints::GET_CONTACT, + post(contact_endpoints::get_contacts).route_layer(axum::middleware::from_fn( + crate::auth::auth::, + )), + ) .layer(cors::configure_cors().await) } diff --git a/src/repo/mod.rs b/src/repo/mod.rs index fa0acf5..6bda712 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -119,4 +119,62 @@ pub mod contact { }, } } + + pub async fn get_with_user_id( + pool: &sqlx::PgPool, + user_id: &uuid::Uuid, + ) -> Result, sqlx::Error> { + match sqlx::query( + r#" + SELECT id, firstname, lastname, nickname, phone_number, user_id FROM "contacts" + WHERE + user_id = $1; + "#, + ) + .bind(user_id) + .fetch_all(pool) + .await + { + Ok(rows) => { + let mut contacts: Vec = Vec::new(); + for row in rows { + 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")?; + let contact = 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, + user_id: Some(user_id), + }; + contacts.push(contact); + } + Ok(contacts) + } + Err(_) => Err(sqlx::Error::RowNotFound), + } + } }