From 8f853491ae808dd261cad5a053d5238181192e54 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 16 Mar 2021 10:54:20 +0100 Subject: feat: search repository helper --- src/repository/models.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/repository/models.rs b/src/repository/models.rs index bcddcf1..607008e 100644 --- a/src/repository/models.rs +++ b/src/repository/models.rs @@ -1,7 +1,7 @@ use crate::db::get_client; use crate::errors::{AppError, AppErrorType}; use chrono::NaiveDateTime; -use deadpool_postgres::Pool; +use deadpool_postgres::{Client, Pool}; use serde::{Deserialize, Serialize}; use tokio_pg_mapper::FromTokioPostgresRow; use tokio_pg_mapper_derive::PostgresMapper; @@ -91,4 +91,29 @@ impl Repository { }), } } + + /// Search a repository by its url + async fn search( + client: &Client, + url: String, + ) -> Result { + let statement = client + .prepare("SELECT * FROM repository WHERE url=$1") + .await?; + + let repo = client + .query_opt(&statement, &[&url]) + .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