Get message endpoint (#14)
Reviewed-on: phoenix/textsender_api#14
This commit was merged in pull request #14.
This commit is contained in:
@@ -171,7 +171,7 @@ pub mod endpoint {
|
||||
}
|
||||
}
|
||||
|
||||
// Endpoint to get songs
|
||||
/// Endpoint to get Contacts
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = super::super::endpoints::GET_CONTACT,
|
||||
|
||||
@@ -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<uuid::Uuid>,
|
||||
pub user_id: Option<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
@@ -24,6 +30,8 @@ pub mod response {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::message::Message>,
|
||||
}
|
||||
|
||||
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<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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user