Added doc gen for endpoint and made some improvements
This commit is contained in:
+74
-39
@@ -198,6 +198,21 @@ async fn is_registration_enabled() -> Result<bool, std::io::Error> {
|
||||
}
|
||||
|
||||
/// Endpoint to register a service user
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = super::endpoints::REGISTER_SERVICE_USER,
|
||||
request_body(
|
||||
content = request::RegisterServiceUserRequest,
|
||||
description = "Data required to register service user",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 201, description = "Service user created", body = response::RegisterServiceUserResponse),
|
||||
(status = 400, description = "Issue creating service user", body = response::RegisterServiceUserResponse),
|
||||
(status = 406, description = "Cannot create service user", body = response::RegisterServiceUserResponse),
|
||||
(status = 500, description = "Issue creating service user", body = response::RegisterServiceUserResponse),
|
||||
)
|
||||
)]
|
||||
pub async fn register_service_user(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
Json(payload): Json<request::RegisterServiceUserRequest>,
|
||||
@@ -209,56 +224,76 @@ pub async fn register_service_user(
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let registration_enabled = match is_registration_enabled().await {
|
||||
Ok(value) => value,
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
resp.message = String::from("Registration check failed");
|
||||
return (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(resp)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let (res, msg) = payload.is_empty();
|
||||
if res {
|
||||
resp.message = msg.unwrap();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(resp))
|
||||
} else {
|
||||
match repo::service::exists(&pool, &payload.username).await {
|
||||
Ok(exists) => {
|
||||
if exists {
|
||||
resp.message = String::from("Invalid");
|
||||
if registration_enabled {
|
||||
match repo::service::exists(&pool, &payload.username).await {
|
||||
Ok(exists) => {
|
||||
if exists {
|
||||
resp.message = String::from("Invalid");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(resp),
|
||||
)
|
||||
} else {
|
||||
let (generate_salt, mut salt) = generate_the_salt();
|
||||
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
||||
let mut service_user = textsender_models::user::ServiceUser {
|
||||
username: payload.username.clone(),
|
||||
passphrase: hashing::hash_password(&payload.username, &generate_salt)
|
||||
.unwrap(),
|
||||
salt_id: salt.id,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
println!("Creating user");
|
||||
|
||||
match repo::service::insert(&pool, &service_user).await {
|
||||
Ok(created) => {
|
||||
resp.message = String::from(super::messages::SUCCESSFUL_MESSAGE);
|
||||
service_user.created = Some(created);
|
||||
resp.data.push(service_user);
|
||||
(axum::http::StatusCode::CREATED, axum::Json(resp))
|
||||
}
|
||||
Err(err) => {
|
||||
resp.message = err.to_string();
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(resp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
resp.message = err.to_string();
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(resp),
|
||||
)
|
||||
} else {
|
||||
let (generate_salt, mut salt) = generate_the_salt();
|
||||
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
||||
let mut service_user = textsender_models::user::ServiceUser {
|
||||
username: payload.username.clone(),
|
||||
passphrase: hashing::hash_password(&payload.username, &generate_salt)
|
||||
.unwrap(),
|
||||
salt_id: salt.id,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
println!("Creating user");
|
||||
|
||||
match repo::service::insert(&pool, &service_user).await {
|
||||
Ok(created) => {
|
||||
resp.message = String::from(super::messages::SUCCESSFUL_MESSAGE);
|
||||
service_user.created = Some(created);
|
||||
resp.data.push(service_user);
|
||||
(axum::http::StatusCode::CREATED, axum::Json(resp))
|
||||
}
|
||||
Err(err) => {
|
||||
resp.message = err.to_string();
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(resp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
resp.message = err.to_string();
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(resp),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
resp.message = String::from("Registration is not enabled");
|
||||
(
|
||||
axum::http::StatusCode::NOT_ACCEPTABLE,
|
||||
Json(resp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user