From b3b93ab0ccebb021d03bef47a4b6a8d52962fb95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Tue, 17 Dec 2024 03:19:34 +0300 Subject: [PATCH] feat: :sparkles: more user functions exposed to routing --- src/database/user.rs | 140 ++++++++++++------------- src/feature/user.rs | 115 +++++++++++++++++++++ src/routing/user.rs | 238 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 423 insertions(+), 70 deletions(-) diff --git a/src/database/user.rs b/src/database/user.rs index 010e927..b3c970b 100644 --- a/src/database/user.rs +++ b/src/database/user.rs @@ -167,40 +167,6 @@ pub async fn read_all_id(database_connection: &Pool) -> Result>()) } -pub async fn read_all_id_for_role( - role_id: &i64, - database_connection: &Pool, -) -> Result, sqlx::Error> { - Ok(sqlx::query!( - r#" - SELECT "id" FROM "user" WHERE "role_id" = $1 - "#, - role_id - ) - .fetch_all(database_connection) - .await? - .iter() - .map(|record| record.id) - .collect::>()) -} - -pub async fn read_all_id_for_gender( - gender: &bool, - database_connection: &Pool, -) -> Result, sqlx::Error> { - Ok(sqlx::query!( - r#" - SELECT "id" FROM "user" WHERE "gender" = $1 - "#, - gender - ) - .fetch_all(database_connection) - .await? - .iter() - .map(|record| record.id) - .collect::>()) -} - pub async fn read_all_id_for_name( name: &String, database_connection: &Pool, @@ -252,6 +218,40 @@ pub async fn read_all_id_for_birth_date( .collect::>()) } +pub async fn read_all_id_for_role( + role_id: &i64, + database_connection: &Pool, +) -> Result, sqlx::Error> { + Ok(sqlx::query!( + r#" + SELECT "id" FROM "user" WHERE "role_id" = $1 + "#, + role_id + ) + .fetch_all(database_connection) + .await? + .iter() + .map(|record| record.id) + .collect::>()) +} + +pub async fn read_all_id_for_gender( + gender: &bool, + database_connection: &Pool, +) -> Result, sqlx::Error> { + Ok(sqlx::query!( + r#" + SELECT "id" FROM "user" WHERE "gender" = $1 + "#, + gender + ) + .fetch_all(database_connection) + .await? + .iter() + .map(|record| record.id) + .collect::>()) +} + pub async fn count_all(database_connection: &Pool) -> Result { sqlx::query!( r#" @@ -266,42 +266,6 @@ pub async fn count_all(database_connection: &Pool) -> Result, -) -> Result { - sqlx::query!( - r#" - SELECT COUNT(id) FROM "user" WHERE "gender" = $1 - "#, - gender - ) - .fetch_one(database_connection) - .await? - .count - .map_or(0, |count| count) - .try_into() - .or(Ok(0)) -} - -pub async fn count_all_for_role( - role_id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query!( - r#" - SELECT COUNT(id) FROM "user" WHERE "role_id" = $1 - "#, - role_id - ) - .fetch_one(database_connection) - .await? - .count - .map_or(0, |count| count) - .try_into() - .or(Ok(0)) -} - pub async fn count_all_for_name( name: &String, database_connection: &Pool, @@ -355,3 +319,39 @@ pub async fn count_all_for_birth_date( .try_into() .or(Ok(0)) } + +pub async fn count_all_for_role( + role_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query!( + r#" + SELECT COUNT(id) FROM "user" WHERE "role_id" = $1 + "#, + role_id + ) + .fetch_one(database_connection) + .await? + .count + .map_or(0, |count| count) + .try_into() + .or(Ok(0)) +} + +pub async fn count_all_for_gender( + gender: &bool, + database_connection: &Pool, +) -> Result { + sqlx::query!( + r#" + SELECT COUNT(id) FROM "user" WHERE "gender" = $1 + "#, + gender + ) + .fetch_one(database_connection) + .await? + .count + .map_or(0, |count| count) + .try_into() + .or(Ok(0)) +} diff --git a/src/feature/user.rs b/src/feature/user.rs index 1083088..a66b657 100644 --- a/src/feature/user.rs +++ b/src/feature/user.rs @@ -68,4 +68,119 @@ impl User { pub async fn read_all(database_connection: &Pool) -> Result, sqlx::Error> { user::read_all(database_connection).await } + + pub async fn read_all_for_name( + name: &String, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_for_name(name, database_connection).await + } + + pub async fn read_all_for_surname( + surname: &String, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_for_surname(surname, database_connection).await + } + + pub async fn read_all_for_birth_date( + birth_date: &NaiveDate, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_for_birth_date(birth_date, database_connection).await + } + + pub async fn read_all_for_role( + role_id: &i64, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_for_role(role_id, database_connection).await + } + + pub async fn read_all_for_gender( + gender: &bool, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_for_gender(gender, database_connection).await + } + + pub async fn read_all_id( + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_id(database_connection).await + } + + pub async fn read_all_id_for_name( + name: &String, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_id_for_name(name, database_connection).await + } + + pub async fn read_all_id_for_surname( + surname: &String, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_id_for_surname(surname, database_connection).await + } + + pub async fn read_all_id_for_birth_date( + birth_date: &NaiveDate, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_id_for_birth_date(birth_date, database_connection).await + } + + pub async fn read_all_id_for_role( + role_id: &i64, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_id_for_role(role_id, database_connection).await + } + + pub async fn read_all_id_for_gender( + gender: &bool, + database_connection: &Pool, + ) -> Result, sqlx::Error> { + user::read_all_id_for_gender(gender, database_connection).await + } + + pub async fn count_all(database_connection: &Pool) -> Result { + user::count_all(database_connection).await + } + + pub async fn count_all_for_name( + name: &String, + database_connection: &Pool, + ) -> Result { + user::count_all_for_name(name, database_connection).await + } + + pub async fn count_all_for_surname( + surname: &String, + database_connection: &Pool, + ) -> Result { + user::count_all_for_surname(surname, database_connection).await + } + + pub async fn count_all_for_birth_date( + birth_date: &NaiveDate, + database_connection: &Pool, + ) -> Result { + user::count_all_for_birth_date(birth_date, database_connection).await + } + + pub async fn count_all_for_role( + role_id: &i64, + database_connection: &Pool, + ) -> Result { + user::count_all_for_role(role_id, database_connection).await + } + + pub async fn count_all_for_gender( + gender: &bool, + database_connection: &Pool, + ) -> Result { + user::count_all_for_gender(gender, database_connection).await + } } diff --git a/src/routing/user.rs b/src/routing/user.rs index 58a0b14..a0f1cde 100644 --- a/src/routing/user.rs +++ b/src/routing/user.rs @@ -35,6 +35,29 @@ pub fn route(State(app_state): State) -> Router { .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) -> impl IntoResponse { ), } } + +async fn read_all_for_name( + State(app_state): State, + Path(name): Path, +) -> 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, + Path(surname): Path, +) -> 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, + Path(birth_date): Path, +) -> 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, + Path(role_id): Path, +) -> 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, + Path(gender): Path, +) -> 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) -> 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, + Path(name): Path, +) -> 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, + Path(surname): Path, +) -> 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, + Path(birth_date): Path, +) -> 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, + Path(role_id): Path, +) -> 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, + Path(gender): Path, +) -> 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) -> 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, + Path(name): Path, +) -> 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, + Path(surname): Path, +) -> 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, + Path(birth_date): Path, +) -> 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, + Path(role_id): Path, +) -> 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, + Path(gender): Path, +) -> 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())), + ), + } +}