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