Hardned add contact endpoint
This commit is contained in:
+51
-20
@@ -76,29 +76,60 @@ pub mod contact {
|
||||
let mut response = super::response::AddContactResponse::default();
|
||||
|
||||
if payload.is_valid() {
|
||||
let mut 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 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))
|
||||
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) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Contact not created");
|
||||
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::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
(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 {
|
||||
|
||||
@@ -26,4 +26,97 @@ pub mod contact {
|
||||
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get(
|
||||
pool: &sqlx::PgPool,
|
||||
id: &uuid::Uuid,
|
||||
) -> Result<textsender_models::contact::Contact, sqlx::Error> {
|
||||
match sqlx::query(
|
||||
r#"
|
||||
SELECT id, firstname, lastname, nickname, phone_number, user_id FROM "contacts"
|
||||
WHERE
|
||||
id = $1;
|
||||
"#,
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
Ok(row) => {
|
||||
let id: uuid::Uuid = row.try_get("id")?;
|
||||
|
||||
let phone_number: String = row.try_get("phone_number")?;
|
||||
let user_id: uuid::Uuid = row.try_get("user_id")?;
|
||||
Ok(textsender_models::contact::Contact {
|
||||
id: Some(id),
|
||||
firstname: match row.try_get("firstname") {
|
||||
Ok(val) => val,
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
None
|
||||
}
|
||||
},
|
||||
lastname: match row.try_get("lastname") {
|
||||
Ok(val) => val,
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
None
|
||||
}
|
||||
},
|
||||
nickname: match row.try_get("nickname") {
|
||||
Ok(val) => val,
|
||||
Err(err) => {
|
||||
eprintln!("Error: nickname has no value for contacts record {err:?}");
|
||||
None
|
||||
}
|
||||
},
|
||||
phone_number: phone_number,
|
||||
user_id: Some(user_id),
|
||||
})
|
||||
}
|
||||
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_with_user_id_and_phone(
|
||||
pool: &sqlx::PgPool,
|
||||
user_id: &uuid::Uuid,
|
||||
phone_number: &str,
|
||||
) -> Result<textsender_models::contact::Contact, sqlx::Error> {
|
||||
match sqlx::query(
|
||||
r#"
|
||||
SELECT id, firstname, lastname, nickname, phone_number, user_id FROM "contacts"
|
||||
WHERE
|
||||
phone_number = $1 AND user_id = $2;
|
||||
"#,
|
||||
)
|
||||
.bind(phone_number)
|
||||
.bind(user_id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
Ok(row) => {
|
||||
println!("In here");
|
||||
let id: uuid::Uuid = row.try_get("id")?;
|
||||
let firstname: String = row.try_get("firstname")?;
|
||||
let lastname: String = row.try_get("lastname")?;
|
||||
let nickname: String = row.try_get("nickname")?;
|
||||
let phone_number: String = row.try_get("phone_number")?;
|
||||
let user_id: uuid::Uuid = row.try_get("user_id")?;
|
||||
println!("Found");
|
||||
Ok(textsender_models::contact::Contact {
|
||||
id: Some(id),
|
||||
firstname: Some(firstname),
|
||||
lastname: Some(lastname),
|
||||
nickname: Some(nickname),
|
||||
phone_number: phone_number,
|
||||
user_id: Some(user_id),
|
||||
})
|
||||
}
|
||||
Err(err) => match err {
|
||||
sqlx::Error::RowNotFound => Err(err),
|
||||
_ => Err(sqlx::Error::WorkerCrashed),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user