travail sur add transaction
This commit is contained in:
90
src/app.rs
90
src/app.rs
@@ -1,3 +1,4 @@
|
||||
use chrono::{DateTime, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
|
||||
use sqlite::Connection;
|
||||
|
||||
use crate::{data_layer, entities::*, enums::*};
|
||||
@@ -9,6 +10,8 @@ pub struct App {
|
||||
pub trx_table: TrxTable,
|
||||
pub new_account: Account,
|
||||
pub new_transaction: Transaction,
|
||||
pub selected_transaction_input: TransactionInput,
|
||||
pub current_input: String,
|
||||
pub connection: Connection,
|
||||
}
|
||||
|
||||
@@ -31,6 +34,8 @@ impl App {
|
||||
trx_table: TrxTable::new(),
|
||||
new_account: Account::new(0, String::new(), AccountType::Cash),
|
||||
new_transaction: Transaction::new_empty(),
|
||||
selected_transaction_input: TransactionInput::Type,
|
||||
current_input: String::new(),
|
||||
connection: con,
|
||||
};
|
||||
}
|
||||
@@ -71,4 +76,89 @@ impl App {
|
||||
self.trx_table.add_tr(tr);
|
||||
self.new_transaction = Transaction::new_empty();
|
||||
}
|
||||
|
||||
pub fn next_tr_input(&mut self) {
|
||||
match self.selected_transaction_input {
|
||||
TransactionInput::Type => {
|
||||
self.selected_transaction_input = TransactionInput::Amount;
|
||||
self.new_transaction.set_type(
|
||||
self.current_input
|
||||
.parse::<i64>()
|
||||
.expect("Failed to parse data"),
|
||||
&self.connection,
|
||||
);
|
||||
}
|
||||
TransactionInput::Amount => {
|
||||
self.selected_transaction_input = TransactionInput::Date;
|
||||
self.new_transaction.set_amount(
|
||||
self.current_input
|
||||
.parse::<f64>()
|
||||
.expect("Failed to parse data"),
|
||||
);
|
||||
}
|
||||
TransactionInput::Date => {
|
||||
self.selected_transaction_input = TransactionInput::Description;
|
||||
let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d")
|
||||
.expect("Faile to parse data");
|
||||
let time = NaiveTime::from_hms_opt(5, 0, 0).unwrap(); // to account for my local datetime
|
||||
self.new_transaction
|
||||
.set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time)));
|
||||
}
|
||||
TransactionInput::Description => {
|
||||
self.new_transaction.set_desc(self.current_input.clone())
|
||||
}
|
||||
}
|
||||
self.set_current_input();
|
||||
}
|
||||
pub fn previous_tr_input(&mut self) {
|
||||
match self.selected_transaction_input {
|
||||
TransactionInput::Type => {
|
||||
self.new_transaction.set_type(
|
||||
self.current_input
|
||||
.parse::<i64>()
|
||||
.expect("Failed to parse data"),
|
||||
&self.connection,
|
||||
);
|
||||
}
|
||||
TransactionInput::Amount => {
|
||||
self.selected_transaction_input = TransactionInput::Type;
|
||||
self.new_transaction.set_amount(
|
||||
self.current_input
|
||||
.parse::<f64>()
|
||||
.expect("Failed to parse data"),
|
||||
);
|
||||
}
|
||||
TransactionInput::Date => {
|
||||
self.selected_transaction_input = TransactionInput::Amount;
|
||||
let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d")
|
||||
.expect("Faile to parse data");
|
||||
let time = NaiveTime::from_hms_opt(5, 0, 0).unwrap(); // to account for my local datetime
|
||||
self.new_transaction
|
||||
.set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time)));
|
||||
}
|
||||
TransactionInput::Description => {
|
||||
self.selected_transaction_input = TransactionInput::Date;
|
||||
self.new_transaction.set_desc(self.current_input.clone());
|
||||
}
|
||||
}
|
||||
self.set_current_input();
|
||||
}
|
||||
fn set_current_input(&mut self) {
|
||||
match self.selected_transaction_input {
|
||||
TransactionInput::Type => {
|
||||
self.current_input = self.new_transaction.get_type().get_id().to_string()
|
||||
}
|
||||
TransactionInput::Amount => {
|
||||
self.current_input = self.new_transaction.get_amount().to_string()
|
||||
}
|
||||
TransactionInput::Date => {
|
||||
self.current_input = self
|
||||
.new_transaction
|
||||
.get_date()
|
||||
.format("%Y-%m-%d")
|
||||
.to_string()
|
||||
}
|
||||
TransactionInput::Description => self.current_input = self.new_transaction.get_desc(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user