rust_forum/src/database/user.rs

391 lines
8.4 KiB
Rust
Raw Normal View History

2024-12-03 21:33:42 +03:00
use chrono::NaiveDate;
use sqlx::{Pool, Postgres};
2024-12-01 22:17:38 +03:00
2024-12-04 23:17:19 +03:00
use crate::feature::user::User;
2024-12-03 21:33:42 +03:00
2024-12-04 23:17:19 +03:00
pub async fn create(
2024-12-03 21:33:42 +03:00
name: &String,
surname: &String,
2024-12-07 23:43:15 +03:00
gender: &bool,
2024-12-03 21:33:42 +03:00
birth_date: &NaiveDate,
email: &String,
database_connection: &Pool<Postgres>,
) -> Result<User, sqlx::Error> {
sqlx::query_as!(
User,
r#"
2024-12-04 23:17:19 +03:00
INSERT INTO "user"(name, surname, gender, birth_date, email, role_id)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *
2024-12-03 21:33:42 +03:00
"#,
name,
surname,
gender,
birth_date,
2024-12-04 23:17:19 +03:00
email,
2
2024-12-03 21:33:42 +03:00
)
.fetch_one(database_connection)
.await
}
2024-12-07 23:43:15 +03:00
pub async fn read(id: &i64, database_connection: &Pool<Postgres>) -> Result<User, sqlx::Error> {
2024-12-04 23:17:19 +03:00
sqlx::query_as!(
User,
2024-12-03 21:33:42 +03:00
r#"
2024-12-07 23:43:15 +03:00
SELECT * FROM "user" WHERE "id" = $1
2024-12-04 23:17:19 +03:00
"#,
2024-12-07 23:43:15 +03:00
id
2024-12-04 23:17:19 +03:00
)
.fetch_one(database_connection)
.await
2024-12-03 21:33:42 +03:00
}
2024-12-04 23:17:19 +03:00
pub async fn update(
2024-12-07 23:43:15 +03:00
id: &i64,
2024-12-03 21:33:42 +03:00
name: &String,
surname: &String,
gender: &bool,
birth_date: &NaiveDate,
email: &String,
2024-12-07 23:43:15 +03:00
role_id: &i64,
2024-12-03 21:33:42 +03:00
database_connection: &Pool<Postgres>,
) -> Result<User, sqlx::Error> {
sqlx::query_as!(User,
r#"
2024-12-04 23:17:19 +03:00
UPDATE "user" SET "name" = $1, "surname" = $2, "gender" = $3, "birth_date" = $4, "email" = $5, "role_id" = $6 WHERE "id" = $7
RETURNING *
"#, name, surname, gender, birth_date, email, role_id, id).fetch_one(database_connection).await
2024-12-03 21:33:42 +03:00
}
2024-12-07 23:43:15 +03:00
pub async fn delete(id: &i64, database_connection: &Pool<Postgres>) -> Result<User, sqlx::Error> {
2024-12-03 21:33:42 +03:00
sqlx::query_as!(
User,
r#"
2024-12-05 01:57:47 +03:00
DELETE FROM "user" where "id" = $1
2024-12-04 23:17:19 +03:00
RETURNING *
2024-12-03 21:33:42 +03:00
"#,
id
)
.fetch_one(database_connection)
.await
}
pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<User>, sqlx::Error> {
sqlx::query_as!(
User,
r#"
SELECT * FROM "user"
"#,
)
.fetch_all(database_connection)
.await
}
2024-12-07 23:43:15 +03:00
pub async fn read_with_email(
email: &String,
database_connection: &Pool<Postgres>,
) -> Result<User, sqlx::Error> {
sqlx::query_as!(
User,
r#"
SELECT * FROM "user" WHERE "email" = $1
"#,
email
)
.fetch_one(database_connection)
.await
}
pub async fn read_all_for_name(
name: &String,
database_connection: &Pool<Postgres>,
) -> Result<Vec<User>, sqlx::Error> {
sqlx::query_as!(
User,
r#"
SELECT * FROM "user" WHERE "name" = $1
"#,
name
)
.fetch_all(database_connection)
.await
}
pub async fn read_all_for_surname(
surname: &String,
database_connection: &Pool<Postgres>,
) -> Result<Vec<User>, sqlx::Error> {
sqlx::query_as!(
User,
r#"
SELECT * FROM "user" WHERE "surname" = $1
"#,
surname
)
.fetch_all(database_connection)
.await
}
pub async fn read_all_for_birth_date(
birth_date: &NaiveDate,
database_connection: &Pool<Postgres>,
) -> Result<Vec<User>, sqlx::Error> {
sqlx::query_as!(
User,
r#"
SELECT * FROM "user" WHERE "birth_date" = $1
"#,
birth_date
)
.fetch_all(database_connection)
.await
}
pub async fn read_all_for_role(
2024-12-07 23:43:15 +03:00
role_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<Vec<User>, sqlx::Error> {
sqlx::query_as!(
User,
r#"
SELECT * FROM "user" WHERE "role_id" = $1
"#,
role_id
)
.fetch_all(database_connection)
.await
}
2024-12-07 23:43:15 +03:00
pub async fn read_all_for_gender(
gender: &bool,
database_connection: &Pool<Postgres>,
) -> Result<Vec<User>, sqlx::Error> {
sqlx::query_as!(
User,
r#"
SELECT * FROM "user" WHERE "gender" = $1
"#,
gender
)
.fetch_all(database_connection)
.await
}
pub async fn read_id_with_email(
email: &String,
database_connection: &Pool<Postgres>,
) -> Result<i64, sqlx::Error> {
Ok(sqlx::query!(
r#"
SELECT "id" FROM "user" WHERE "email" = $1
"#,
email
)
.fetch_one(database_connection)
.await?
.id)
}
pub async fn read_all_id(database_connection: &Pool<Postgres>) -> Result<Vec<i64>, sqlx::Error> {
Ok(sqlx::query!(
r#"
SELECT "id" FROM "user"
"#,
)
.fetch_all(database_connection)
.await?
.iter()
.map(|record| record.id)
.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>,
) -> Result<Vec<i64>, sqlx::Error> {
Ok(sqlx::query!(
r#"
SELECT "id" FROM "user" WHERE "name" = $1
"#,
name
)
.fetch_all(database_connection)
.await?
.iter()
.map(|record| record.id)
.collect::<Vec<i64>>())
}
pub async fn read_all_id_for_surname(
surname: &String,
database_connection: &Pool<Postgres>,
) -> Result<Vec<i64>, sqlx::Error> {
Ok(sqlx::query!(
r#"
SELECT "id" FROM "user" WHERE "surname" = $1
"#,
surname
)
.fetch_all(database_connection)
.await?
.iter()
.map(|record| record.id)
.collect::<Vec<i64>>())
}
2024-12-09 15:18:32 +03:00
pub async fn read_all_id_for_birth_date(
birth_date: &NaiveDate,
database_connection: &Pool<Postgres>,
) -> Result<Vec<i64>, sqlx::Error> {
Ok(sqlx::query!(
r#"
SELECT "id" FROM "user" WHERE "birth_date" = $1
"#,
birth_date
)
.fetch_all(database_connection)
.await?
.iter()
.map(|record| record.id)
.collect::<Vec<i64>>())
}
2024-12-07 23:43:15 +03:00
pub async fn count_all(database_connection: &Pool<Postgres>) -> Result<u64, sqlx::Error> {
sqlx::query!(
r#"
SELECT COUNT(id) FROM "user"
"#,
)
.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))
}
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>,
) -> Result<u64, sqlx::Error> {
sqlx::query!(
r#"
SELECT COUNT(id) FROM "user" WHERE "name" = $1
"#,
name
)
.fetch_one(database_connection)
.await?
.count
.map_or(0, |count| count)
.try_into()
.or(Ok(0))
}
pub async fn count_all_for_surname(
surname: &String,
database_connection: &Pool<Postgres>,
) -> Result<u64, sqlx::Error> {
sqlx::query!(
r#"
SELECT COUNT(id) FROM "user" WHERE "surname" = $1
"#,
surname
)
.fetch_one(database_connection)
.await?
.count
.map_or(0, |count| count)
.try_into()
.or(Ok(0))
}
2024-12-09 15:18:32 +03:00
pub async fn count_all_for_birth_date(
birth_date: &NaiveDate,
database_connection: &Pool<Postgres>,
) -> Result<u64, sqlx::Error> {
sqlx::query!(
r#"
SELECT COUNT(id) FROM "user" WHERE "birth_date" = $1
"#,
birth_date
)
.fetch_one(database_connection)
.await?
.count
.map_or(0, |count| count)
.try_into()
.or(Ok(0))
}