diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-20 18:02:15 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-03-20 18:02:15 +0100 |
commit | c79ba1e9dee9552415f8783b2fdc0372dfe395a8 (patch) | |
tree | ff9a459385e1c5870cc2413c9e6c0d3defa55d3c | |
parent | 4d3f2fd0e83a9412e8e42a0884047bfb0cfc4ee4 (diff) |
feat: add endpoint to get top authors
-rw-r--r-- | src/commit/models.rs | 32 | ||||
-rw-r--r-- | src/commit/routes.rs | 13 |
2 files changed, 45 insertions, 0 deletions
diff --git a/src/commit/models.rs b/src/commit/models.rs index 95d2efa..cec135a 100644 --- a/src/commit/models.rs +++ b/src/commit/models.rs @@ -22,6 +22,14 @@ pub struct Commit { pub repository_url: String, // Reference to Repository } +/// Model used for 'most authors' function +#[derive(Serialize, Deserialize)] +pub struct CommitNumAuthor { + pub num: i64, + pub author_email: String, + pub author_name: String, +} + impl Commit { /// Find all commits. Order them by descrescent `date` field pub async fn find_all(pool: Pool) -> Result<Vec<Commit>, AppError> { @@ -143,4 +151,28 @@ impl Commit { Ok(result) } + + /// Returns a ranking of authors of his commits number + pub async fn most_authors( + pool: Pool, + ) -> Result<Vec<CommitNumAuthor>, AppError> { + let client = get_client(pool.clone()).await.unwrap(); + let statement = client.prepare( + "SELECT COUNT(hash) as num, author_email, author_name FROM commit + GROUP BY author_email, author_name ORDER BY COUNT(hash) DESC" + ).await?; + + let authors = client + .query(&statement, &[]) + .await? + .iter() + .map(|row| CommitNumAuthor { + num: row.get(0), + author_email: row.get(1), + author_name: row.get(2), + }) + .collect::<Vec<CommitNumAuthor>>(); + + Ok(authors) + } } diff --git a/src/commit/routes.rs b/src/commit/routes.rs index 95c0faa..95a74a2 100644 --- a/src/commit/routes.rs +++ b/src/commit/routes.rs @@ -70,12 +70,25 @@ async fn delete_commit( .map_err(|e| e) } +/// Endpoint used for getting a raking of the post authors by commit number +async fn get_top_authors(state: web::Data<AppState>) -> impl Responder { + info!(state.log, "GET /commit/top/"); + let result = Commit::most_authors(state.pool.clone()).await; + + result + .map(|authors| HttpResponse::Ok().json(authors)) + .map_err(|e| e) +} + /// Routes for commits pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( web::scope("/commit") .service(web::resource("/").route(web::get().to(index))) .service( + web::resource("/top/").route(web::get().to(get_top_authors)), + ) + .service( web::resource("/{hash}/") .route(web::get().to(get_commit)) .route(web::delete().to(delete_commit)), |