Skip to content

Commit

Permalink
Merge pull request #291 from systemaccounting/290-db-traits
Browse files Browse the repository at this point in the history
290 db traits
  • Loading branch information
mxfactorial authored Sep 18, 2023
2 parents 1919269 + 656062a commit c55b3d6
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 96 deletions.
1 change: 1 addition & 0 deletions .github/workflows/dev-rule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
paths:
- 'services/rule/**'
- 'crates/**'
branches-ignore:
- 'master'

Expand Down
55 changes: 34 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/pg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
bb8 = "0.8.0"
bb8-postgres = "0.8.1"
tokio = "1.26.0"
async-trait = "0.1.73"
tokio-postgres = {version = "0.7.7", features = ["with-chrono-0_4"]}
types = { path = "../types" }
sqls = { path = "../sqls" }
91 changes: 22 additions & 69 deletions crates/pg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use std::env;

use async_trait::async_trait;
use bb8::{Pool, PooledConnection};
use bb8_postgres::PostgresConnectionManager;
use sqls::{
select_account_profile_by_account, select_account_profiles_by_db_cr_accounts, select_approvers,
select_account_profiles_by_db_cr_accounts, select_approvers,
select_rule_instance_by_type_role_account, select_rule_instance_by_type_role_state,
};
use std::error::Error;
use std::{env, error::Error};
use tokio_postgres::{types::ToSql, NoTls};
use types::{
account::{AccountProfile, AccountProfiles},
account::{AccountProfiles, AccountStore},
account_role::AccountRole,
rule::RuleInstances,
rule::{RuleInstanceStore, RuleInstances},
};

pub struct DB;
Expand Down Expand Up @@ -40,7 +39,6 @@ impl DB {
#[cfg(test)]
mod tests {
use super::*;

use std::env;

#[test]
Expand Down Expand Up @@ -80,25 +78,9 @@ impl ConnectionPool {

pub struct DatabaseConnection(PooledConnection<'static, PostgresConnectionManager<NoTls>>);

impl DatabaseConnection {
pub async fn get_account_profile(
&self,
account: String,
) -> Result<AccountProfile, Box<dyn Error>> {
let row = self
.0
.query_one(select_account_profile_by_account().as_str(), &[&account])
.await;
match row {
Err(row) => Err(Box::new(row)),
Ok(row) => {
let account_profile = AccountProfile::from_row(row);
Ok(account_profile)
}
}
}

pub async fn get_account_profiles(
#[async_trait]
impl AccountStore for &DatabaseConnection {
async fn get_account_profiles(
&self,
accounts: Vec<String>,
) -> Result<AccountProfiles, Box<dyn Error>> {
Expand All @@ -125,23 +107,7 @@ impl DatabaseConnection {
}
}

pub async fn get_db_cr_account_profiles(
&self,
debitor: String,
creditor: String,
) -> AccountProfiles {
let rows = self
.0
.query(
select_account_profiles_by_db_cr_accounts().as_str(),
&[&debitor, &creditor],
)
.await
.unwrap(); // todo: handle error
types::account::AccountProfiles::from_rows(rows)
}

pub async fn get_approvers_for_account(&self, account: String) -> Vec<String> {
async fn get_approvers_for_account(&self, account: String) -> Vec<String> {
let rows = self
.0
.query(select_approvers().as_str(), &[&account])
Expand All @@ -150,56 +116,43 @@ impl DatabaseConnection {
let account_approvers: Vec<String> = rows.into_iter().map(|row| row.get(0)).collect();
account_approvers
}
}

pub async fn get_approvers_for_accounts(&self, accounts: Vec<String>) -> Vec<String> {
let mut params: Vec<&(dyn ToSql + Sync)> = Vec::new();

for a in accounts.iter() {
params.push(a)
}

let rows = self
.0
.query(select_approvers().as_str(), &params[..])
.await
.unwrap(); // todo: handle error
let account_approvers: Vec<String> = rows.into_iter().map(|row| row.get(0)).collect();
account_approvers
}

pub async fn get_approval_rule_instances(
#[async_trait]
impl RuleInstanceStore for &DatabaseConnection {
async fn get_profile_state_rule_instances(
&self,
account_role: AccountRole,
account: String,
state_name: String,
) -> RuleInstances {
let rows = self
.0
.query(
select_rule_instance_by_type_role_account().as_str(),
&[&"approval", &account_role, &account],
select_rule_instance_by_type_role_state().as_str(),
&[&"transaction_item", &account_role, &state_name],
)
.await
.unwrap(); // todo: handle error
RuleInstances::from_rows(rows)
}

pub async fn get_profile_state_rule_instances(
async fn get_rule_instances_by_type_role_account(
&self,
account_role: AccountRole,
state_name: String,
account: String,
) -> RuleInstances {
let rows = self
.0
.query(
select_rule_instance_by_type_role_state().as_str(),
&[&"transaction_item", &account_role, &state_name],
select_rule_instance_by_type_role_account().as_str(),
&[&"transaction_item", &account_role, &account],
)
.await
.unwrap(); // todo: handle error
RuleInstances::from_rows(rows)
}

pub async fn get_rule_instances_by_type_role_account(
async fn get_approval_rule_instances(
&self,
account_role: AccountRole,
account: String,
Expand All @@ -208,7 +161,7 @@ impl DatabaseConnection {
.0
.query(
select_rule_instance_by_type_role_account().as_str(),
&[&"transaction_item", &account_role, &account],
&[&"approval", &account_role, &account],
)
.await
.unwrap(); // todo: handle error
Expand Down
1 change: 1 addition & 0 deletions crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ serde_with = {version = "2.2.0", features = ["chrono_0_4"]}
strum = {version = "0.24.1", features = ["derive"]}
strum_macros = "0.24.3"
tokio-postgres = {version = "0.7.7", features = ["with-chrono-0_4"]}
async-trait = "0.1.73"
Loading

0 comments on commit c55b3d6

Please sign in to comment.