summaryrefslogtreecommitdiff
path: root/src/commit
diff options
context:
space:
mode:
Diffstat (limited to 'src/commit')
-rw-r--r--src/commit/models.rs26
-rw-r--r--src/commit/routes.rs26
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),