Adding code for adding message
This commit is contained in:
@@ -0,0 +1,85 @@
|
|||||||
|
pub mod request {
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
||||||
|
pub struct AddMessageRequest {
|
||||||
|
pub content: String,
|
||||||
|
pub user_id: uuid::Uuid
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AddMessageRequest {
|
||||||
|
pub fn is_valid(&self) -> bool {
|
||||||
|
!self.content.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 AddMessageResponse {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<textsender_models::message::Message>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod endpoint {
|
||||||
|
use crate::repo::message as message_repo;
|
||||||
|
|
||||||
|
/// Endpoint to create Message
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::super::endpoints::ADD_MESSAGE,
|
||||||
|
request_body(
|
||||||
|
content = super::request::AddMessageRequest,
|
||||||
|
description = "Data needed to create a Message",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "Message created", body = super::response::AddMessageResponse),
|
||||||
|
(status = 400, description = "Error", body = super::response::AddMessageResponse),
|
||||||
|
(status = 500, description = "Error creating Message", body = super::response::AddMessageResponse)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub async fn create_message(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::Json(payload): axum::Json<super::request::AddMessageRequest>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<super::response::AddMessageResponse>,
|
||||||
|
) {
|
||||||
|
let mut response = super::response::AddMessageResponse::default();
|
||||||
|
|
||||||
|
if payload.is_valid() {
|
||||||
|
let mut msg = textsender_models::message::Message {
|
||||||
|
content: payload.content,
|
||||||
|
user_id: Some(payload.user_id),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
match message_repo::insert(&pool, &msg, &payload.user_id) .await
|
||||||
|
{
|
||||||
|
Ok(message_id) => {
|
||||||
|
msg.id = Some(message_id);
|
||||||
|
response.data.push(msg);
|
||||||
|
response.message = String::from("Message created");
|
||||||
|
(axum::http::StatusCode::CREATED, axum::Json(response))
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
response.message = String::from("Contact not created");
|
||||||
|
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
response.message = String::from("Request body is not valid");
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
pub mod contact;
|
pub mod contact;
|
||||||
|
pub mod message;
|
||||||
|
|
||||||
pub mod endpoints {
|
pub mod endpoints {
|
||||||
pub const ROOT: &str = "/";
|
pub const ROOT: &str = "/";
|
||||||
@@ -9,6 +10,8 @@ pub mod endpoints {
|
|||||||
pub const GET_CONTACT: &str = "/api/v1/contact";
|
pub const GET_CONTACT: &str = "/api/v1/contact";
|
||||||
/// Constant for updating names of a Contact endpoint
|
/// Constant for updating names of a Contact endpoint
|
||||||
pub const UPDATE_CONTACT_NAME: &str = "/api/v1/contact/update";
|
pub const UPDATE_CONTACT_NAME: &str = "/api/v1/contact/update";
|
||||||
|
/// Constant for adding message endpoint
|
||||||
|
pub const ADD_MESSAGE: &str = "/api/v1/message/new";
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
|
|||||||
+13
-3
@@ -15,8 +15,11 @@ pub mod init {
|
|||||||
use utoipa::OpenApi;
|
use utoipa::OpenApi;
|
||||||
|
|
||||||
use crate::caller::contact as contact_caller;
|
use crate::caller::contact as contact_caller;
|
||||||
|
use crate::caller::message as message_caller;
|
||||||
use contact_caller::endpoint as contact_endpoints;
|
use contact_caller::endpoint as contact_endpoints;
|
||||||
// use contact_caller::response as contact_responses;
|
use message_caller::endpoint as message_endpoints;
|
||||||
|
use contact_caller::response as contact_responses;
|
||||||
|
use message_caller::response as message_responses;
|
||||||
|
|
||||||
mod cors {
|
mod cors {
|
||||||
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
|
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
|
||||||
@@ -73,8 +76,9 @@ pub mod init {
|
|||||||
|
|
||||||
#[derive(utoipa::OpenApi)]
|
#[derive(utoipa::OpenApi)]
|
||||||
#[openapi(
|
#[openapi(
|
||||||
paths(contact_endpoints::create_contact,),
|
paths(contact_endpoints::create_contact,
|
||||||
components(schemas(contact_caller::response::AddContactResponse)),
|
message_endpoints::create_message),
|
||||||
|
components(schemas(contact_responses::AddContactResponse)),
|
||||||
tags(
|
tags(
|
||||||
(name = "textsender API", description = "Web API to manage texting")
|
(name = "textsender API", description = "Web API to manage texting")
|
||||||
)
|
)
|
||||||
@@ -101,6 +105,12 @@ pub mod init {
|
|||||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
crate::caller::endpoints::ADD_MESSAGE,
|
||||||
|
patch(message_endpoints::create_message).route_layer(
|
||||||
|
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||||
|
),
|
||||||
|
)
|
||||||
.layer(cors::configure_cors().await)
|
.layer(cors::configure_cors().await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
use sqlx::Row;
|
||||||
|
|
||||||
|
pub async fn insert(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
message: &textsender_models::message::Message,
|
||||||
|
user_id: &uuid::Uuid,
|
||||||
|
) -> Result<uuid::Uuid, sqlx::Error> {
|
||||||
|
match sqlx::query(
|
||||||
|
r#"
|
||||||
|
INSERT INTO "contacts" (content, user_id)
|
||||||
|
VALUES($1, $2) RETURNING id;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(&message.content)
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(row) => {
|
||||||
|
let id: uuid::Uuid = row.try_get("id")?;
|
||||||
|
Ok(id)
|
||||||
|
}
|
||||||
|
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1 +1,2 @@
|
|||||||
pub mod contact;
|
pub mod contact;
|
||||||
|
pub mod message;
|
||||||
|
|||||||
Reference in New Issue
Block a user