diff options
Diffstat (limited to 'src/commit/models.rs')
-rw-r--r-- | src/commit/models.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/commit/models.rs b/src/commit/models.rs new file mode 100644 index 0000000..215ab57 --- /dev/null +++ b/src/commit/models.rs @@ -0,0 +1,42 @@ +use crate::db::get_client; +use crate::errors::AppError; + +use chrono::NaiveDateTime; +use deadpool_postgres::Pool; +use serde::{Deserialize, Serialize}; +use tokio_pg_mapper::FromTokioPostgresRow; +use tokio_pg_mapper_derive::PostgresMapper; + +#[derive(Serialize, Deserialize, PostgresMapper)] +#[pg_mapper(table = "commit")] +/// Commit model +pub struct Commit { + pub hash: String, + pub tree: String, + pub text: String, + pub date: NaiveDateTime, + pub author_email: String, // Reference to Email + pub author_name: String, + pub committer_email: String, // Reference to Email + pub committer_name: String, + pub repository_url: String, // Reference to Repository +} + +impl Commit { + /// Find all commits. Order them by descrescent `date` field + pub async fn find_all(pool: Pool) -> Result<Vec<Commit>, AppError> { + let client = get_client(pool.clone()).await.unwrap(); + let statement = client + .prepare("SELECT * FROM commit ORDER BY date DESC") + .await?; + + let commits = client + .query(&statement, &[]) + .await? + .iter() + .map(|row| Commit::from_row_ref(row).unwrap()) + .collect::<Vec<Commit>>(); + + Ok(commits) + } +} |