Added doc gen for endpoint and made some improvements
This commit is contained in:
@@ -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,11 +224,24 @@ 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 {
|
||||
if registration_enabled {
|
||||
match repo::service::exists(&pool, &payload.username).await {
|
||||
Ok(exists) => {
|
||||
if exists {
|
||||
@@ -260,5 +288,12 @@ pub async fn register_service_user(
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resp.message = String::from("Registration is not enabled");
|
||||
(
|
||||
axum::http::StatusCode::NOT_ACCEPTABLE,
|
||||
Json(resp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user