summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-18 12:13:31 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-18 12:13:31 +0100
commit23090f94334a662e11a3120fc84cf7a1e7e983b0 (patch)
tree7d3e6d6581e4d397725b5e9fcbadd6a47d1c744c /src
parent8fe649cb5f12f990ae067d6c80293c0dd241731c (diff)
feat(commit): create new commits from vec
Diffstat (limited to 'src')
-rw-r--r--src/commit/models.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/commit/models.rs b/src/commit/models.rs
index 8b81a15..2f1536b 100644
--- a/src/commit/models.rs
+++ b/src/commit/models.rs
@@ -93,4 +93,50 @@ impl Commit {
}),
}
}
+
+ /// Create commits from an array
+ pub async fn create(
+ pool: Pool,
+ commits: Vec<Commit>,
+ ) -> Result<Vec<Commit>, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let mut raw_query = "INSERT INTO commit VALUES".to_string();
+
+ for commit in commits {
+ let tree = match commit.tree {
+ Some(t) => format!("'{}'", t),
+ None => "NULL".to_string(),
+ };
+ raw_query += &format!(
+ "('{}', {}, '{}', '{}', '{}', '{}', '{}', '{}', '{}'),",
+ commit.hash,
+ tree,
+ commit.text,
+ commit.date,
+ commit.author_email,
+ commit.author_name,
+ commit.committer_email,
+ commit.committer_name,
+ commit.repository_url
+ )[..]
+ }
+
+ // Remove the last `,`
+ let _ = raw_query.pop();
+
+ // TODO: write query with &commits and parameter. Need to implement
+ // ToSql trait for `Commit` model
+ // let statement = client.prepare(&query[..]).await?;
+ // client.query(&statement, &[&commits]
+
+ let statement = client.prepare(&raw_query[..]).await?;
+ let result = client
+ .query(&statement, &[])
+ .await?
+ .iter()
+ .map(|row| Commit::from_row_ref(row).unwrap())
+ .collect::<Vec<Commit>>();
+
+ Ok(result)
+ }
}