The bulk (#11)
Rust Build / Rustfmt (push) Successful in 1m26s
Rust Build / Test Suite (push) Successful in 1m41s
Rust Build / Check (push) Successful in 2m19s
Rust Build / Clippy (push) Successful in 1m36s
textsender_api PR / Rustfmt (pull_request) Successful in 31s
Rust Build / build (push) Successful in 2m11s
textsender_api PR / Clippy (pull_request) Successful in 1m39s
textsender_api PR / Check (pull_request) Successful in 2m30s
Rust Build / Rustfmt (push) Successful in 1m26s
Rust Build / Test Suite (push) Successful in 1m41s
Rust Build / Check (push) Successful in 2m19s
Rust Build / Clippy (push) Successful in 1m36s
textsender_api PR / Rustfmt (pull_request) Successful in 31s
Rust Build / build (push) Successful in 2m11s
textsender_api PR / Clippy (pull_request) Successful in 1m39s
textsender_api PR / Check (pull_request) Successful in 2m30s
Reviewed-on: phoenix/textsender_api#11
This commit was merged in pull request #11.
This commit is contained in:
@@ -22,6 +22,24 @@ pub mod request {
|
||||
pub id: Option<uuid::Uuid>,
|
||||
pub user_id: Option<uuid::Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
||||
pub struct UpdateContactNamesRequest {
|
||||
pub id: uuid::Uuid,
|
||||
pub firstname: Option<String>,
|
||||
pub lastname: Option<String>,
|
||||
pub nickname: Option<String>,
|
||||
}
|
||||
|
||||
impl UpdateContactNamesRequest {
|
||||
pub fn is_valid(&self) -> bool {
|
||||
if self.id.is_nil() {
|
||||
false
|
||||
} else {
|
||||
self.firstname.is_some() || self.lastname.is_some() || self.nickname.is_some()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
@@ -36,6 +54,22 @@ pub mod response {
|
||||
|
||||
pub use AddContactResponse as GetContactResponse;
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct UpdateContactNamesResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<UpdateContactNameChange>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct UpdateContactNameChange {
|
||||
pub old_firstname: Option<String>,
|
||||
pub old_lastname: Option<String>,
|
||||
pub old_nickname: Option<String>,
|
||||
pub new_firstname: Option<String>,
|
||||
pub new_lastname: Option<String>,
|
||||
pub new_nickname: Option<String>,
|
||||
}
|
||||
|
||||
/*
|
||||
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
||||
pub struct GetContactResponse {
|
||||
@@ -195,4 +229,84 @@ pub mod endpoint {
|
||||
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
|
||||
/// Endpoint to update Contact
|
||||
#[utoipa::path(
|
||||
patch,
|
||||
path = super::super::endpoints::UPDATE_CONTACT_NAME,
|
||||
request_body(
|
||||
content = super::request::UpdateContactNamesRequest,
|
||||
description = "Data needed to update Contact names",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 201, description = "Contact created", body = super::response::UpdateContactNamesResponse),
|
||||
(status = 400, description = "Error", body = super::response::UpdateContactNamesResponse),
|
||||
(status = 500, description = "Error creating Contact", body = super::response::UpdateContactNamesResponse)
|
||||
)
|
||||
)]
|
||||
pub async fn update_contact_names(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<super::request::UpdateContactNamesRequest>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::UpdateContactNamesResponse>,
|
||||
) {
|
||||
let mut response = super::response::UpdateContactNamesResponse::default();
|
||||
|
||||
if payload.is_valid() {
|
||||
match contact_repo::get(&pool, &payload.id).await {
|
||||
Ok(contact) => {
|
||||
let old_firstname: Option<String> = contact.firstname.clone();
|
||||
let old_lastname: Option<String> = contact.lastname.clone();
|
||||
let old_nickname: Option<String> = contact.nickname.clone();
|
||||
let new_firstname = payload.firstname;
|
||||
let new_lastname = payload.lastname;
|
||||
let new_nickname = payload.nickname;
|
||||
match contact_repo::update_names(
|
||||
&pool,
|
||||
&payload.id,
|
||||
&new_firstname,
|
||||
&new_lastname,
|
||||
&new_nickname,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(()) => {
|
||||
response.message = String::from(super::super::response::SUCCESSFUL);
|
||||
let data = super::response::UpdateContactNameChange {
|
||||
old_firstname,
|
||||
old_lastname,
|
||||
old_nickname,
|
||||
new_firstname,
|
||||
new_lastname,
|
||||
new_nickname,
|
||||
};
|
||||
response.data = vec![data];
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Something went wrong");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
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