summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-20 18:23:07 +0100
committerGitHub <noreply@github.com>2021-03-20 18:23:07 +0100
commit5a99c285de3a5cb9c44a5495c6bf2766259f7ecb (patch)
treeff9a459385e1c5870cc2413c9e6c0d3defa55d3c /src
parent4d3f2fd0e83a9412e8e42a0884047bfb0cfc4ee4 (diff)
parentc79ba1e9dee9552415f8783b2fdc0372dfe395a8 (diff)
Merge pull request #21 from gico-net/feat/top-authors
Top authors feat
Diffstat (limited to 'src')
-rw-r--r--src/commit/models.rs32
-rw-r--r--src/commit/routes.rs13
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)),