Refactored DB classes

This commit is contained in:
Stedd 2022-01-15 17:07:16 +01:00
parent 934ad28fa4
commit 8a10fbbb5f
6 changed files with 277 additions and 215 deletions

View File

@ -12,7 +12,7 @@ namespace CryptoCalc
public class DBInteraction
{
public static void SaveData<T>(ref T _data) where T : IDBClasses
public static void SaveData<T>(ref T _data) where T : DB_Classes
{
if (_data is null)
{
@ -23,42 +23,29 @@ namespace CryptoCalc
Debug.WriteLine($"Saved {_data.DBTableName} DB data");
}
//public static void SaveTransaction(RawData _data)
//public static List<T> _LoadTransactions()
//{
// if (_data is null)
// {
// throw new ArgumentNullException(nameof(_data));
// }
// using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(_data.DBTableName));
// cnn.Execute($"insert into {_data.DBTableName} (Date_Year, Date_Month, Date_Day, Time_Hour, Time_Minute, Time_Second, DateTimeString, CryptoCurrency, Amount, TransactionType, Service, Comment) values (@Date_Year, @Date_Month, @Date_Day, @Time_Hour, @Time_Minute, @Time_Second, @DateTimeString, @CryptoCurrency, @Amount, @TransactionType, @Service, @Comment)", _data);
// Debug.WriteLine("Saved DB data");
//}
//public static void SaveNewWallet(Wallet _data)
//{
// if (_data is null)
// {
// throw new ArgumentNullException(nameof(_data));
// }
// using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(_data.DBTableName));
// cnn.Execute($"insert into {_data.DBTableName} (wCreationYear, wCreationMonth, wCreationDay, wPlatform, wName, wCurrency, wBalance, wDefaultWallet, wNotes) values (@wCreationYear, @wCreationMonth, @wCreationDay, @wPlatform, @wName, @wCurrency, @wBalance, @wDefaultWallet, @wNotes)", _data);
// //cnn.Execute("insert into RawData (wCreationYear, wCreationMonth, wCreationDay, wPlatform, wName, wCurrency, wBalance, wDefaultWallet, wNotes) values ()", _walletData);
// Debug.WriteLine("Saved Wallet");
// 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<RawData> LoadTransactions()
public static List<Transaction> LoadTransactions()
{
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("RawData"));
var output = _connection.Query<RawData>("select * from RawData", new DynamicParameters());
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("Transactions"));
var output = _connection.Query<Transaction>("select * from Transactions", new DynamicParameters());
Debug.WriteLine("Loaded DB data");
return output.ToList();
}
public static List<RawData> LoadTransactionsOfCurrency(string _currency)
public static List<Transaction> LoadTransactionsOfCurrency(string _currency)
{
Debug.WriteLine($"Fetching all {_currency} transactions from Database");
using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("RawData"));
return connection.Query<RawData>($"select * from RawData where CryptoCurrency = '{ _currency }'").ToList();
using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("Transactions"));
return connection.Query<Transaction>($"select * from Transactions where Currency = '{ _currency.ToUpper() }'").ToList();
}
}
}

View File

