summaryrefslogtreecommitdiff
path: root/src/branch/models.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-17 23:19:06 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-17 23:19:06 +0100
commit9c9921d9151f813e30aed42cf2b0ae3ae775cc52 (patch)
treed79cc4ce13e3b02bcbd5584e1066f720dc7b6eb0 /src/branch/models.rs
parent124048f2b7052c47816ce40d8a88a7e3f538fd84 (diff)
feat: branch CRUD
Diffstat (limited to 'src/branch/models.rs')
-rw-r--r--src/branch/models.rs105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/branch/models.rs b/src/branch/models.rs
new file mode 100644
index 0000000..b631162
--- /dev/null
+++ b/src/branch/models.rs
@@ -0,0 +1,105 @@
+use crate::db::get_client;
+use crate::errors::{AppError, AppErrorType};
+
+use deadpool_postgres::Pool;
+use serde::{Deserialize, Serialize};
+use tokio_pg_mapper::FromTokioPostgresRow;
+use tokio_pg_mapper_derive::PostgresMapper;
+use uuid::Uuid;
+
+#[derive(Serialize, Deserialize, PostgresMapper)]
+#[pg_mapper(table = "branch")]
+/// Branch model
+pub struct Branch {
+ pub id: Uuid,
+ 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> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let statement = client.prepare("SELECT * FROM branch").await?;
+
+ let branches = client
+ .query(&statement, &[])
+ .await?
+ .iter()
+ .map(|row| Branch::from_row_ref(row).unwrap())
+ .collect::<Vec<Branch>>();
+
+ Ok(branches)
+ }
+
+ /// Find a branch with an `id` equals to an Uuid element
+ pub async fn find(pool: Pool, id: &Uuid) -> Result<Branch, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let statement =
+ client.prepare("SELECT * FROM branch WHERE id = $1").await?;
+
+ let branch = client
+ .query_opt(&statement, &[&id])
+ .await?
+ .map(|row| Branch::from_row_ref(&row).unwrap());
+
+ match branch {
+ Some(branch) => Ok(branch),
+ None => Err(AppError {
+ error_type: AppErrorType::NotFoundError,
+ cause: None,
+ message: Some("Branch not found".to_string()),
+ }),
+ }
+ }
+
+ /// Find all branches of a repository
+ pub async fn find_by_repo(
+ pool: Pool,
+ repo: &Uuid,
+ ) -> Result<Vec<Branch>, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let statement = client
+ .prepare("SELECT * FROM branch WHERE repository_id=$1")
+ .await?;
+
+ let branches = client
+ .query(&statement, &[&repo])
+ .await?
+ .iter()
+ .map(|row| Branch::from_row_ref(row).unwrap())
+ .collect::<Vec<Branch>>();
+
+ Ok(branches)
+ }
+
+ /// Find a branch and delete it, but before check if "Authorization"
+ /// matches with SECRET_KEY
+ pub async fn delete(pool: Pool, id: &Uuid) -> Result<Branch, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let statement = client
+ .prepare(
+ "
+ DELETE FROM branch
+ WHERE id=$1
+ RETURNING *
+ ",
+ )
+ .await?;
+
+ let branch = client
+ .query_opt(&statement, &[&id])
+ .await?
+ .map(|row| Branch::from_row_ref(&row).unwrap());
+
+ match branch {
+ Some(branch) => Ok(branch),
+ None => Err(AppError {
+ error_type: AppErrorType::NotFoundError,
+ cause: None,
+ message: Some("Branch not found".to_string()),
+ }),
+ }
+ }
+}