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
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:
+30
-17
@@ -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<String>,
|
||||
pub lastname: Option<String>,
|
||||
pub nickname: Option<String>,
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user