@ -1,105 +1,206 @@
using System;
using System.Text.RegularExpressions;
namespace CryptoCalc
{
public interface IDBClasses
{
string dbVariables { get; set; }
string variables { get; set; }
string DBTableName { get; }
string DBVariables { get; }
}
public class DB_Classes
public class DB_Classes : IDBClasses
{
public class Transaction : IDBClasses
{
#region Constructors
public virtual string dbVariables { get; set ; }
public virtual string variables { get; set; }
public Transaction() { }
public virtual string DBTableName => throw new NotImplementedException();
public string DBVariables => $"({variables}) values ({dbVariables})";
public Transaction(string _currency, float _amount, string _type, string _feeCurrency, float _feeAmount)
{
tYear = DateTime.Now.Year;
tMonth = DateTime.Now.Month;
tDay = DateTime.Now.Day;
tHour = DateTime.Now.Hour;
tMinute = DateTime.Now.Minute;
tSecond = DateTime.Now.Second;
tDateTimeString = DateTime.Now.ToString();
tCurrency = _currency;
tAmount = _amount;
tTransactionType = _type;
tFeeCurrency = _feeCurrency;
tFeeAmount = _feeAmount;
tPlatform = "";
tNote = "";
public int Index { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public int Hour { get; set; }
public int Minute { get; set; }
public int Second { get; set; }
public string DateTimeString { get; set; }
}
public class Transaction : DB_Classes
{
#region Publics
public override string dbVariables { get => base.dbVariables; set => base.dbVariables = value; }
public override string variables { get => base.variables; set => base.variables = value; }
public override string DBTableName => "Transactions";
public string Currency { get; set; }
public float Amount { get; set; }
public string TransactionType { get; set; }
public string FeeCurrency { get; set; }
public float FeeAmount { get; set; }
public string Platform { get; set; }
public string Note { get; set; }
#endregion
#region Publics
public string DBTableName => "Transactions";
#region Constructors
public string DBVariables => "(Date_Year, Date_Month, Date_Day, Time_Hour, Time_Minute, Time_Second, DateTimeString, CryptoCurrency, Amount, TransactionType, Service, Comment) values (@Date_Year, @Date_Month, @Date_Day, @Time_Hour, @Time_Minute, @Time_Second, @DateTimeString, @CryptoCurrency, @Amount, @TransactionType, @Service, @Comment)";
public Transaction() { SetDBStrings(); }
public int Index { get; set; }
public int tYear { get; set; }
public int tMonth { get; set; }
public int tDay { get; set; }
public int tHour { get; set; }
public int tMinute { get; set; }
public int tSecond { get; set; }
public string tDateTimeString { get; set; }
public string tCurrency { get; set; }
public float tAmount { get; set; }
public string tTransactionType { get; set; }
public string tFeeCurrency { get; set; }
public float tFeeAmount { get; set; }
public string tPlatform { get; set; }
public string tNote { get; set; }
public Transaction(string _currency, float _amount, string _type)
{
Year = DateTime.Now.Year;
Month = DateTime.Now.Month;
Day = DateTime.Now.Day;
Hour = DateTime.Now.Hour;
Minute = DateTime.Now.Minute;
Second = DateTime.Now.Second;
DateTimeString = DateTime.Now.ToString();
Currency = _currency;
Amount = _amount;
TransactionType = _type;
Platform = "";
Note = "";
SetDBStrings();
}
public Transaction(string _currency, float _amount, string _type, string _feeCurrency, float _feeAmount)
{
Year = DateTime.Now.Year;
Month = DateTime.Now.Month;
Day = DateTime.Now.Day;
Hour = DateTime.Now.Hour;
Minute = DateTime.Now.Minute;
Second = DateTime.Now.Second;
DateTimeString = DateTime.Now.ToString();
Currency = _currency;
Amount = _amount;
TransactionType = _type;
FeeCurrency = _feeCurrency;
FeeAmount = _feeAmount;
Platform = "";
Note = "";
SetDBStrings();
}
#endregion
#region Functions
public string FullInfo => $"{ Index.ToString() } { tDateTimeString } { tCurrency } { tAmount } { tTransactionType }";
void SetDBStrings()
{
dbVariables =
$"@{nameof(Year)}, " +
$" @{nameof(Month)}," +
$" @{nameof(Day)}," +
$" @{nameof(Hour)}," +
$" @{nameof(Minute)}," +
$" @{nameof(Second)}," +
$" @{nameof(DateTimeString)}," +
$" @{nameof(Currency)}," +
$" @{nameof(Amount)}," +
$" @{nameof(TransactionType)}," +
$" @{nameof(FeeCurrency)}," +
$" @{nameof(FeeAmount)}," +
$" @{nameof(Platform)}," +
$" @{nameof(Note)}";
variables = Regex.Replace(dbVariables, "@", "");
}
public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";
#endregion
}
public class RawData : IDBClasses
public class Wallet : DB_Classes
{
#region Contructors
public RawData() { }
public RawData(string _currency, float _amount, string _type)
#region Publics
public override string dbVariables { get => base.dbVariables; set => base.dbVariables = value; }
public override string variables { get => base.variables; set => base.variables = value; }
public override string DBTableName => "Wallets";
public int CreationYear { get; set; }
public int CreationMonth { get; set; }
public int CreationDay { get; set; }
public string Platform { get; set; }
public string Name { get; set; }
public string Currency { get; set; }
public float Balance { get; set; }
public int DefaultWallet { get; set; }
public string Notes { get; set; }
#endregion
#region Constructors
public Wallet() { SetDBStrings(); }
public Wallet(int _year, int _month, int _day, string _platform, string _name, string _currency, float _balance, int _default, string _note)
{
Date_Year = DateTime.Now.Year;
Date_Month = DateTime.Now.Month;
Date_Day = DateTime.Now.Day;
Time_Hour = DateTime.Now.Hour;
Time_Minute = DateTime.Now.Minute;
Time_Second = DateTime.Now.Second;
DateTimeString = DateTime.Now.ToString();
CryptoCurrency = _currency;
Amount = _amount;
TransactionType = _type;
Service = "";
Comment = "";
CreationYear = _year;
CreationMonth = _month;
CreationDay = _day;
Platform = _platform;
Name = _name;
Currency = _currency;
Balance = _balance;
DefaultWallet = _default;
Notes = _note;
SetDBStrings();
}
#endregion
#region Functions
void SetDBStrings()
{
dbVariables =
$"@{nameof(Year)}, " +
$" @{nameof(Month)}," +
$" @{nameof(Day)}," +
$" @{nameof(Hour)}," +
$" @{nameof(Minute)}," +
$" @{nameof(Second)}," +
$" @{nameof(DateTimeString)}," +
$" @{nameof(CreationYear)}," +
$" @{nameof(CreationMonth)}," +
$" @{nameof(CreationDay)}," +
$" @{nameof(Platform)}," +
$" @{nameof(Name)}," +
$" @{nameof(Currency)}," +
$" @{nameof(Balance)}," +
$" @{nameof(DefaultWallet)}," +
$" @{nameof(Notes)}";
variables = Regex.Replace(dbVariables, "@", "");
}
//public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";
#endregion
}
/*
public class RawData : DB_Classes
{
#region Publics
public string DBTableName => "RawData";
public string DBVariables => "(Date_Year, Date_Month, Date_Day, Time_Hour, Time_Minute, Time_Second, DateTimeString, CryptoCurrency, Amount, TransactionType, Service, Comment) values (@Date_Year, @Date_Month, @Date_Day, @Time_Hour, @Time_Minute, @Time_Second, @DateTimeString, @CryptoCurrency, @Amount, @TransactionType, @Service, @Comment)";
public int Index { get; set; }
public int Date_Year { get; set; }
public int Date_Month { get; set; }
public int Date_Day { get; set; }
public int Time_Hour { get; set; }
public int Time_Minute { get; set; }
public int Time_Second { get; set; }
public string DateTimeString { get; set; }
public override string DBTableName => "RawData";
//public override string DBVariables => "(Date_Year, Date_Month, Date_Day, Time_Hour, Time_Minute, Time_Second, DateTimeString, CryptoCurrency, Amount, TransactionType, Service, Comment) values (@Date_Year, @Date_Month, @Date_Day, @Time_Hour, @Time_Minute, @Time_Second, @DateTimeString, @CryptoCurrency, @Amount, @TransactionType, @Service, @Comment)";
public string CryptoCurrency { get; set; }
public float Amount { get; set; }
public string TransactionType { get; set; }
@ -108,79 +209,52 @@ namespace CryptoCalc
#endregion
#region Contructors
public RawData() { }
public RawData(string _currency, float _amount, string _type)
{
Year = DateTime.Now.Year;
Month = DateTime.Now.Month;
Day = DateTime.Now.Day;
Hour = DateTime.Now.Hour;
Minute = DateTime.Now.Minute;
Second = DateTime.Now.Second;
DateTimeString = DateTime.Now.ToString();
CryptoCurrency = _currency;
Amount = _amount;
TransactionType = _type;
Service = "";
Comment = "";
dbVariables =
$"@{nameof(Year)}, " +
$" @{nameof(Month)}," +
$" @{nameof(Day)}," +
$" @{nameof(Hour)}," +
$" @{nameof(Minute)}," +
$" @{nameof(Second)}," +
$" @{nameof(DateTimeString)}," +
$" @{nameof(tCurrency)}," +
$" @{nameof(tAmount)}," +
$" @{nameof(tTransactionType)}," +
$" @{nameof(tFeeCurrency)}," +
$" @{nameof(tFeeAmount)}," +
$" @{nameof(tPlatform)}," +
$" @{nameof(tNote)}";
variables = Regex.Replace(dbVariables, "@", "");
}
#endregion
#region Functions
public string FullInfo => $"{ Index.ToString() } { DateTimeString } { CryptoCurrency } { Amount } { TransactionType }";
#endregion
}
*/
}
public class Wallet : IDBClasses
{
#region Constructors
public Wallet()
{
//wCreationYear = 0;
//wCreationMonth = 0;
//wCreationDay = 0;
//wLastActivityYear = 0;
//wLastActivityMonth = 0;
//wLastActivityDay = 0;
//wLastActivityHour = 0;
//wLastActivityMinute = 0;
//wLastActivitySecond = 0;
//wPlatform = "";
//wName = "";
//wCurrency = "";
//wBalance = 0f;
//wDefaultWallet = "";
//wNotes = "";
}
public Wallet(int _year, int _month, int _day, string _platform, string _name, string _currency, float _balance, int _default, string _note)
{
wCreationYear = _year;
wCreationMonth = _month;
wCreationDay = _day;
wPlatform = _platform;
wName = _name;
wCurrency = _currency;
wBalance = _balance;
wDefaultWallet = _default;
wNotes = _note;
}
#endregion
#region Publics
public string DBTableName => "Wallets";
public string DBVariables => "(wCreationYear, wCreationMonth, wCreationDay, wPlatform, wName, wCurrency, wBalance, wDefaultWallet, wNotes) values (@wCreationYear, @wCreationMonth, @wCreationDay, @wPlatform, @wName, @wCurrency, @wBalance, @wDefaultWallet, @wNotes)";
public int Index { get; set; }
public int wCreationYear { get; set; }
public int wCreationMonth { get; set; }
public int wCreationDay { get; set; }
public int wLastActivityYear { get; set; }
public int wLastActivityMonth { get; set; }
public int wLastActivityDay { get; set; }
public int wLastActivityHour { get; set; }
public int wLastActivityMinute { get; set; }
public int wLastActivitySecond { get; set; }
public string wPlatform { get; set; }
public string wName { get; set; }
public string wCurrency { get; set; }
public float wBalance { get; set; }
public int wDefaultWallet { get; set; }
public string wNotes { get; set; }
#endregion
#region Functions
//public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";
#endregion
}
}
}

View File

@ -9,7 +9,7 @@ namespace CryptoCalc
public partial class MainWindow : Window
{
private readonly Random rand = new();
private List<RawData> transactions = new();
private List<Transaction> transactions = new();
public MainWindow()
{
@ -18,48 +18,49 @@ namespace CryptoCalc
private void saveButton_Click(object sender, RoutedEventArgs e)
{
RawData rd = DummyTransaction();
Transaction rd = DummyTransaction();
DBInteraction.SaveData(ref rd);
}
private void saveButtonFromInput_Click(object sender, RoutedEventArgs e)
{
RawData rd = new(inputCurrency.Text, Convert.ToSingle(inputAmount.Text), inputType.Text);
Transaction rd = new(inputCurrency.Text, Convert.ToSingle(inputAmount.Text), inputType.Text);
DBInteraction.SaveData(ref rd);
}
private void readButton_click(object sender, RoutedEventArgs e)
{
transactions = DBInteraction.LoadTransactions();
foreach (RawData x in transactions)
foreach (Transaction x in transactions)
{
Debug.WriteLine($"{x.DateTimeString} *** {x.CryptoCurrency} - {x.Amount}");
Debug.WriteLine($"{x.DateTimeString} *** {x.Currency} - {x.Amount}");
}
}
private void searchButton_Click(object sender, RoutedEventArgs e)
{
transactions = DBInteraction.LoadTransactionsOfCurrency(currencyText.Text);
transactionsFoundListBox.Items.Clear();
foreach (RawData x in transactions)
foreach (Transaction x in transactions)
{
transactionsFoundListBox.Items.Add(x.FullInfo);
}
}
private RawData DummyTransaction()
private Transaction DummyTransaction()
{
RawData t = new();
t.Date_Year = DateTime.Now.Year;
t.Date_Month = DateTime.Now.Month;
t.Date_Day = DateTime.Now.Day;
t.Time_Hour = DateTime.Now.Hour;
t.Time_Minute = DateTime.Now.Minute;
t.Time_Second = DateTime.Now.Second;
Transaction t = new();
t.Year = DateTime.Now.Year;
t.Month = DateTime.Now.Month;
t.Day = DateTime.Now.Day;
t.Hour = DateTime.Now.Hour;
t.Minute = DateTime.Now.Minute;
t.Second = DateTime.Now.Second;
t.DateTimeString = DateTime.Now.ToString();
t.CryptoCurrency = "SOL";
t.Currency = "SOL";
t.Amount = Convert.ToSingle(30 * rand.NextDouble());
t.TransactionType = "DEPOSIT";
t.Service = "Firi";
t.Comment = "Test";
t.TransactionType = "BUY";
t.Platform = "Firi";
t.Note = "Test";
return t;
}

BIN
data.db

Binary file not shown.

Binary file not shown.

BIN
wallet.db

Binary file not shown.