tsk-14: Update user profile #15
No files matched your search
+25
-11
@@ -51,7 +51,7 @@ pub mod request {
|
|||||||
pub struct UpdateUserProfileRequest {
|
pub struct UpdateUserProfileRequest {
|
||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
pub firstname: String,
|
pub firstname: String,
|
||||||
pub lastname: String
|
pub lastname: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UpdateUserProfileRequest {
|
impl UpdateUserProfileRequest {
|
||||||
@@ -115,7 +115,6 @@ pub mod response {
|
|||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<simodels::user::UserProfile>,
|
pub data: Vec<simodels::user::UserProfile>,
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,10 +488,17 @@ pub mod endpoint {
|
|||||||
(status = 500, description = "Failure", body = super::response::update_user_profile::UpdateUserProfileResponse)
|
(status = 500, description = "Failure", body = super::response::update_user_profile::UpdateUserProfileResponse)
|
||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
pub async fn update_user_profile(axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
pub async fn update_user_profile(
|
||||||
axum::Json(payload): axum::Json<super::request::update_user_profile::UpdateUserProfileRequest>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
) -> (axum::http::StatusCode, axum::Json<super::response::update_user_profile::UpdateUserProfileResponse>,) {
|
axum::Json(payload): axum::Json<
|
||||||
let mut response = super::response::update_user_profile::UpdateUserProfileResponse::default();
|
super::request::update_user_profile::UpdateUserProfileRequest,
|
||||||
|
>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<super::response::update_user_profile::UpdateUserProfileResponse>,
|
||||||
|
) {
|
||||||
|
let mut response =
|
||||||
|
super::response::update_user_profile::UpdateUserProfileResponse::default();
|
||||||
if !payload.is_valid() {
|
if !payload.is_valid() {
|
||||||
response.message = "Invalid".to_string();
|
response.message = "Invalid".to_string();
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
@@ -502,7 +508,9 @@ pub mod endpoint {
|
|||||||
Ok(user) => {
|
Ok(user) => {
|
||||||
let firstname = payload.firstname;
|
let firstname = payload.firstname;
|
||||||
let lastname = payload.lastname;
|
let lastname = payload.lastname;
|
||||||
match repo::user::update_user_profile(&pool, &user_id, &firstname, &lastname).await {
|
match repo::user::update_user_profile(&pool, &user_id, &firstname, &lastname)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let user_profile = simodels::user::UserProfile {
|
let user_profile = simodels::user::UserProfile {
|
||||||
firstname,
|
firstname,
|
||||||
@@ -513,18 +521,24 @@ pub mod endpoint {
|
|||||||
response.message = "SUCCESSFUL".to_string();
|
response.message = "SUCCESSFUL".to_string();
|
||||||
response.data.push(user_profile);
|
response.data.push(user_profile);
|
||||||
(axum::http::StatusCode::OK, axum::Json(response))
|
(axum::http::StatusCode::OK, axum::Json(response))
|
||||||
},
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
response.message = "ERROR - Updating user profile".to_string();
|
response.message = "ERROR - Updating user profile".to_string();
|
||||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
response.message = "ERROR".to_string();
|
response.message = "ERROR".to_string();
|
||||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-6
@@ -14,7 +14,6 @@ async fn main() {
|
|||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -144,7 +143,11 @@ mod tests {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_user_profile(user_id: &uuid::Uuid, firstname: &str, lastname: &str) -> serde_json::Value {
|
pub fn update_user_profile(
|
||||||
|
user_id: &uuid::Uuid,
|
||||||
|
firstname: &str,
|
||||||
|
lastname: &str,
|
||||||
|
) -> serde_json::Value {
|
||||||
json!({
|
json!({
|
||||||
"firstname": firstname,
|
"firstname": firstname,
|
||||||
"lastname": lastname,
|
"lastname": lastname,
|
||||||
@@ -154,8 +157,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod requests {
|
pub mod requests {
|
||||||
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
|
|
||||||
use soaricarus_auth::callers;
|
use soaricarus_auth::callers;
|
||||||
|
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
|
||||||
|
|
||||||
pub async fn register(
|
pub async fn register(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
@@ -746,7 +749,14 @@ mod tests {
|
|||||||
let updated_firstname = test_data::UPDATED_FIRSTNAME;
|
let updated_firstname = test_data::UPDATED_FIRSTNAME;
|
||||||
let updated_lastname = test_data::UPDATED_LASTNAME;
|
let updated_lastname = test_data::UPDATED_LASTNAME;
|
||||||
|
|
||||||
match requests::update_user_profile(&app, &user_id, &updated_firstname, &updated_lastname).await {
|
match requests::update_user_profile(
|
||||||
|
&app,
|
||||||
|
&user_id,
|
||||||
|
&updated_firstname,
|
||||||
|
&updated_lastname,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
assert_eq!(StatusCode::OK, resp.status(), "Status is not right");
|
assert_eq!(StatusCode::OK, resp.status(), "Status is not right");
|
||||||
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
|
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
|
||||||
@@ -757,8 +767,14 @@ mod tests {
|
|||||||
serde_json::from_slice(&body).unwrap();
|
serde_json::from_slice(&body).unwrap();
|
||||||
let user_profile = &parsed_body.data[0];
|
let user_profile = &parsed_body.data[0];
|
||||||
|
|
||||||
assert_eq!(updated_firstname, user_profile.firstname, "Firstname has not been updated");
|
assert_eq!(
|
||||||
assert_eq!(updated_lastname, user_profile.lastname, "Lastname has not been updated");
|
updated_firstname, user_profile.firstname,
|
||||||
|
"Firstname has not been updated"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
updated_lastname, user_profile.lastname,
|
||||||
|
"Lastname has not been updated"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
assert!(false, "Error: {:?}", err.to_string());
|
assert!(false, "Error: {:?}", err.to_string());
|
||||||
|
|||||||
Reference in new issue
Block a user