summaryrefslogtreecommitdiff
path: root/src/branch
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-17 23:26:07 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-17 23:26:07 +0100
commit3dff10c09ef6db02360a2d2e914e8c2d96ef7a89 (patch)
treeb663cd9542dd420ef88c53745207eef38e11717b /src/branch
parent9c9921d9151f813e30aed42cf2b0ae3ae775cc52 (diff)
feat: method for create a new branch
Actually is unusued until #15
Diffstat (limited to 'src/branch')
-rw-r--r--src/branch/models.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/branch/models.rs b/src/branch/models.rs
index b631162..c8a8d65 100644
--- a/src/branch/models.rs
+++ b/src/branch/models.rs
@@ -17,6 +17,13 @@ pub struct Branch {
pub head: String,
}
+/// Struct used for forms
+pub struct BranchData {
+ pub name: String,
+ pub repository_id: Uuid,
+ pub head: String,
+}
+
impl Branch {
/// Find all branches
pub async fn find_all(pool: Pool) -> Result<Vec<Branch>, AppError> {
@@ -102,4 +109,40 @@ impl Branch {
}),
}
}
+
+ /// Create a new branch
+ pub async fn create(
+ pool: Pool,
+ data: &BranchData,
+ ) -> Result<Branch, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+
+ let statement = client
+ .prepare(
+ "INSERT INTO repository(id, name, repository_id, head)
+ VALUES($1, $2, $3, $4)
+ RETURNING *",
+ )
+ .await?;
+
+ // Create a new UUID v4
+ let uuid = Uuid::new_v4();
+
+ let branch = client
+ .query_opt(
+ &statement,
+ &[&uuid, &data.name, &data.repository_id, &data.head],
+ )
+ .await?
+ .map(|row| Branch::from_row_ref(&row).unwrap());
+
+ match branch {
+ Some(branch) => Ok(branch),
+ None => Err(AppError {
+ message: Some("Error creating a new branch".to_string()),
+ cause: Some("Unknown error".to_string()),
+ error_type: AppErrorType::DbError,
+ }),
+ }
+ }
}