feat: more user functions exposed to routing

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-17 03:19:34 +03:00
parent 9509bd31b5
commit b3b93ab0cc
3 changed files with 423 additions and 70 deletions

View file

@ -35,6 +35,29 @@ pub fn route(State(app_state): State<AppState>) -> Router<AppState> {
.route("/", patch(update))
.route("/:id", delete(delete_))
.route("/", get(read_all))
.route("/names/:name", get(read_all_for_name))
.route("/surnames/:surname", get(read_all_for_surname))
.route("/birth_dates/:birth_date", get(read_all_for_birth_date))
.route("/roles/:role", get(read_all_for_role))
.route("/genders/:gender", get(read_all_for_gender))
.route("/ids", get(read_all_id))
.route("/ids/names/:name", get(read_all_id_for_name))
.route("/ids/surnames/:surname", get(read_all_id_for_surname))
.route(
"/ids/birth_dates/:birth_date",
get(read_all_id_for_birth_date),
)
.route("/ids/roles/:role", get(read_all_id_for_role))
.route("/ids/genders/:gender", get(read_all_id_for_gender))
.route("/count", get(count_all))
.route("/count/names/:name", get(count_all_for_name))
.route("/count/surnames/:surname", get(count_all_for_surname))
.route(
"/count/birth_dates/:birth_date",
get(count_all_for_birth_date),
)
.route("/count/roles/:role", get(count_all_for_role))
.route("/count/genders/:gender", get(count_all_for_gender))
.with_state(app_state)
}
@ -111,3 +134,218 @@ async fn read_all(State(app_state): State<AppState>) -> impl IntoResponse {
),
}
}
async fn read_all_for_name(
State(app_state): State<AppState>,
Path(name): Path<String>,
) -> impl IntoResponse {
match User::read_all_for_name(&name, &app_state.database_connection).await {
Ok(users) => (StatusCode::OK, Json(serde_json::json!(users))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_for_surname(
State(app_state): State<AppState>,
Path(surname): Path<String>,
) -> impl IntoResponse {
match User::read_all_for_surname(&surname, &app_state.database_connection).await {
Ok(users) => (StatusCode::OK, Json(serde_json::json!(users))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_for_birth_date(
State(app_state): State<AppState>,
Path(birth_date): Path<NaiveDate>,
) -> impl IntoResponse {
match User::read_all_for_birth_date(&birth_date, &app_state.database_connection).await {
Ok(users) => (StatusCode::OK, Json(serde_json::json!(users))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_for_role(
State(app_state): State<AppState>,
Path(role_id): Path<i64>,
) -> impl IntoResponse {
match User::read_all_for_role(&role_id, &app_state.database_connection).await {
Ok(users) => (StatusCode::OK, Json(serde_json::json!(users))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_for_gender(
State(app_state): State<AppState>,
Path(gender): Path<bool>,
) -> impl IntoResponse {
match User::read_all_for_gender(&gender, &app_state.database_connection).await {
Ok(users) => (StatusCode::OK, Json(serde_json::json!(users))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_id(State(app_state): State<AppState>) -> impl IntoResponse {
match User::read_all_id(&app_state.database_connection).await {
Ok(user_ids) => (StatusCode::OK, Json(serde_json::json!(user_ids))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_id_for_name(
State(app_state): State<AppState>,
Path(name): Path<String>,
) -> impl IntoResponse {
match User::read_all_id_for_name(&name, &app_state.database_connection).await {
Ok(user_ids) => (StatusCode::OK, Json(serde_json::json!(user_ids))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_id_for_surname(
State(app_state): State<AppState>,
Path(surname): Path<String>,
) -> impl IntoResponse {
match User::read_all_id_for_surname(&surname, &app_state.database_connection).await {
Ok(user_ids) => (StatusCode::OK, Json(serde_json::json!(user_ids))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_id_for_birth_date(
State(app_state): State<AppState>,
Path(birth_date): Path<NaiveDate>,
) -> impl IntoResponse {
match User::read_all_id_for_birth_date(&birth_date, &app_state.database_connection).await {
Ok(user_ids) => (StatusCode::OK, Json(serde_json::json!(user_ids))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_id_for_role(
State(app_state): State<AppState>,
Path(role_id): Path<i64>,
) -> impl IntoResponse {
match User::read_all_id_for_role(&role_id, &app_state.database_connection).await {
Ok(user_ids) => (StatusCode::OK, Json(serde_json::json!(user_ids))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all_id_for_gender(
State(app_state): State<AppState>,
Path(gender): Path<bool>,
) -> impl IntoResponse {
match User::read_all_id_for_gender(&gender, &app_state.database_connection).await {
Ok(user_ids) => (StatusCode::OK, Json(serde_json::json!(user_ids))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn count_all(State(app_state): State<AppState>) -> impl IntoResponse {
match User::count_all(&app_state.database_connection).await {
Ok(count) => (StatusCode::OK, Json(serde_json::json!(count))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn count_all_for_name(
State(app_state): State<AppState>,
Path(name): Path<String>,
) -> impl IntoResponse {
match User::count_all_for_name(&name, &app_state.database_connection).await {
Ok(count) => (StatusCode::OK, Json(serde_json::json!(count))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn count_all_for_surname(
State(app_state): State<AppState>,
Path(surname): Path<String>,
) -> impl IntoResponse {
match User::count_all_for_surname(&surname, &app_state.database_connection).await {
Ok(count) => (StatusCode::OK, Json(serde_json::json!(count))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn count_all_for_birth_date(
State(app_state): State<AppState>,
Path(birth_date): Path<NaiveDate>,
) -> impl IntoResponse {
match User::count_all_for_birth_date(&birth_date, &app_state.database_connection).await {
Ok(count) => (StatusCode::OK, Json(serde_json::json!(count))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn count_all_for_role(
State(app_state): State<AppState>,
Path(role_id): Path<i64>,
) -> impl IntoResponse {
match User::count_all_for_role(&role_id, &app_state.database_connection).await {
Ok(count) => (StatusCode::OK, Json(serde_json::json!(count))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn count_all_for_gender(
State(app_state): State<AppState>,
Path(gender): Path<bool>,
) -> impl IntoResponse {
match User::count_all_for_gender(&gender, &app_state.database_connection).await {
Ok(count) => (StatusCode::OK, Json(serde_json::json!(count))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}