Skip to content

Commit

Permalink
Move add transaction to db
Browse files Browse the repository at this point in the history
  • Loading branch information
will-lynas committed Oct 13, 2024
1 parent 99bb26f commit 1fc8d2c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,20 @@ impl<'a> DB<'a> {
.await
.unwrap();
}

pub async fn add_transaction(&self, transaction: Transaction) {
sqlx::query!(
r#"
INSERT INTO transactions (chatID, userID, description, amount)
VALUES (?1, ?2, ?3, ?4)
"#,
self.chat_id,
transaction.user_id,
transaction.description,
transaction.amount
)
.execute(self.pool)
.await
.unwrap();
}
}
23 changes: 10 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{
path::Path,
};

use db::DB;
use db::{
Transaction,
DB,
};
use dotenv::from_path;
use dptree::case;
use sqlx::sqlite::SqlitePool;
Expand Down Expand Up @@ -180,19 +183,13 @@ async fn receive_title(
let user = msg.from.unwrap();
let user_id = user.id.0 as i64;

sqlx::query!(
r#"
INSERT INTO transactions (chatID, userID, description, amount)
VALUES (?1, ?2, ?3, ?4)
"#,
chat_id,
let transaction = Transaction {
user_id,
title,
amount
)
.execute(&pool)
.await
.unwrap();
description: Some(title.into()),
amount: Some(amount),
};

DB::new(&pool, chat_id).add_transaction(transaction).await;

bot.send_message(
msg.chat.id,
Expand Down

0 comments on commit 1fc8d2c

Please sign in to comment.