pub mod endpoints { pub const ROOT: &str = "/"; /// Constant for adding Contact endpoint pub const ADD_CONTACT: &str = "/api/v1/contact/new"; } pub mod response { pub const SUCCESSFUL: &str = "SUCCESSFUL"; } /// Basic handler that responds with a static string pub async fn root() -> &'static str { "Hello, World!" } pub mod contact { pub mod request { use serde::{Deserialize, Serialize}; use utoipa::ToSchema; #[derive(Debug, Deserialize, Serialize, ToSchema)] pub struct AddContactRequest { pub phone_number: String, pub firstname: Option, pub lastname: Option, pub nickname: Option, pub user_id: uuid::Uuid, } impl AddContactRequest { pub fn is_valid(&self) -> bool { self.phone_number.is_empty() || self.firstname.is_none() || self.lastname.is_none() || self.user_id.is_nil() } } } pub mod response { use serde::{Deserialize, Serialize}; use utoipa::ToSchema; #[derive(Debug, Default, Deserialize, Serialize, ToSchema)] pub struct AddContactResponse { pub message: String, // TODO: Should be this. Update code in textsender_models // pub data: Vec, pub data: Vec, } } pub mod endpoint { // use axum::{Json, response::IntoResponse}; // use crate::repo; // use crate::repo::contact as contact_repo; /// Endpoint to create Contact #[utoipa::path( post, path = super::super::endpoints::ADD_CONTACT, request_body( content = super::request::AddContactRequest, description = "Data needed to create a Contact", content_type = "application/json" ), responses( (status = 201, description = "Contact created", body = super::response::AddContactResponse), (status = 400, description = "Error", body = super::response::AddContactResponse), (status = 500, description = "Error creating Contact", body = super::response::AddContactResponse) ) )] pub async fn create_contact( axum::Extension(pool): axum::Extension, axum::Json(payload): axum::Json, ) -> ( axum::http::StatusCode, axum::Json, ) { let mut response = super::response::AddContactResponse::default(); if payload.is_valid() { let contact = textsender_models::contact::Contact { firstname: payload.firstname, lastname: payload.lastname, phone_number: payload.phone_number, user_id: Some(payload.user_id), ..Default::default() }; match crate::repo::contact::insert(&pool, &contact).await { Ok(_) => ( axum::http::StatusCode::NOT_IMPLEMENTED, 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)) } } } }