From f7932c366bab2f5d0726f4d1cdf5b9dbeddf92eb Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 14 Mar 2021 00:39:37 +0100 Subject: feat: find repo by id --- src/repository/models.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/repository/models.rs') 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 { + 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()), + }), + } + } } -- cgit v1.2.3-18-g5258