Add message endpoint #13
+9
-10
@@ -5,7 +5,7 @@ pub mod request {
|
||||
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
||||
pub struct AddMessageRequest {
|
||||
pub content: String,
|
||||
pub user_id: uuid::Uuid
|
||||
pub user_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
impl AddMessageRequest {
|
||||
@@ -22,7 +22,7 @@ pub mod response {
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct AddMessageResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::message::Message>
|
||||
pub data: Vec<textsender_models::message::Message>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,8 +59,7 @@ pub mod endpoint {
|
||||
user_id: Some(payload.user_id),
|
||||
..Default::default()
|
||||
};
|
||||
match message_repo::insert(&pool, &msg, &payload.user_id) .await
|
||||
{
|
||||
match message_repo::insert(&pool, &msg, &payload.user_id).await {
|
||||
Ok(message_id) => {
|
||||
msg.id = Some(message_id);
|
||||
response.data.push(msg);
|
||||
@@ -68,13 +67,13 @@ pub mod endpoint {
|
||||
(axum::http::StatusCode::CREATED, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Contact not created");
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Contact not created");
|
||||
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
+6
-6
@@ -17,8 +17,8 @@ pub mod init {
|
||||
use crate::caller::contact as contact_caller;
|
||||
use crate::caller::message as message_caller;
|
||||
use contact_caller::endpoint as contact_endpoints;
|
||||
use message_caller::endpoint as message_endpoints;
|
||||
use contact_caller::response as contact_responses;
|
||||
use message_caller::endpoint as message_endpoints;
|
||||
use message_caller::response as message_responses;
|
||||
|
||||
mod cors {
|
||||
@@ -76,9 +76,9 @@ pub mod init {
|
||||
|
||||
#[derive(utoipa::OpenApi)]
|
||||
#[openapi(
|
||||
paths(contact_endpoints::create_contact,
|
||||
paths(contact_endpoints::create_contact,
|
||||
message_endpoints::create_message),
|
||||
components(schemas(contact_responses::AddContactResponse)),
|
||||
components(schemas(contact_responses::AddContactResponse, message_responses::AddMessageResponse)),
|
||||
tags(
|
||||
(name = "textsender API", description = "Web API to manage texting")
|
||||
)
|
||||
@@ -107,9 +107,9 @@ pub mod init {
|
||||
)
|
||||
.route(
|
||||
crate::caller::endpoints::ADD_MESSAGE,
|
||||
patch(message_endpoints::create_message).route_layer(
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
post(message_endpoints::create_message).route_layer(axum::middleware::from_fn(
|
||||
crate::auth::auth::<axum::body::Body>,
|
||||
)),
|
||||
)
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
+1
-2
@@ -7,7 +7,7 @@ pub async fn insert(
|
||||
) -> Result<uuid::Uuid, sqlx::Error> {
|
||||
match sqlx::query(
|
||||
r#"
|
||||
INSERT INTO "contacts" (content, user_id)
|
||||
INSERT INTO "messages" (content, user_id)
|
||||
VALUES($1, $2) RETURNING id;
|
||||
"#,
|
||||
)
|
||||
@@ -23,4 +23,3 @@ pub async fn insert(
|
||||
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user