diff --git a/src/caller/mod.rs b/src/caller/mod.rs index 408cfc0..6374623 100644 --- a/src/caller/mod.rs +++ b/src/caller/mod.rs @@ -22,23 +22,18 @@ pub mod contact { #[derive(Debug, Deserialize, Serialize, ToSchema)] pub struct AddContactRequest { pub phone_number: String, - pub first_name: String, - pub last_name: String, + pub firstname: Option, + pub lastname: Option, pub nickname: Option, pub user_id: uuid::Uuid, } impl AddContactRequest { pub fn is_valid(&self) -> bool { - if self.phone_number.is_empty() - || self.first_name.is_empty() - || self.last_name.is_empty() + self.phone_number.is_empty() + || self.firstname.is_none() + || self.lastname.is_none() || self.user_id.is_nil() - { - false - } else { - true - } } } } @@ -57,10 +52,10 @@ pub mod contact { } pub mod endpoint { - use axum::{Json, response::IntoResponse}; + // use axum::{Json, response::IntoResponse}; - use crate::repo; - use crate::repo::contact as contact_repo; + // use crate::repo; + // use crate::repo::contact as contact_repo; /// Endpoint to create Contact #[utoipa::path( @@ -87,10 +82,28 @@ pub mod contact { let mut response = super::response::AddContactResponse::default(); if payload.is_valid() { - ( - axum::http::StatusCode::NOT_IMPLEMENTED, - axum::Json(response), - ) + 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)) diff --git a/src/config/mod.rs b/src/config/mod.rs index a24887e..b79f3a9 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -10,13 +10,13 @@ pub mod host { pub mod init { use std::time::Duration; - use axum::routing::{delete, get, patch, post}; + use axum::routing::post; use tower_http::timeout::TimeoutLayer; use utoipa::OpenApi; use crate::caller::contact as contact_caller; use contact_caller::endpoint as contact_endpoints; - use contact_caller::response as contact_responses; + // use contact_caller::response as contact_responses; mod cors { pub async fn configure_cors() -> tower_http::cors::CorsLayer { @@ -57,8 +57,8 @@ pub mod init { _ => { // Development (default): Allow localhost origins cors.allow_origin(vec![ - "http://localhost:8000".parse().unwrap(), - "http://127.0.0.1:8000".parse().unwrap(), + "http://localhost:9081".parse().unwrap(), + "http://127.0.0.1:9081".parse().unwrap(), "http://localhost:4200".parse().unwrap(), "http://127.0.0.1:4200".parse().unwrap(), ]) @@ -74,7 +74,7 @@ pub mod init { tags( (name = "textsender API", description = "Web API to manage texting") ) -)] + )] struct ApiDoc; pub async fn routes() -> axum::Router { diff --git a/src/repo/mod.rs b/src/repo/mod.rs index fab9efc..0700239 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -13,7 +13,7 @@ pub mod contact { .bind(&contact.lastname) .bind(&contact.nickname) .bind(&contact.phone_number) - .bind(&contact.user_id) + .bind(contact.user_id) .execute(pool) .await;