summaryrefslogtreecommitdiff
path: root/src/commit/models.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commit/models.rs')
-rw-r--r--src/commit/models.rs32
1 files changed, 32 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)
+ }
}