diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-14 00:39:37 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-03-14 00:39:37 +0100 |
commit | f7932c366bab2f5d0726f4d1cdf5b9dbeddf92eb (patch) | |
tree | 7913f9f79b01323464285372508fee8da261f285 /src/repository/models.rs | |
parent | 3232579f29d1c91e70e7dc76305d6589c4acbaae (diff) |
feat: find repo by id
Diffstat (limited to 'src/repository/models.rs')
-rw-r--r-- | src/repository/models.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/repository/models.rs b/src/repository/models.rs index ec5559b..54a93d7 100644 --- a/src/repository/models.rs +++ b/src/repository/models.rs @@ -1,5 +1,5 @@ use crate::db::get_client; -use crate::errors::AppError; +use crate::errors::{AppError, AppErrorType}; use chrono::NaiveDateTime; use deadpool_postgres::Pool; use serde::{Deserialize, Serialize}; @@ -33,4 +33,25 @@ impl Repository { Ok(repos) } + + pub async fn find(pool: Pool, id: &Uuid) -> Result<Repository, AppError> { + let client = get_client(pool.clone()).await.unwrap(); + let statement = client + .prepare("SELECT * FROM repository WHERE id = $1") + .await?; + + let repo = client + .query_opt(&statement, &[&id]) + .await? + .map(|row| Repository::from_row_ref(&row).unwrap()); + + match repo { + Some(repo) => Ok(repo), + None => Err(AppError { + error_type: AppErrorType::NotFoundError, + cause: None, + message: Some("Repository not found".to_string()), + }), + } + } } |