pub mod event; pub mod scheduling; 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() } } #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] pub struct GetMessageParams { pub id: Option, pub user_id: Option, } } 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, } pub use AddMessageResponse as GetMessageResponse; } 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, axum::Json(payload): axum::Json, ) -> ( axum::http::StatusCode, axum::Json, ) { let mut response = super::response::AddMessageResponse::default(); if payload.is_valid() { let mut msg = schedtxt_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)) } } /// Endpoint to get Messages #[utoipa::path( get, path = super::super::endpoints::GET_MESSAGE, params( ("id" = uuid::Uuid, Path, description = "Id of Message"), ("user_id" = uuid::Uuid, Path, description = "User Id associated with the Message") ), responses( (status = 200, description = "Songs found", body = super::response::GetMessageResponse), (status = 400, description = "Error getting songs", body = super::response::GetMessageResponse) ) )] pub async fn get_messages( axum::Extension(pool): axum::Extension, axum::extract::Query(params): axum::extract::Query, ) -> ( axum::http::StatusCode, axum::Json, ) { let mut response = super::response::GetMessageResponse::default(); let messages = match params.id { Some(id) => match message_repo::get(&pool, &id).await { Ok(message) => { vec![message] } Err(err) => { eprintln!("Error: {err:?}"); return ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response), ); } }, None => match params.user_id { Some(user_id) => match message_repo::get_with_user_id(&pool, &user_id).await { Ok(messages) => messages, Err(err) => { eprintln!("Error: {err:?}"); return ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response), ); } }, None => { response.message = String::from("Invalid parameter"); return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)); } }, }; response.data = messages; response.message = String::from(super::super::response::SUCCESSFUL); (axum::http::StatusCode::OK, axum::Json(response)) } }