2024-12-04 23:17:19 +03:00
|
|
|
use chrono::{DateTime, Utc};
|
2024-12-02 23:56:43 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-12-15 04:30:39 +03:00
|
|
|
use sqlx::{Pool, Postgres};
|
|
|
|
|
|
|
|
use crate::database::post;
|
2024-12-02 23:56:43 +03:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Post {
|
2024-12-04 23:17:19 +03:00
|
|
|
pub creation_time: DateTime<Utc>,
|
2024-12-15 04:30:39 +03:00
|
|
|
pub user_id: i64,
|
2024-12-02 23:56:43 +03:00
|
|
|
pub post: String,
|
|
|
|
}
|
2024-12-15 04:30:39 +03:00
|
|
|
|
|
|
|
impl Post {
|
|
|
|
pub async fn create(
|
|
|
|
user_id: &i64,
|
|
|
|
post: &String,
|
|
|
|
database_connection: &Pool<Postgres>,
|
|
|
|
) -> Result<Post, sqlx::Error> {
|
|
|
|
post::create(user_id, post, database_connection).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read(
|
|
|
|
creation_time: &DateTime<Utc>,
|
|
|
|
database_connection: &Pool<Postgres>,
|
|
|
|
) -> Result<Post, sqlx::Error> {
|
|
|
|
post::read(creation_time, database_connection).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update(
|
|
|
|
creation_time: &DateTime<Utc>,
|
|
|
|
user_id: &i64,
|
|
|
|
post: &String,
|
|
|
|
database_connection: &Pool<Postgres>,
|
|
|
|
) -> Result<Post, sqlx::Error> {
|
|
|
|
post::update(creation_time, user_id, post, database_connection).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete(
|
|
|
|
creation_time: &DateTime<Utc>,
|
|
|
|
database_connection: &Pool<Postgres>,
|
|
|
|
) -> Result<Post, sqlx::Error> {
|
|
|
|
post::delete(creation_time, database_connection).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
|
|
|
post::read_all(database_connection).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read_all_for_user(
|
|
|
|
user_id: &i64,
|
|
|
|
database_connection: &Pool<Postgres>,
|
|
|
|
) -> Result<Vec<Post>, sqlx::Error> {
|
|
|
|
post::read_all_for_user(user_id, database_connection).await
|
|
|
|
}
|
|
|
|
}
|