tsk-16: Change response of Register endpoint #17
+30
-24
@@ -29,7 +29,7 @@ pub mod response {
|
|||||||
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
|
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<simodels::user::User>,
|
pub data: Vec<simodels::register_result::RegisterResult>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ pub async fn register_user(
|
|||||||
return (
|
return (
|
||||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Json(response::Response {
|
Json(response::Response {
|
||||||
message: String::from("Registration check failed"),
|
message: "Registration check failed".to_string(),
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@@ -67,19 +67,7 @@ pub async fn register_user(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if registration_enabled {
|
if registration_enabled {
|
||||||
let mut user = simodels::user::User {
|
match repo::user::exists(&pool, &payload.username).await {
|
||||||
username: payload.username.clone(),
|
|
||||||
password: payload.password.clone(),
|
|
||||||
email: payload.email.clone(),
|
|
||||||
phone: payload.phone.clone(),
|
|
||||||
firstname: payload.firstname.clone(),
|
|
||||||
lastname: payload.lastname.clone(),
|
|
||||||
status: String::from("Active"),
|
|
||||||
email_verified: true,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
match repo::user::exists(&pool, &user.username).await {
|
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
if res {
|
if res {
|
||||||
(
|
(
|
||||||
@@ -95,20 +83,38 @@ pub async fn register_user(
|
|||||||
let generated_salt = salt_string;
|
let generated_salt = salt_string;
|
||||||
salt.salt = generated_salt.to_string().clone();
|
salt.salt = generated_salt.to_string().clone();
|
||||||
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
||||||
user.salt_id = salt.id;
|
|
||||||
let hashed_password =
|
let hashed_password =
|
||||||
hashing::hash_password(&user.password, &generated_salt).unwrap();
|
hashing::hash_password(&payload.password, &generated_salt).unwrap();
|
||||||
user.password = hashed_password;
|
let user = simodels::user::User {
|
||||||
|
username: payload.username,
|
||||||
|
password: hashed_password,
|
||||||
|
email: payload.email,
|
||||||
|
phone: payload.phone,
|
||||||
|
firstname: payload.firstname,
|
||||||
|
lastname: payload.lastname,
|
||||||
|
status: "Active".to_string(),
|
||||||
|
salt_id: salt.id,
|
||||||
|
email_verified: true,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
match repo::user::insert(&pool, &user).await {
|
match repo::user::insert(&pool, &user).await {
|
||||||
Ok((id, date_created)) => {
|
Ok((id, date_created)) => {
|
||||||
user.id = id;
|
let user_id = id;
|
||||||
user.date_created = date_created;
|
let date_created = date_created;
|
||||||
|
let register_user = simodels::register_result::RegisterResult {
|
||||||
|
id: user_id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
firstname: user.firstname,
|
||||||
|
lastname: user.lastname,
|
||||||
|
date_created,
|
||||||
|
};
|
||||||
(
|
(
|
||||||
StatusCode::CREATED,
|
StatusCode::CREATED,
|
||||||
Json(response::Response {
|
Json(response::Response {
|
||||||
message: String::from("User created"),
|
message: "User created".to_string(),
|
||||||
data: vec![user],
|
data: vec![register_user],
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -116,7 +122,7 @@ pub async fn register_user(
|
|||||||
StatusCode::BAD_REQUEST,
|
StatusCode::BAD_REQUEST,
|
||||||
Json(response::Response {
|
Json(response::Response {
|
||||||
message: err.to_string(),
|
message: err.to_string(),
|
||||||
data: vec![user],
|
data: vec![],
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
@@ -126,7 +132,7 @@ pub async fn register_user(
|
|||||||
StatusCode::BAD_REQUEST,
|
StatusCode::BAD_REQUEST,
|
||||||
Json(response::Response {
|
Json(response::Response {
|
||||||
message: err.to_string(),
|
message: err.to_string(),
|
||||||
data: vec![user],
|
data: vec![],
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user