summaryrefslogtreecommitdiff
path: root/src/repository
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-16 10:54:20 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-16 10:54:20 +0100
commit8f853491ae808dd261cad5a053d5238181192e54 (patch)
tree275990fe37a3af9554f3f739525e3d6bee60156f /src/repository
parent3273a9cc539ecf41029c2541a668c3ed24d9438f (diff)
feat: search repository helper
Diffstat (limited to 'src/repository')
-rw-r--r--src/repository/models.rs27
1 files changed, 26 insertions, 1 deletions
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<Repository, AppError> {
+ 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()),
+ }),
+ }
+ }
+
}