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

@ -167,40 +167,6 @@ pub async fn read_all_id(database_connection: &Pool<Postgres>) -> Result<Vec<i64
.collect::<Vec<i64>>())
}
pub async fn read_all_id_for_role(
role_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<Vec<i64>, 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::<Vec<i64>>())
}
pub async fn read_all_id_for_gender(
gender: &bool,
database_connection: &Pool<Postgres>,
) -> Result<Vec<i64>, 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::<Vec<i64>>())
}
pub async fn read_all_id_for_name(
name: &String,
database_connection: &Pool<Postgres>,
@ -252,6 +218,40 @@ pub async fn read_all_id_for_birth_date(
.collect::<Vec<i64>>())
}
pub async fn read_all_id_for_role(
role_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<Vec<i64>, 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::<Vec<i64>>())
}
pub async fn read_all_id_for_gender(
gender: &bool,
database_connection: &Pool<Postgres>,
) -> Result<Vec<i64>, 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::<Vec<i64>>())
}
pub async fn count_all(database_connection: &Pool<Postgres>) -> Result<u64, sqlx::Error> {
sqlx::query!(
r#"
@ -266,42 +266,6 @@ pub async fn count_all(database_connection: &Pool<Postgres>) -> Result<u64, sqlx
.or(Ok(0))
}
pub async fn count_all_for_gender(
gender: &bool,
database_connection: &Pool<Postgres>,
) -> Result<u64, sqlx::Error> {
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<Postgres>,
) -> Result<u64, sqlx::Error> {
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<Postgres>,
@ -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<Postgres>,
) -> Result<u64, sqlx::Error> {
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<Postgres>,
) -> Result<u64, sqlx::Error> {
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))
}

View file

@ -68,4 +68,119 @@ impl User {
pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<User>, sqlx::Error> {
user::read_all(database_connection).await
}
pub async fn read_all_for_name(
name: &String,
database_connection: &Pool<Postgres>,
) -> Result<Vec<User>, sqlx::Error> {
user::read_all_for_name(name, database_connection).await
}
pub async fn read_all_for_surname(
surname: &String,
database_connection: &Pool<Postgres>,
) -> Result<Vec<User>, 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<Postgres>,
) -> Result<Vec<User>, 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<Postgres>,
) -> Result<Vec<User>, sqlx::Error> {
user::read_all_for_role(role_id, database_connection).await
}
pub async fn read_all_for_gender(
gender: &bool,
database_connection: &Pool<Postgres>,
) -> Result<Vec<User>, sqlx::Error> {
user::read_all_for_gender(gender, database_connection).await
}
pub async fn read_all_id(
database_connection: &Pool<Postgres>,
) -> Result<Vec<i64>, sqlx::Error> {
user::read_all_id(database_connection).await
}
pub async fn read_all_id_for_name(
name: &String,
database_connection: &Pool<Postgres>,
) -> Result<Vec<i64>, 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<Postgres>,
) -> Result<Vec<i64>, 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<Postgres>,
) -> Result<Vec<i64>, 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<Postgres>,
) -> Result<Vec<i64>, 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<Postgres>,
) -> Result<Vec<i64>, sqlx::Error> {
user::read_all_id_for_gender(gender, database_connection).await
}
pub async fn count_all(database_connection: &Pool<Postgres>) -> Result<u64, sqlx::Error> {
user::count_all(database_connection).await
}
pub async fn count_all_for_name(
name: &String,
database_connection: &Pool<Postgres>,
) -> Result<u64, sqlx::Error> {
user::count_all_for_name(name, database_connection).await
}
pub async fn count_all_for_surname(
surname: &String,
database_connection: &Pool<Postgres>,
) -> Result<u64, sqlx::Error> {
user::count_all_for_surname(surname, database_connection).await
}
pub async fn count_all_for_birth_date(
birth_date: &NaiveDate,
database_connection: &Pool<Postgres>,
) -> Result<u64, sqlx::Error> {
user::count_all_for_birth_date(birth_date, database_connection).await
}
pub async fn count_all_for_role(
role_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<u64, sqlx::Error> {
user::count_all_for_role(role_id, database_connection).await
}
pub async fn count_all_for_gender(
gender: &bool,
database_connection: &Pool<Postgres>,
) -> Result<u64, sqlx::Error> {
user::count_all_for_gender(gender, database_connection).await
}
}

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())),
),
}
}