diff --git a/src/caller/message/event.rs b/src/caller/message/event.rs index 4341d07..3606847 100644 --- a/src/caller/message/event.rs +++ b/src/caller/message/event.rs @@ -25,6 +25,12 @@ pub mod request { || !self.message_id.is_nil() } } + + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct GetMessageEventResponseParams { + pub id: Option, + pub user_id: Option, + } } pub mod response { @@ -36,6 +42,8 @@ pub mod response { pub message: String, pub data: Vec, } + + pub use RecordMessageEventResponse as GetMERResponse; } pub mod endpoint { @@ -95,4 +103,63 @@ pub mod endpoint { (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } + + /// Endpoint to get Messages + #[utoipa::path( + get, + path = crate::caller::endpoints::GET_MESSAGE_EVENT_RESPONSE, + params( + ("id" = uuid::Uuid, Path, description = "Id of Message Event Response"), + ("user_id" = uuid::Uuid, Path, description = "User Id associated with the Message Event Response") + ), + responses( + (status = 200, description = "Message Event Response found", body = super::response::GetMERResponse), + (status = 400, description = "Error getting Message Event Response", body = super::response::GetMERResponse) + ) + )] + pub async fn get_record_message_event_response( + axum::Extension(pool): axum::Extension, + axum::extract::Query(params): axum::extract::Query, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::GetMERResponse::default(); + + let messages = match params.id { + Some(id) => match event_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 event_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::super::response::SUCCESSFUL); + + (axum::http::StatusCode::OK, axum::Json(response)) + } }