fix new tr
This commit is contained in:
@@ -72,9 +72,12 @@ impl App {
|
||||
}
|
||||
|
||||
pub fn save_new_tr(&mut self) {
|
||||
self.next_tr_input();
|
||||
let tr = data_layer::upsert_transaction(&self.connection, self.new_transaction.clone());
|
||||
self.trx_table.add_tr(tr);
|
||||
self.new_transaction = Transaction::new_empty();
|
||||
self.current_input = String::new();
|
||||
self.selected_transaction_input = TransactionInput::Type;
|
||||
}
|
||||
|
||||
pub fn next_tr_input(&mut self) {
|
||||
|
||||
@@ -174,27 +174,32 @@ pub fn get_transaction_types(con: &Connection) -> Vec<TransactionType> {
|
||||
}
|
||||
|
||||
pub fn upsert_transaction(con: &Connection, tr: Transaction) -> Transaction {
|
||||
println!("{}", tr.get_desc());
|
||||
let query;
|
||||
if tr.get_id() == 0 {
|
||||
query = "INSERT INTO Transactions
|
||||
(account_id, type_id, amount, date, description)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
VALUES (:ac_id, :type_id, :amnt, :date, :desc)
|
||||
RETURNING id;";
|
||||
} else {
|
||||
query = "UPDATE Transactions
|
||||
SET account_id = ?, type_id = ?, amount = ?, date = ?, description = ?
|
||||
WHERE id = ? RETURNING id;";
|
||||
SET account_id = :ac_id, type_id = :type_id, amount = :amnt, date = :date, description = :desc
|
||||
WHERE id = :id RETURNING id;";
|
||||
}
|
||||
|
||||
let mut statement = con.prepare(query).unwrap();
|
||||
statement.bind((1, tr.get_account_id())).unwrap();
|
||||
statement.bind((2, tr.get_type().get_id())).unwrap();
|
||||
statement.bind((3, tr.get_amount())).unwrap();
|
||||
statement.bind((4, tr.get_date().timestamp())).unwrap();
|
||||
statement.bind((5, &tr.get_desc() as &str)).unwrap();
|
||||
statement.bind((":ac_id", tr.get_account_id())).unwrap();
|
||||
statement
|
||||
.bind((":type_id", tr.get_type().get_id()))
|
||||
.unwrap();
|
||||
statement.bind((":amnt", tr.get_amount())).unwrap();
|
||||
statement
|
||||
.bind((":date", tr.get_date().timestamp()))
|
||||
.unwrap();
|
||||
statement.bind((":desc", &tr.get_desc() as &str)).unwrap();
|
||||
|
||||
if tr.get_id() != 0 {
|
||||
statement.bind((6, tr.get_id())).unwrap();
|
||||
statement.bind((":id", tr.get_id())).unwrap();
|
||||
}
|
||||
|
||||
let id;
|
||||
@@ -216,9 +221,9 @@ pub fn upsert_transaction(con: &Connection, tr: Transaction) -> Transaction {
|
||||
}
|
||||
|
||||
pub fn get_transaction(con: &Connection, id: i64) -> Transaction {
|
||||
let query = "SELECT * FROM Transactions WHERE id = ?";
|
||||
let query = "SELECT * FROM Transactions WHERE id = :id";
|
||||
let mut statement = con.prepare(query).unwrap();
|
||||
statement.bind((1, id)).unwrap();
|
||||
statement.bind((":id", id)).unwrap();
|
||||
|
||||
if let Ok(State::Row) = statement.next() {
|
||||
return Transaction::new(
|
||||
|
||||
@@ -5,8 +5,8 @@ use ratatui::{
|
||||
style::{Color, Modifier, Style, palette::tailwind::SLATE},
|
||||
text::{Line, Text},
|
||||
widgets::{
|
||||
Block, Borders, HighlightSpacing, List, ListItem, Paragraph, Row, StatefulWidget, Table,
|
||||
Wrap,
|
||||
Block, Borders, Clear, HighlightSpacing, List, ListItem, Paragraph, Row, StatefulWidget,
|
||||
Table, Wrap,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -110,6 +110,7 @@ pub fn ui(frame: &mut Frame, app: &mut App) {
|
||||
.style(Style::default());
|
||||
|
||||
let area = centered_rect(50, 40, frame.area());
|
||||
frame.render_widget(Clear, area);
|
||||
frame.render_widget(popup, area);
|
||||
}
|
||||
|
||||
@@ -120,6 +121,7 @@ pub fn ui(frame: &mut Frame, app: &mut App) {
|
||||
.style(Style::default());
|
||||
|
||||
let area = centered_rect(50, 40, frame.area());
|
||||
frame.render_widget(Clear, area);
|
||||
frame.render_widget(popup, area);
|
||||
|
||||
let chunks = Layout::default()
|
||||
@@ -174,6 +176,7 @@ pub fn ui(frame: &mut Frame, app: &mut App) {
|
||||
.wrap(Wrap { trim: false });
|
||||
|
||||
let area = centered_rect(50, 20, frame.area());
|
||||
frame.render_widget(Clear, area);
|
||||
frame.render_widget(exit_paragraph, area);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user