user, role, post db

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-04 23:17:19 +03:00
parent 20af44c357
commit ee9015c1e3
15 changed files with 210 additions and 74 deletions

View file

@ -1,9 +1,9 @@
use chrono::NaiveDate;
use sqlx::{Pool, Postgres};
use crate::feature::user::{Role, User};
use crate::feature::user::User;
pub async fn create_user(
pub async fn create(
name: &String,
surname: &String,
gender: bool,
@ -14,57 +14,59 @@ pub async fn create_user(
sqlx::query_as!(
User,
r#"
INSERT INTO "user"(name, surname, gender, birth_date, email)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, name, surname, gender, birth_date, email, role AS "role:Role"
INSERT INTO "user"(name, surname, gender, birth_date, email, role_id)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *
"#,
name,
surname,
gender,
birth_date,
email,
2
)
.fetch_one(database_connection)
.await
}
pub async fn read(
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_user(
email: &String,
database_connection: &Pool<Postgres>,
) -> Result<User, sqlx::Error> {
sqlx::query_as!(User,
r#"
SELECT id, name, surname, gender, birth_date, email, role AS "role:Role" FROM "user" WHERE "email" = $1
"#,
email
).fetch_one(database_connection).await
}
pub async fn update_user(
pub async fn update(
id: i64,
name: &String,
surname: &String,
gender: &bool,
birth_date: &NaiveDate,
email: &String,
role_id: i64,
database_connection: &Pool<Postgres>,
) -> Result<User, sqlx::Error> {
sqlx::query_as!(User,
r#"
UPDATE "user" SET "name" = $1, "surname" = $2, "gender" = $3, "birth_date" = $4, "email" = $5 WHERE "id" = $6
RETURNING id, name, surname, gender, birth_date, email, role AS "role:Role"
"#, name, surname, gender, birth_date, email, id).fetch_one(database_connection).await
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
}
pub async fn delete_user(
id: i64,
database_connection: &Pool<Postgres>,
) -> Result<User, sqlx::Error> {
pub async fn delete(id: i64, database_connection: &Pool<Postgres>) -> Result<User, sqlx::Error> {
sqlx::query_as!(
User,
r#"
DELETE FROM "user" where id = $1
RETURNING id, name, surname, gender, birth_date, email, role AS "role:Role"
RETURNING *
"#,
id
)