Adding code to get messages endpoint

This commit is contained in:
2026-06-17 14:44:04 -04:00
parent cd61e67dd6
commit 24dca570a5
3 changed files with 70 additions and 1 deletions
+1 -1
View File
@@ -171,7 +171,7 @@ pub mod endpoint {
} }
} }
// Endpoint to get songs /// Endpoint to get Contacts
#[utoipa::path( #[utoipa::path(
get, get,
path = super::super::endpoints::GET_CONTACT, path = super::super::endpoints::GET_CONTACT,
+67
View File
@@ -13,6 +13,12 @@ pub mod request {
!self.content.is_empty() || !self.user_id.is_nil() !self.content.is_empty() || !self.user_id.is_nil()
} }
} }
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
pub struct GetMessageParams {
pub id: Option<uuid::Uuid>,
pub user_id: Option<uuid::Uuid>,
}
} }
pub mod response { pub mod response {
@@ -24,6 +30,8 @@ pub mod response {
pub message: String, pub message: String,
pub data: Vec<textsender_models::message::Message>, pub data: Vec<textsender_models::message::Message>,
} }
pub use AddMessageResponse as GetMessageResponse;
} }
pub mod endpoint { pub mod endpoint {
@@ -81,4 +89,63 @@ pub mod endpoint {
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (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<sqlx::PgPool>,
axum::extract::Query(params): axum::extract::Query<super::request::GetMessageParams>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::GetMessageResponse>,
) {
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))
}
} }
+2
View File
@@ -12,6 +12,8 @@ pub mod endpoints {
pub const UPDATE_CONTACT_NAME: &str = "/api/v1/contact/update"; pub const UPDATE_CONTACT_NAME: &str = "/api/v1/contact/update";
/// Constant for adding message endpoint /// Constant for adding message endpoint
pub const ADD_MESSAGE: &str = "/api/v1/message/new"; 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 { pub mod response {