Making endpoint available
This commit is contained in:
@@ -104,6 +104,8 @@ pub mod response {
|
||||
pub struct ChangedStatus {
|
||||
pub old_status: String,
|
||||
}
|
||||
|
||||
pub use ScheduleMessageResponse as FetchScheduledMessageResponse;
|
||||
}
|
||||
|
||||
pub mod endpoint {
|
||||
@@ -243,6 +245,50 @@ pub mod endpoint {
|
||||
.is_some_and(|t| t - time::OffsetDateTime::now_utc() >= time::Duration::minutes(10))
|
||||
}
|
||||
|
||||
/// Endpoint to fetch Scheduled Message that is ready
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = crate::caller::endpoints::FETCH_SCHEDULED_MESSAGE,
|
||||
responses(
|
||||
(status = 200, description = "Scheduled Message found", body = super::response::FetchScheduledMessageResponse),
|
||||
(status = 400, description = "Error getting Scheduled Message", body = super::response::FetchScheduledMessageResponse)
|
||||
)
|
||||
)]
|
||||
pub async fn fetch_scheduled_message(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::FetchScheduledMessageResponse>,
|
||||
) {
|
||||
let mut response = super::response::FetchScheduledMessageResponse::default();
|
||||
|
||||
match scheduling_repo::fetch(&pool).await {
|
||||
Ok(scheduled_message) => {
|
||||
response.data.push(scheduled_message);
|
||||
response.message = String::from(super::super::super::response::SUCCESSFUL);
|
||||
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
match err {
|
||||
sqlx::Error::RowNotFound => {
|
||||
response.message = String::from("Nothing to fetch");
|
||||
|
||||
(axum::http::StatusCode::NO_CONTENT, axum::Json(response))
|
||||
}
|
||||
_ => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Error");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to update status of Scheduled Message
|
||||
#[utoipa::path(
|
||||
patch,
|
||||
|
||||
@@ -160,6 +160,12 @@ pub mod init {
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
crate::caller::endpoints::FETCH_SCHEDULED_MESSAGE,
|
||||
get(scheduling_endpoints::fetch_scheduled_message).route_layer(
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,40 @@ pub async fn get_with_user_id(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn fetch(
|
||||
pool: &sqlx::PgPool,
|
||||
) -> Result<textsender_models::message::scheduling::ScheduledMessage, sqlx::Error> {
|
||||
match sqlx::query(
|
||||
r#"
|
||||
UPDATE "scheduled_messages"
|
||||
SET status = $1
|
||||
WHERE id = (
|
||||
SELECT id FROM "scheduled_messages"
|
||||
WHERE status = $2
|
||||
ORDER BY id
|
||||
FOR UPDATE SKIP LOCKED
|
||||
LIMIT 1
|
||||
)
|
||||
RETURNING id, scheduled, created, status, user_id
|
||||
"#,
|
||||
)
|
||||
.bind(textsender_models::message::scheduling::PROCESSING)
|
||||
.bind(textsender_models::message::scheduling::READY)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
Ok(row) => {
|
||||
match parse_row(&row).await {
|
||||
Ok(scheduled_message) => {
|
||||
Ok(scheduled_message)
|
||||
}
|
||||
Err(err) => Err(err)
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_status(
|
||||
pool: &sqlx::PgPool,
|
||||
id: &uuid::Uuid,
|
||||
|
||||
Reference in New Issue
Block a user