summaryrefslogtreecommitdiff
path: root/src/commit/models.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commit/models.rs')
-rw-r--r--src/commit/models.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/commit/models.rs b/src/commit/models.rs
index 6246511..c70ecf2 100644
--- a/src/commit/models.rs
+++ b/src/commit/models.rs
@@ -1,5 +1,5 @@
use crate::db::get_client;
-use crate::errors::AppError;
+use crate::errors::{AppError, AppErrorType};
use chrono::{DateTime, Local};
use deadpool_postgres::Pool;
@@ -39,4 +39,26 @@ impl Commit {
Ok(commits)
}
+
+ // Find a commit that it has an hash equals to `hash`
+ pub async fn find(pool: Pool, hash: String) -> Result<Commit, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let statement = client
+ .prepare("SELECT * FROM commit WHERE hash = $1")
+ .await?;
+
+ let commit = client
+ .query_opt(&statement, &[&hash])
+ .await?
+ .map(|row| Commit::from_row_ref(&row).unwrap());
+
+ match commit {
+ Some(commit) => Ok(commit),
+ None => Err(AppError {
+ error_type: AppErrorType::NotFoundError,
+ cause: None,
+ message: Some("Commit not found".to_string()),
+ }),
+ }
+ }
}