diff --git a/src/caller/contact.rs b/src/caller/contact.rs index 3b07568..6093df5 100644 --- a/src/caller/contact.rs +++ b/src/caller/contact.rs @@ -171,7 +171,7 @@ pub mod endpoint { } } - // Endpoint to get songs + /// Endpoint to get Contacts #[utoipa::path( get, path = super::super::endpoints::GET_CONTACT, diff --git a/src/caller/message.rs b/src/caller/message.rs index 05d8280..b262dea 100644 --- a/src/caller/message.rs +++ b/src/caller/message.rs @@ -13,6 +13,12 @@ pub mod request { !self.content.is_empty() || !self.user_id.is_nil() } } + + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct GetMessageParams { + pub id: Option, + pub user_id: Option, + } } pub mod response { @@ -24,6 +30,8 @@ pub mod response { pub message: String, pub data: Vec, } + + pub use AddMessageResponse as GetMessageResponse; } pub mod endpoint { @@ -81,4 +89,63 @@ pub mod endpoint { (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } + + /// Endpoint to get Messages + #[utoipa::path( + get, + path = super::super::endpoints::GET_MESSAGE, + params( + ("id" = uuid::Uuid, Path, description = "Id of Message"), + ("user_id" = uuid::Uuid, Path, description = "User Id associated with the Message") + ), + responses( + (status = 200, description = "Songs found", body = super::response::GetMessageResponse), + (status = 400, description = "Error getting songs", body = super::response::GetMessageResponse) + ) + )] + pub async fn get_messages( + axum::Extension(pool): axum::Extension, + axum::extract::Query(params): axum::extract::Query, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::GetMessageResponse::default(); + + let messages = match params.id { + Some(id) => match message_repo::get(&pool, &id).await { + Ok(message) => { + vec![message] + } + Err(err) => { + eprintln!("Error: {err:?}"); + return ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ); + } + }, + None => match params.user_id { + Some(user_id) => match message_repo::get_with_user_id(&pool, &user_id).await { + Ok(messages) => messages, + 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 = messages; + 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 85b102e..3a9e62a 100644 --- a/src/caller/mod.rs +++ b/src/caller/mod.rs @@ -12,6 +12,8 @@ pub mod endpoints { pub const UPDATE_CONTACT_NAME: &str = "/api/v1/contact/update"; /// Constant for adding message endpoint pub const ADD_MESSAGE: &str = "/api/v1/message/new"; + /// Constant for getting messages endpoint + pub const GET_MESSAGE: &str = "/api/v1/message"; } pub mod response {