rust_forum/src/database/interaction.rs

83 lines
1.6 KiB
Rust
Raw Normal View History

2024-12-05 01:57:47 +03:00
use sqlx::{Pool, Postgres};
2024-12-02 23:56:43 +03:00
2024-12-05 01:57:47 +03:00
use crate::feature::interaction::Interaction;
pub async fn create(
name: &String,
database_connection: &Pool<Postgres>,
) -> Result<Interaction, sqlx::Error> {
sqlx::query_as!(
Interaction,
r#"
INSERT INTO "interaction"(name)
VALUES ($1)
RETURNING *
"#,
name,
)
.fetch_one(database_connection)
.await
}
pub async fn read(
2024-12-15 21:07:58 +03:00
id: &i64,
2024-12-05 01:57:47 +03:00
database_connection: &Pool<Postgres>,
) -> Result<Interaction, sqlx::Error> {
sqlx::query_as!(
Interaction,
r#"
2024-12-15 21:07:58 +03:00
SELECT * FROM "interaction" WHERE "id" = $1
2024-12-05 01:57:47 +03:00
"#,
2024-12-15 21:07:58 +03:00
id
2024-12-05 01:57:47 +03:00
)
.fetch_one(database_connection)
.await
}
pub async fn update(
2024-12-09 15:18:32 +03:00
id: &i64,
2024-12-05 01:57:47 +03:00
name: &String,
database_connection: &Pool<Postgres>,
) -> Result<Interaction, sqlx::Error> {
sqlx::query_as!(
Interaction,
r#"
UPDATE "interaction" SET "name" = $2 WHERE "id" = $1
2024-12-05 01:57:47 +03:00
RETURNING *
"#,
id,
name
2024-12-05 01:57:47 +03:00
)
.fetch_one(database_connection)
.await
}
pub async fn delete(
2024-12-09 15:18:32 +03:00
id: &i64,
2024-12-05 01:57:47 +03:00
database_connection: &Pool<Postgres>,
) -> Result<Interaction, sqlx::Error> {
sqlx::query_as!(
Interaction,
r#"
DELETE FROM "interaction" WHERE "id" = $1
2024-12-05 01:57:47 +03:00
RETURNING *
"#,
id
)
.fetch_one(database_connection)
.await
}
2024-12-15 21:07:58 +03:00
pub async fn read_all(
database_connection: &Pool<Postgres>,
) -> Result<Vec<Interaction>, sqlx::Error> {
sqlx::query_as!(
Interaction,
r#"
SELECT * FROM "interaction"
"#,
)
.fetch_all(database_connection)
.await
}