From c79ba1e9dee9552415f8783b2fdc0372dfe395a8 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 20 Mar 2021 18:02:15 +0100 Subject: feat: add endpoint to get top authors --- src/commit/models.rs | 32 ++++++++++++++++++++++++++++++++ src/commit/routes.rs | 13 +++++++++++++ 2 files changed, 45 insertions(+) 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, 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, 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::>(); + + 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,11 +70,24 @@ 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) -> 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)) -- cgit v1.2.3-18-g5258