Adding code
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Test Suite (pull_request) Successful in 1m10s
Rust Build / Clippy (pull_request) Successful in 1m42s
Rust Build / Check (pull_request) Successful in 2m33s
Rust Build / build (pull_request) Successful in 3m20s

This commit is contained in:
2026-06-13 15:51:56 -04:00
parent 567853134c
commit 39697a82e1
3 changed files with 36 additions and 23 deletions
+30 -17
View File
@@ -22,23 +22,18 @@ pub mod contact {
#[derive(Debug, Deserialize, Serialize, ToSchema)] #[derive(Debug, Deserialize, Serialize, ToSchema)]
pub struct AddContactRequest { pub struct AddContactRequest {
pub phone_number: String, pub phone_number: String,
pub first_name: String, pub firstname: Option<String>,
pub last_name: String, pub lastname: Option<String>,
pub nickname: Option<String>, pub nickname: Option<String>,
pub user_id: uuid::Uuid, pub user_id: uuid::Uuid,
} }
impl AddContactRequest { impl AddContactRequest {
pub fn is_valid(&self) -> bool { pub fn is_valid(&self) -> bool {
if self.phone_number.is_empty() self.phone_number.is_empty()
|| self.first_name.is_empty() || self.firstname.is_none()
|| self.last_name.is_empty() || self.lastname.is_none()
|| self.user_id.is_nil() || self.user_id.is_nil()
{
false
} else {
true
}
} }
} }
} }
@@ -57,10 +52,10 @@ pub mod contact {
} }
pub mod endpoint { pub mod endpoint {
use axum::{Json, response::IntoResponse}; // use axum::{Json, response::IntoResponse};
use crate::repo; // use crate::repo;
use crate::repo::contact as contact_repo; // use crate::repo::contact as contact_repo;
/// Endpoint to create Contact /// Endpoint to create Contact
#[utoipa::path( #[utoipa::path(
@@ -87,10 +82,28 @@ pub mod contact {
let mut response = super::response::AddContactResponse::default(); let mut response = super::response::AddContactResponse::default();
if payload.is_valid() { if payload.is_valid() {
( let contact = textsender_models::contact::Contact {
axum::http::StatusCode::NOT_IMPLEMENTED, firstname: payload.firstname,
axum::Json(response), 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 { } else {
response.message = String::from("Request body is not valid"); response.message = String::from("Request body is not valid");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
+5 -5
View File
@@ -10,13 +10,13 @@ pub mod host {
pub mod init { pub mod init {
use std::time::Duration; use std::time::Duration;
use axum::routing::{delete, get, patch, post}; use axum::routing::post;
use tower_http::timeout::TimeoutLayer; use tower_http::timeout::TimeoutLayer;
use utoipa::OpenApi; use utoipa::OpenApi;
use crate::caller::contact as contact_caller; use crate::caller::contact as contact_caller;
use contact_caller::endpoint as contact_endpoints; use contact_caller::endpoint as contact_endpoints;
use contact_caller::response as contact_responses; // use contact_caller::response as contact_responses;
mod cors { mod cors {
pub async fn configure_cors() -> tower_http::cors::CorsLayer { pub async fn configure_cors() -> tower_http::cors::CorsLayer {
@@ -57,8 +57,8 @@ pub mod init {
_ => { _ => {
// Development (default): Allow localhost origins // Development (default): Allow localhost origins
cors.allow_origin(vec![ cors.allow_origin(vec![
"http://localhost:8000".parse().unwrap(), "http://localhost:9081".parse().unwrap(),
"http://127.0.0.1:8000".parse().unwrap(), "http://127.0.0.1:9081".parse().unwrap(),
"http://localhost:4200".parse().unwrap(), "http://localhost:4200".parse().unwrap(),
"http://127.0.0.1:4200".parse().unwrap(), "http://127.0.0.1:4200".parse().unwrap(),
]) ])
@@ -74,7 +74,7 @@ pub mod init {
tags( tags(
(name = "textsender API", description = "Web API to manage texting") (name = "textsender API", description = "Web API to manage texting")
) )
)] )]
struct ApiDoc; struct ApiDoc;
pub async fn routes() -> axum::Router { pub async fn routes() -> axum::Router {
+1 -1
View File
@@ -13,7 +13,7 @@ pub mod contact {
.bind(&contact.lastname) .bind(&contact.lastname)
.bind(&contact.nickname) .bind(&contact.nickname)
.bind(&contact.phone_number) .bind(&contact.phone_number)
.bind(&contact.user_id) .bind(contact.user_id)
.execute(pool) .execute(pool)
.await; .await;