diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-24 20:58:39 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-03-24 20:58:39 +0100 |
commit | 06103e396a428ab693ee48df93236602114924b7 (patch) | |
tree | 3d37cd79664d15f3ba57a5ae2ce82aaba4127171 /src | |
parent | 005e85c170890e12df4fe407b27a539a4c9d963a (diff) |
feat: add endpoint to search commits by repository
Diffstat (limited to 'src')
-rw-r--r-- | src/commit/models.rs | 26 | ||||
-rw-r--r-- | 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<Vec<Commit>, 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::<Vec<Commit>>(); + + Ok(commits) + } + // Find a commit that it has an hash equals to `hash` pub async fn find(pool: Pool, hash: String) -> Result<Commit, AppError> { 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), |