From 06103e396a428ab693ee48df93236602114924b7 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 24 Mar 2021 20:58:39 +0100 Subject: feat: add endpoint to search commits by repository --- src/commit/models.rs | 26 ++++++++++++++++++++++++++ src/commit/routes.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/commit/models.rs b/src/commit/models.rs index 2925955..0e7ba24 100644 --- a/src/commit/models.rs +++ b/src/commit/models.rs @@ -66,6 +66,32 @@ impl Commit { Ok(result) } + /// Find all repository url' commits + pub async fn find_by_repository( + pool: Pool, + repository_url: String, + ) -> Result, AppError> { + let client = get_client(pool.clone()).await.unwrap(); + + let statement = client + .prepare( + "SELECT * FROM commit + WHERE repository_url = $1 + ORDER BY date DESC + LIMIT 1000", + ) + .await?; + + let commits = client + .query(&statement, &[&repository_url]) + .await? + .iter() + .map(|row| Commit::from_row_ref(row).unwrap()) + .collect::>(); + + Ok(commits) + } + // Find a commit that it has an hash equals to `hash` pub async fn find(pool: Pool, hash: String) -> Result { let client = get_client(pool.clone()).await.unwrap(); diff --git a/src/commit/routes.rs b/src/commit/routes.rs index a1da519..542edbb 100644 --- a/src/commit/routes.rs +++ b/src/commit/routes.rs @@ -21,9 +21,31 @@ async fn index( None => String::new(), }; - info!(state.log, "GET /commit/?q={}", &hash); + let repo_user = match query.get("repository_user") { + Some(x) => x.clone(), + None => String::new(), + }; + let repo_name = match query.get("repository_name") { + Some(x) => x.clone(), + None => String::new(), + }; - let result = Commit::find_all(state.pool.clone(), &hash).await; + let result; + if repo_user != "" && repo_name != "" { + info!( + state.log, + "GET /commit/?repository_user={}&repository_name={}", + &repo_user, + &repo_name + ); + let repository_url = format!("{}/{}", repo_user, repo_name); + result = + Commit::find_by_repository(state.pool.clone(), repository_url) + .await; + } else { + info!(state.log, "GET /commit/?q={}", &hash); + result = Commit::find_all(state.pool.clone(), &hash).await; + } match result { Ok(commits) => HttpResponse::Ok().json(commits), -- cgit v1.2.3-18-g5258