Add message endpoint (#13)
Reviewed-on: phoenix/textsender_api#13
This commit was merged in pull request #13.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
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 message;
|
||||
|
||||
pub mod endpoints {
|
||||
pub const ROOT: &str = "/";
|
||||
@@ -9,6 +10,8 @@ pub mod endpoints {
|
||||
pub const GET_CONTACT: &str = "/api/v1/contact";
|
||||
/// Constant for updating names of a Contact endpoint
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user