2024-12-04 23:17:19 +03:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use sqlx::{Pool, Postgres};
|
2024-12-02 23:56:43 +03:00
|
|
|
|
2024-12-04 23:17:19 +03:00
|
|
|
use crate::feature::post::Post;
|
|
|
|
|
|
|
|
pub async fn create(
|
2024-12-09 15:18:32 +03:00
|
|
|
poster_id: &i64,
|
2024-12-04 23:17:19 +03:00
|
|
|
post: &String,
|
|
|
|
database_connection: &Pool<Postgres>,
|
|
|
|
) -> Result<Post, sqlx::Error> {
|
|
|
|
sqlx::query_as!(
|
|
|
|
Post,
|
|
|
|
r#"
|
2024-12-05 02:30:51 +03:00
|
|
|
INSERT INTO "post"(poster_id, post)
|
|
|
|
VALUES ($1, $2)
|
2024-12-04 23:17:19 +03:00
|
|
|
RETURNING *
|
|
|
|
"#,
|
|
|
|
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>,
|
2024-12-09 15:18:32 +03:00
|
|
|
poster_id: &i64,
|
2024-12-04 23:17:19 +03:00
|
|
|
post: &String,
|
|
|
|
database_connection: &Pool<Postgres>,
|
|
|
|
) -> Result<Post, sqlx::Error> {
|
|
|
|
sqlx::query_as!(
|
|
|
|
Post,
|
|
|
|
r#"
|
2024-12-14 03:11:39 +03:00
|
|
|
UPDATE "post" SET poster_id = $2, post = $3 WHERE "creation_time" = $1
|
2024-12-04 23:17:19 +03:00
|
|
|
RETURNING *
|
|
|
|
"#,
|
2024-12-14 03:11:39 +03:00
|
|
|
creation_time,
|
2024-12-04 23:17:19 +03:00
|
|
|
poster_id,
|
2024-12-14 03:11:39 +03:00
|
|
|
post
|
2024-12-04 23:17:19 +03:00
|
|
|
)
|
|
|
|
.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#"
|
2024-12-05 01:57:47 +03:00
|
|
|
DELETE FROM "post" where "creation_time" = $1
|
2024-12-04 23:17:19 +03:00
|
|
|
RETURNING *
|
|
|
|
"#,
|
|
|
|
creation_time
|
|
|
|
)
|
|
|
|
.fetch_one(database_connection)
|
|
|
|
.await
|
|
|
|
}
|
2024-12-05 02:47:51 +03:00
|
|
|
|
2024-12-06 02:04:48 +03:00
|
|
|
pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
2024-12-05 02:49:54 +03:00
|
|
|
sqlx::query_as!(
|
|
|
|
Post,
|
|
|
|
r#"
|
|
|
|
SELECT * FROM "post"
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.fetch_all(database_connection)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read_all_for_user(
|
2024-12-09 15:18:32 +03:00
|
|
|
poster_id: &i64,
|
2024-12-05 02:47:51 +03:00
|
|
|
database_connection: &Pool<Postgres>,
|
|
|
|
) -> Result<Vec<Post>, sqlx::Error> {
|
|
|
|
sqlx::query_as!(
|
|
|
|
Post,
|
|
|
|
r#"
|
|
|
|
SELECT * FROM "post" WHERE "poster_id" = $1
|
|
|
|
"#,
|
|
|
|
poster_id
|
|
|
|
)
|
|
|
|
.fetch_all(database_connection)
|
|
|
|
.await
|
2024-12-06 02:04:48 +03:00
|
|
|
}
|