Merge branch 'GenericGetDBData'
This commit is contained in:
commit
3f833429c8
|
@ -16,40 +16,37 @@ namespace CryptoCalc
|
|||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
|
||||
using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(data.DBTableName));
|
||||
cnn.Execute($"insert into {data.DBTableName} {data.DBSaveDataString}", data);
|
||||
Debug.WriteLine($"Saved {data.DBTableName} DB data");
|
||||
}
|
||||
|
||||
//public static List<T> _LoadTransactions()
|
||||
//{
|
||||
// using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("RawData"));
|
||||
// var output = _connection.Query<T>("select * from RawData", new DynamicParameters());
|
||||
// Debug.WriteLine("Loaded DB data");
|
||||
// return output.ToList();
|
||||
//}
|
||||
|
||||
|
||||
public static List<Transaction> LoadTransactions()
|
||||
public class GenericDB<T> where T : DBClasses
|
||||
{
|
||||
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("Transactions"));
|
||||
IEnumerable<Transaction> output = _connection.Query<Transaction>("select * from Transactions", new DynamicParameters());
|
||||
Debug.WriteLine("Loaded DB data");
|
||||
return output.ToList();
|
||||
|
||||
public IEnumerable<T> LoadAllData(string tableName)
|
||||
{
|
||||
if (tableName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(tableName));
|
||||
}
|
||||
|
||||
|
||||
public static List<Transaction> LoadTransactionsOfCurrency(string currency)
|
||||
{
|
||||
if (currency is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(currency));
|
||||
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(tableName));
|
||||
return _connection.Query<T>($"select * from {tableName}", new DynamicParameters());
|
||||
}
|
||||
|
||||
Debug.WriteLine($"Fetching all {currency} transactions from Database");
|
||||
using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("Transactions"));
|
||||
return connection.Query<Transaction>($"select * from Transactions where Currency = '{ currency.ToUpper() }'")
|
||||
.ToList();
|
||||
public IEnumerable<T> QueryData(string tableName, string query)
|
||||
{
|
||||
if (tableName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(query));
|
||||
}
|
||||
|
||||
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(tableName));
|
||||
return _connection.Query<T>($"{query}", new DynamicParameters());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,12 @@ namespace CryptoCalc
|
|||
public partial class MainWindow : Window
|
||||
{
|
||||
private readonly Random rand = new();
|
||||
private List<Transaction> transactions = new();
|
||||
|
||||
private DBInteraction.GenericDB<Transaction> transactionDB = new();
|
||||
private DBInteraction.GenericDB<Wallet> walletDB = new();
|
||||
|
||||
private Transaction genericT = new();
|
||||
private Wallet genericW = new();
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
|
@ -17,32 +22,34 @@ namespace CryptoCalc
|
|||
|
||||
private void saveButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Transaction dummy = DummyTransaction();
|
||||
DBInteraction.SaveData(ref dummy);
|
||||
genericT = DummyTransaction();
|
||||
DBInteraction.SaveData(ref genericT);
|
||||
}
|
||||
private void saveWalletButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Wallet dummy = DummyWallet();
|
||||
DBInteraction.SaveData(ref dummy);
|
||||
genericW = DummyWallet();
|
||||
DBInteraction.SaveData(ref genericW);
|
||||
}
|
||||
|
||||
private void saveButtonFromInput_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Transaction rd = new(inputCurrency.Text, Convert.ToSingle(inputAmount.Text), inputType.Text);
|
||||
DBInteraction.SaveData(ref rd);
|
||||
genericT = new(inputCurrency.Text, Convert.ToSingle(inputAmount.Text), inputType.Text);
|
||||
DBInteraction.SaveData(ref genericT);
|
||||
}
|
||||
|
||||
private void readButton_click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
transactions = DBInteraction.LoadTransactions();
|
||||
foreach (Transaction x in transactions)
|
||||
IEnumerable <Transaction> transactions = transactionDB.LoadAllData(genericT.DBTableName);
|
||||
foreach (Transaction t in transactions)
|
||||
{
|
||||
Debug.WriteLine($"{x.GetLocalTimeFromUnixTime(x.UnixTime)} *** {x.Currency} - {x.Amount}");
|
||||
Debug.WriteLine($"{t.GetLocalTimeFromUnixTime(t.UnixTime)} *** {t.Currency} - {t.Amount}");
|
||||
}
|
||||
}
|
||||
private void searchButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
transactions = DBInteraction.LoadTransactionsOfCurrency(currencyText.Text);
|
||||
string query = $"SELECT * from {genericT.DBTableName} WHERE {nameof(genericT.Currency)}=\'{currencyText.Text}\'";
|
||||
IEnumerable<Transaction> transactions = transactionDB.QueryData(genericT.DBTableName, query);
|
||||
//transactions = DBInteraction.LoadTransactionsOfCurrency(currencyText.Text);
|
||||
transactionsFoundListBox.Items.Clear();
|
||||
foreach (Transaction x in transactions)
|
||||
{
|
||||
|
@ -50,6 +57,12 @@ namespace CryptoCalc
|
|||
}
|
||||
}
|
||||
|
||||
//private IEnumerable<Transaction> ReadTransactionFromDB()
|
||||
//{
|
||||
// Transaction t = new();
|
||||
// return transactionDB.LoadAllData(t.DBTableName);
|
||||
//}
|
||||
|
||||
private Transaction DummyTransaction()
|
||||
{
|
||||
Transaction t = new();
|
||||
|
|
Loading…
Reference in New Issue