Moved contact code to its own module
This commit is contained in:
@@ -0,0 +1,123 @@
|
|||||||
|
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<String>,
|
||||||
|
pub lastname: Option<String>,
|
||||||
|
pub nickname: Option<String>,
|
||||||
|
pub user_id: uuid::Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AddContactRequest {
|
||||||
|
pub fn is_valid(&self) -> bool {
|
||||||
|
!self.phone_number.is_empty() || !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,
|
||||||
|
pub data: Vec<textsender_models::contact::Contact>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod endpoint {
|
||||||
|
// use axum::{Json, response::IntoResponse};
|
||||||
|
|
||||||
|
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<sqlx::PgPool>,
|
||||||
|
axum::Json(payload): axum::Json<super::request::AddContactRequest>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<super::response::AddContactResponse>,
|
||||||
|
) {
|
||||||
|
let mut response = super::response::AddContactResponse::default();
|
||||||
|
|
||||||
|
if payload.is_valid() {
|
||||||
|
match contact_repo::get_with_user_id_and_phone(
|
||||||
|
&pool,
|
||||||
|
&payload.user_id,
|
||||||
|
&payload.phone_number,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Already created");
|
||||||
|
response.message = String::from("Already created");
|
||||||
|
(axum::http::StatusCode::NOT_MODIFIED, axum::Json(response))
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
match err {
|
||||||
|
sqlx::Error::RowNotFound => {
|
||||||
|
println!("Good to create");
|
||||||
|
// Put code here
|
||||||
|
let mut contact = textsender_models::contact::Contact {
|
||||||
|
firstname: payload.firstname,
|
||||||
|
lastname: payload.lastname,
|
||||||
|
phone_number: payload.phone_number,
|
||||||
|
nickname: payload.nickname,
|
||||||
|
user_id: Some(payload.user_id),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
match contact_repo::insert(&pool, &contact).await {
|
||||||
|
Ok(id) => {
|
||||||
|
contact.id = Some(id);
|
||||||
|
response.message =
|
||||||
|
String::from(super::super::response::SUCCESSFUL);
|
||||||
|
response.data.push(contact);
|
||||||
|
|
||||||
|
(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),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
response.message = String::from("Something went wrong");
|
||||||
|
(
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-126
@@ -1,3 +1,5 @@
|
|||||||
|
pub mod contact;
|
||||||
|
|
||||||
pub mod endpoints {
|
pub mod endpoints {
|
||||||
pub const ROOT: &str = "/";
|
pub const ROOT: &str = "/";
|
||||||
|
|
||||||
@@ -13,129 +15,3 @@ pub mod response {
|
|||||||
pub async fn root() -> &'static str {
|
pub async fn root() -> &'static str {
|
||||||
"Hello, World!"
|
"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<String>,
|
|
||||||
pub lastname: Option<String>,
|
|
||||||
pub nickname: Option<String>,
|
|
||||||
pub user_id: uuid::Uuid,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AddContactRequest {
|
|
||||||
pub fn is_valid(&self) -> bool {
|
|
||||||
!self.phone_number.is_empty() || !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,
|
|
||||||
pub data: Vec<textsender_models::contact::Contact>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod endpoint {
|
|
||||||
// use axum::{Json, response::IntoResponse};
|
|
||||||
|
|
||||||
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<sqlx::PgPool>,
|
|
||||||
axum::Json(payload): axum::Json<super::request::AddContactRequest>,
|
|
||||||
) -> (
|
|
||||||
axum::http::StatusCode,
|
|
||||||
axum::Json<super::response::AddContactResponse>,
|
|
||||||
) {
|
|
||||||
let mut response = super::response::AddContactResponse::default();
|
|
||||||
|
|
||||||
if payload.is_valid() {
|
|
||||||
match contact_repo::get_with_user_id_and_phone(
|
|
||||||
&pool,
|
|
||||||
&payload.user_id,
|
|
||||||
&payload.phone_number,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => {
|
|
||||||
println!("Already created");
|
|
||||||
response.message = String::from("Already created");
|
|
||||||
(axum::http::StatusCode::NOT_MODIFIED, axum::Json(response))
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
match err {
|
|
||||||
sqlx::Error::RowNotFound => {
|
|
||||||
println!("Good to create");
|
|
||||||
// Put code here
|
|
||||||
let mut contact = textsender_models::contact::Contact {
|
|
||||||
firstname: payload.firstname,
|
|
||||||
lastname: payload.lastname,
|
|
||||||
phone_number: payload.phone_number,
|
|
||||||
nickname: payload.nickname,
|
|
||||||
user_id: Some(payload.user_id),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
match contact_repo::insert(&pool, &contact).await {
|
|
||||||
Ok(id) => {
|
|
||||||
contact.id = Some(id);
|
|
||||||
response.message =
|
|
||||||
String::from(super::super::response::SUCCESSFUL);
|
|
||||||
response.data.push(contact);
|
|
||||||
|
|
||||||
(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),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
eprintln!("Error: {err:?}");
|
|
||||||
response.message = String::from("Something went wrong");
|
|
||||||
(
|
|
||||||
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