Schedule message (#15)
Reviewed-on: phoenix/textsender_api#15
This commit was merged in pull request #15.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
pub mod scheduling;
|
||||
|
||||
pub mod request {
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
@@ -0,0 +1,104 @@
|
||||
pub mod request {
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
||||
pub struct ScheduleMessageRequest {
|
||||
#[serde(with = "time::serde::rfc3339::option")]
|
||||
pub scheduled: Option<time::OffsetDateTime>,
|
||||
pub status: String,
|
||||
pub user_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
impl ScheduleMessageRequest {
|
||||
pub fn is_valid(&self) -> bool {
|
||||
self.scheduled.is_some() || !self.status.is_empty() || !self.user_id.is_nil()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct ScheduleMessageResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::message::scheduling::ScheduledMessage>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod endpoint {
|
||||
use crate::repo::scheduling as scheduling_repo;
|
||||
|
||||
/// Endpoint to create a Scheudled Message
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = crate::caller::endpoints::SCHEDULE_MESSAGE,
|
||||
request_body(
|
||||
content = super::request::ScheduleMessageRequest,
|
||||
description = "Data needed to create a Scheduled Message",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 201, description = "Scheduled Message created", body = super::response::ScheduleMessageResponse),
|
||||
(status = 400, description = "Error", body = super::response::ScheduleMessageResponse),
|
||||
(status = 500, description = "Error creating Scheduled Message", body = super::response::ScheduleMessageResponse)
|
||||
)
|
||||
)]
|
||||
pub async fn schedule_message(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<super::request::ScheduleMessageRequest>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::ScheduleMessageResponse>,
|
||||
) {
|
||||
let mut response = super::response::ScheduleMessageResponse::default();
|
||||
|
||||
if payload.is_valid() {
|
||||
if payload.status != textsender_models::message::scheduling::PENDING {
|
||||
response.message = String::from("scheduled message must be pending");
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
} else {
|
||||
if can_schedule(&payload.scheduled) {
|
||||
let mut scheduled_message =
|
||||
textsender_models::message::scheduling::ScheduledMessage {
|
||||
scheduled: payload.scheduled,
|
||||
status: payload.status,
|
||||
..Default::default()
|
||||
};
|
||||
match scheduling_repo::insert(&pool, &scheduled_message, &payload.user_id).await
|
||||
{
|
||||
Ok((id, created)) => {
|
||||
scheduled_message.id = id;
|
||||
scheduled_message.created = Some(created);
|
||||
scheduled_message.user_id = payload.user_id;
|
||||
response.message = String::from("Message scheduled");
|
||||
response.data.push(scheduled_message);
|
||||
(axum::http::StatusCode::CREATED, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Error inserting");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
response.message = String::from("Cannot schedule message");
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
response.message = String::from("Request body is not valid");
|
||||
(axum::http::StatusCode::BAD_REQUEST, 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))
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,8 @@ pub mod endpoints {
|
||||
pub const ADD_MESSAGE: &str = "/api/v1/message/new";
|
||||
/// Constant for getting messages endpoint
|
||||
pub const GET_MESSAGE: &str = "/api/v1/message";
|
||||
/// Constant for scheduling message endpoint
|
||||
pub const SCHEDULE_MESSAGE: &str = "/api/v1/schedule/message";
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
|
||||
+15
-2
@@ -16,10 +16,14 @@ pub mod init {
|
||||
|
||||
use crate::caller::contact as contact_caller;
|
||||
use crate::caller::message as message_caller;
|
||||
use crate::caller::message::scheduling as scheduling_caller;
|
||||
|
||||
use contact_caller::endpoint as contact_endpoints;
|
||||
use contact_caller::response as contact_responses;
|
||||
use message_caller::endpoint as message_endpoints;
|
||||
use message_caller::response as message_responses;
|
||||
use scheduling_caller::endpoint as scheduling_endpoints;
|
||||
use scheduling_caller::response as scheduling_responses;
|
||||
|
||||
mod cors {
|
||||
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
|
||||
@@ -77,8 +81,11 @@ pub mod init {
|
||||
#[derive(utoipa::OpenApi)]
|
||||
#[openapi(
|
||||
paths(contact_endpoints::create_contact,
|
||||
message_endpoints::create_message),
|
||||
components(schemas(contact_responses::AddContactResponse, message_responses::AddMessageResponse)),
|
||||
message_endpoints::create_message,
|
||||
scheduling_endpoints::schedule_message,
|
||||
),
|
||||
components(schemas(contact_responses::AddContactResponse, message_responses::AddMessageResponse,
|
||||
scheduling_responses::ScheduleMessageResponse)),
|
||||
tags(
|
||||
(name = "textsender API", description = "Web API to manage texting")
|
||||
)
|
||||
@@ -117,6 +124,12 @@ pub mod init {
|
||||
crate::auth::auth::<axum::body::Body>,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
crate::caller::endpoints::SCHEDULE_MESSAGE,
|
||||
post(scheduling_endpoints::schedule_message).route_layer(
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod contact;
|
||||
pub mod message;
|
||||
pub mod scheduling;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
use sqlx::Row;
|
||||
|
||||
pub async fn insert(
|
||||
pool: &sqlx::PgPool,
|
||||
scheduled_message: &textsender_models::message::scheduling::ScheduledMessage,
|
||||
user_id: &uuid::Uuid,
|
||||
) -> Result<(uuid::Uuid, time::OffsetDateTime), sqlx::Error> {
|
||||
match sqlx::query(
|
||||
r#"
|
||||
INSERT INTO "scheduled_messages" (scheduled, status, user_id)
|
||||
VALUES($1, $2, $3) RETURNING id, created;
|
||||
"#,
|
||||
)
|
||||
.bind(scheduled_message.scheduled)
|
||||
.bind(&scheduled_message.status)
|
||||
.bind(user_id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
Ok(row) => {
|
||||
let id: uuid::Uuid = row.try_get("id")?;
|
||||
let created: time::OffsetDateTime = row.try_get("created")?;
|
||||
Ok((id, created))
|
||||
}
|
||||
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user