From 23090f94334a662e11a3120fc84cf7a1e7e983b0 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 18 Mar 2021 12:13:31 +0100 Subject: feat(commit): create new commits from vec --- src/commit/models.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) 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, + ) -> Result, 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::>(); + + Ok(result) + } } -- cgit v1.2.3-18-g5258