Saving code

This commit is contained in:
2026-06-14 23:09:09 -04:00
parent 7de2c6c96a
commit b6cee8f39f
4 changed files with 141 additions and 0 deletions
+75
View File
@@ -16,6 +16,12 @@ pub mod request {
!self.phone_number.is_empty() || !self.user_id.is_nil() !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<uuid::Uuid>,
pub user_id: Option<uuid::Uuid>,
}
} }
pub mod response { pub mod response {
@@ -27,6 +33,16 @@ pub mod response {
pub message: String, pub message: String,
pub data: Vec<textsender_models::contact::Contact>, pub data: Vec<textsender_models::contact::Contact>,
} }
pub use AddContactResponse as GetContactResponse;
/*
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
pub struct GetContactResponse {
pub message: String,
pub data: Vec<textsender_models::contact::Contact>,
}
*/
} }
pub mod endpoint { pub mod endpoint {
@@ -120,4 +136,63 @@ pub mod endpoint {
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (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<sqlx::PgPool>,
axum::extract::Query(params): axum::extract::Query<super::request::GetContactParams>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::GetContactResponse>,
) {
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))
}
} }
+2
View File
@@ -5,6 +5,8 @@ pub mod endpoints {
/// Constant for adding Contact endpoint /// Constant for adding Contact endpoint
pub const ADD_CONTACT: &str = "/api/v1/contact/new"; 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 { pub mod response {
+6
View File
@@ -89,6 +89,12 @@ pub mod init {
crate::auth::auth::<axum::body::Body>, crate::auth::auth::<axum::body::Body>,
)), )),
) )
.route(
crate::caller::endpoints::GET_CONTACT,
post(contact_endpoints::get_contacts).route_layer(axum::middleware::from_fn(
crate::auth::auth::<axum::body::Body>,
)),
)
.layer(cors::configure_cors().await) .layer(cors::configure_cors().await)
} }
+58
View File
@@ -119,4 +119,62 @@ pub mod contact {
}, },
} }
} }
pub async fn get_with_user_id(
pool: &sqlx::PgPool,
user_id: &uuid::Uuid,
) -> Result<Vec<textsender_models::contact::Contact>, 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<textsender_models::contact::Contact> = 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),
}
}
} }