Skip to content

Commit

Permalink
chore: add tx template in rust example
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed May 12, 2024
1 parent 0ff7754 commit fb26491
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cala-ledger/src/tx_template/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ mod tests {
.code("CODE")
.tx_input(
NewTxInput::builder()
.effective("date('2022-11-01')")
.effective("DATE('2022-11-01')")
.journal_id(format!("'{journal_id}'"))
.build()
.unwrap(),
Expand Down
32 changes: 16 additions & 16 deletions cala-server/src/graphql/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,29 @@ impl From<cala_ledger::tx_template::ParamDefinition> for ParamDefinition {
impl From<cala_ledger::tx_template::ParamDataType> for ParamDataType {
fn from(value: cala_ledger::tx_template::ParamDataType) -> Self {
match value {
cala_ledger::tx_template::ParamDataType::STRING => ParamDataType::STRING,
cala_ledger::tx_template::ParamDataType::INTEGER => ParamDataType::INTEGER,
cala_ledger::tx_template::ParamDataType::DECIMAL => ParamDataType::DECIMAL,
cala_ledger::tx_template::ParamDataType::BOOLEAN => ParamDataType::BOOLEAN,
cala_ledger::tx_template::ParamDataType::UUID => ParamDataType::UUID,
cala_ledger::tx_template::ParamDataType::DATE => ParamDataType::DATE,
cala_ledger::tx_template::ParamDataType::TIMESTAMP => ParamDataType::TIMESTAMP,
cala_ledger::tx_template::ParamDataType::JSON => ParamDataType::JSON,
cala_ledger::tx_template::ParamDataType::STRING => ParamDataType::String,
cala_ledger::tx_template::ParamDataType::INTEGER => ParamDataType::Integer,
cala_ledger::tx_template::ParamDataType::DECIMAL => ParamDataType::Decimal,
cala_ledger::tx_template::ParamDataType::BOOLEAN => ParamDataType::Boolean,
cala_ledger::tx_template::ParamDataType::UUID => ParamDataType::Uuid,
cala_ledger::tx_template::ParamDataType::DATE => ParamDataType::Date,
cala_ledger::tx_template::ParamDataType::TIMESTAMP => ParamDataType::Timestamp,
cala_ledger::tx_template::ParamDataType::JSON => ParamDataType::Json,
}
}
}

impl From<ParamDataType> for cala_ledger::tx_template::ParamDataType {
fn from(value: ParamDataType) -> Self {
match value {
ParamDataType::STRING => cala_ledger::tx_template::ParamDataType::STRING,
ParamDataType::INTEGER => cala_ledger::tx_template::ParamDataType::INTEGER,
ParamDataType::DECIMAL => cala_ledger::tx_template::ParamDataType::DECIMAL,
ParamDataType::BOOLEAN => cala_ledger::tx_template::ParamDataType::BOOLEAN,
ParamDataType::UUID => cala_ledger::tx_template::ParamDataType::UUID,
ParamDataType::DATE => cala_ledger::tx_template::ParamDataType::DATE,
ParamDataType::TIMESTAMP => cala_ledger::tx_template::ParamDataType::TIMESTAMP,
ParamDataType::JSON => cala_ledger::tx_template::ParamDataType::JSON,
ParamDataType::String => cala_ledger::tx_template::ParamDataType::STRING,
ParamDataType::Integer => cala_ledger::tx_template::ParamDataType::INTEGER,
ParamDataType::Decimal => cala_ledger::tx_template::ParamDataType::DECIMAL,
ParamDataType::Boolean => cala_ledger::tx_template::ParamDataType::BOOLEAN,
ParamDataType::Uuid => cala_ledger::tx_template::ParamDataType::UUID,
ParamDataType::Date => cala_ledger::tx_template::ParamDataType::DATE,
ParamDataType::Timestamp => cala_ledger::tx_template::ParamDataType::TIMESTAMP,
ParamDataType::Json => cala_ledger::tx_template::ParamDataType::JSON,
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions cala-server/src/graphql/tx_template.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use async_graphql::*;
use serde::{Deserialize, Serialize};

use super::primitives::*;

Expand All @@ -23,16 +22,16 @@ pub(super) struct ParamDefinition {
pub description: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Enum, Copy)]
#[derive(Clone, Debug, PartialEq, Eq, Enum, Copy)]
pub enum ParamDataType {
STRING,
INTEGER,
DECIMAL,
BOOLEAN,
UUID,
DATE,
TIMESTAMP,
JSON,
String,
Integer,
Decimal,
Boolean,
Uuid,
Date,
Timestamp,
Json,
}

#[derive(SimpleObject)]
Expand Down
45 changes: 43 additions & 2 deletions examples/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Context;
use std::fs;

use cala_ledger::{account::*, journal::*, migrate::IncludeMigrations, query::*, *};
use cala_ledger::{
account::*, journal::*, migrate::IncludeMigrations, query::*, tx_template::*, *,
};

pub fn store_server_pid(cala_home: &str, pid: u32) -> anyhow::Result<()> {
create_cala_dir(cala_home)?;
Expand Down Expand Up @@ -52,7 +54,46 @@ async fn main() -> anyhow::Result<()> {
.description("description")
.build()?;
let journal = cala.journals().create(new_journal).await?;
println!("journal_id: {}", journal.id());
let journal_id = journal.id();
println!("journal_id: {}", journal_id);

let tx_input = NewTxInput::builder()
.journal_id(format!("UUID('{}')", journal_id))
.effective("DATE('2022-11-01')")
.build()?;
let entries = vec![
NewEntryInput::builder()
.entry_type("'TEST_DR'")
.account_id("param.recipient")
.layer("'SETTLED'")
.direction("'DEBIT'")
.units("1290")
.currency("'BTC'")
.build()
.unwrap(),
NewEntryInput::builder()
.entry_type("'TEST_CR'")
.account_id("param.sender")
.layer("'SETTLED'")
.direction("'CREDIT'")
.units("1290")
.currency("'BTC'")
.build()
.unwrap(),
];
let tx_template_id = TxTemplateId::new();

let new_tx_template = NewTxTemplate::builder()
.id(tx_template_id)
.code("CODE")
.tx_input(tx_input)
.entries(entries)
.build()
.unwrap();
let tx_template = cala.tx_templates().create(new_tx_template).await?;
println!("tx_template_id: {}", tx_template.id());
let code = tx_template.into_values().code;
println!("tx_template_code: {}", code);

loop {
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
Expand Down

0 comments on commit fb26491

Please sign in to comment.