user, role, post db
This commit is contained in:
parent
20af44c357
commit
ee9015c1e3
15 changed files with 210 additions and 74 deletions
|
@ -1 +1,76 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::feature::post::Post;
|
||||
|
||||
pub async fn create(
|
||||
creation_time: &DateTime<Utc>,
|
||||
poster_id: i64,
|
||||
post: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Post, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Post,
|
||||
r#"
|
||||
INSERT INTO "post"(creation_time, poster_id, post)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING *
|
||||
"#,
|
||||
creation_time,
|
||||
poster_id,
|
||||
post
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read(
|
||||
creation_time: &DateTime<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Post, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Post,
|
||||
r#"
|
||||
SELECT * FROM "post" WHERE "creation_time" = $1
|
||||
"#,
|
||||
creation_time
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
creation_time: &DateTime<Utc>,
|
||||
poster_id: i64,
|
||||
post: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Post, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Post,
|
||||
r#"
|
||||
UPDATE "post" SET poster_id = $1, post = $2 WHERE "creation_time" = $3
|
||||
RETURNING *
|
||||
"#,
|
||||
poster_id,
|
||||
post,
|
||||
creation_time
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
creation_time: &DateTime<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Post, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Post,
|
||||
r#"
|
||||
DELETE FROM "post" where creation_time = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
creation_time
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
|
66
src/database/role.rs
Normal file
66
src/database/role.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::feature::role::Role;
|
||||
|
||||
pub async fn create(
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Role, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Role,
|
||||
r#"
|
||||
INSERT INTO "role"(name)
|
||||
VALUES ($1)
|
||||
RETURNING *
|
||||
"#,
|
||||
name,
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read(
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Role, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Role,
|
||||
r#"
|
||||
SELECT * FROM "role" WHERE "name" = $1
|
||||
"#,
|
||||
name
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
id: i64,
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Role, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Role,
|
||||
r#"
|
||||
UPDATE "role" SET "name" = $1 WHERE "id" = $2
|
||||
RETURNING *
|
||||
"#,
|
||||
name,
|
||||
id
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete(id: i64, database_connection: &Pool<Postgres>) -> Result<Role, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Role,
|
||||
r#"
|
||||
DELETE FROM "role" where id = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
|
@ -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
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue