Get schedule message endpoint #16
@@ -15,6 +15,12 @@ pub mod request {
|
||||
self.scheduled.is_some() || !self.status.is_empty() || !self.user_id.is_nil()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct GetScheduledMessageParams {
|
||||
pub id: Option<uuid::Uuid>,
|
||||
pub user_id: Option<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
@@ -26,6 +32,12 @@ pub mod response {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::message::scheduling::ScheduledMessage>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct GetScheduledMessageResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::message::scheduling::ScheduledMessage>
|
||||
}
|
||||
}
|
||||
|
||||
pub mod endpoint {
|
||||
@@ -97,6 +109,65 @@ pub mod endpoint {
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to get Scheduled Message
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = crate::caller::endpoints::GET_SCHEDULE_MESSAGE,
|
||||
params(
|
||||
("id" = uuid::Uuid, Path, description = "Id of Scheduled Message"),
|
||||
("user_id" = uuid::Uuid, Path, description = "User Id associated with the Scheduled Message")
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Scheduled Message found", body = super::response::GetScheduledMessageResponse),
|
||||
(status = 400, description = "Error getting Scheduled Message", body = super::response::GetScheduledMessageResponse)
|
||||
)
|
||||
)]
|
||||
pub async fn get_scheduled_message(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::extract::Query(params): axum::extract::Query<super::request::GetScheduledMessageParams>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::GetScheduledMessageResponse>,
|
||||
) {
|
||||
let mut response = super::response::GetScheduledMessageResponse::default();
|
||||
|
||||
let scheduled_messages = match params.id {
|
||||
Some(id) => match scheduling_repo::get(&pool, &id).await {
|
||||
Ok(scheduled_message) => {
|
||||
vec![scheduled_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 scheduling_repo::get_with_user_id(&pool, &user_id).await {
|
||||
Ok(scheduled_messages) => scheduled_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 = scheduled_messages;
|
||||
response.message = String::from(super::super::super::response::SUCCESSFUL);
|
||||
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
|
||||
fn can_schedule(scheduled_time: &Option<time::OffsetDateTime>) -> bool {
|
||||
scheduled_time
|
||||
.is_some_and(|t| t - time::OffsetDateTime::now_utc() >= time::Duration::minutes(10))
|
||||
|
||||
@@ -16,6 +16,8 @@ pub mod endpoints {
|
||||
pub const GET_MESSAGE: &str = "/api/v1/message";
|
||||
/// Constant for scheduling message endpoint
|
||||
pub const SCHEDULE_MESSAGE: &str = "/api/v1/schedule/message";
|
||||
/// Constant for getting scheduled message endpoint
|
||||
pub const GET_SCHEDULE_MESSAGE: &str = "/api/v1/schedule/message";
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
|
||||
Reference in New Issue
Block a user