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 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) if (_data is null)
{ {
@ -23,42 +23,29 @@ namespace CryptoCalc
Debug.WriteLine($"Saved {_data.DBTableName} DB data"); Debug.WriteLine($"Saved {_data.DBTableName} DB data");
} }
//public static void SaveTransaction(RawData _data) //public static List<T> _LoadTransactions()
//{ //{
// if (_data is null) // using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("RawData"));
// { // var output = _connection.Query<T>("select * from RawData", new DynamicParameters());
// throw new ArgumentNullException(nameof(_data)); // Debug.WriteLine("Loaded DB data");
// } // return output.ToList();
// 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");
//} //}
public static List<RawData> LoadTransactions()
public static List<Transaction> LoadTransactions()
{ {
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("RawData")); using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("Transactions"));
var output = _connection.Query<RawData>("select * from RawData", new DynamicParameters()); var output = _connection.Query<Transaction>("select * from Transactions", new DynamicParameters());
Debug.WriteLine("Loaded DB data"); Debug.WriteLine("Loaded DB data");
return output.ToList(); 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"); Debug.WriteLine($"Fetching all {_currency} transactions from Database");
using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("RawData")); using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("Transactions"));
return connection.Query<RawData>($"select * from RawData where CryptoCurrency = '{ _currency }'").ToList(); return connection.Query<Transaction>($"select * from Transactions where Currency = '{ _currency.ToUpper() }'").ToList();
} }
} }
} }

View File

@ -1,105 +1,206 @@
using System; using System;
using System.Text.RegularExpressions;
namespace CryptoCalc namespace CryptoCalc
{ {
public interface IDBClasses public interface IDBClasses
{ {
string dbVariables { get; set; }
string variables { get; set; }
string DBTableName { get; } string DBTableName { get; }
string DBVariables { get; } string DBVariables { get; }
} }
public class DB_Classes public class DB_Classes : IDBClasses
{ {
public class Transaction : IDBClasses public virtual string dbVariables { get; set ; }
{ public virtual string variables { get; set; }
#region Constructors
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) public int Index { get; set; }
{ public int Year { get; set; }
tYear = DateTime.Now.Year; public int Month { get; set; }
tMonth = DateTime.Now.Month; public int Day { get; set; }
tDay = DateTime.Now.Day; public int Hour { get; set; }
tHour = DateTime.Now.Hour; public int Minute { get; set; }
tMinute = DateTime.Now.Minute; public int Second { get; set; }
tSecond = DateTime.Now.Second; public string DateTimeString { get; set; }
tDateTimeString = DateTime.Now.ToString();
tCurrency = _currency;
tAmount = _amount;
tTransactionType = _type;
tFeeCurrency = _feeCurrency;
tFeeAmount = _feeAmount;
tPlatform = "";
tNote = "";
} }
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 #endregion
#region Publics #region Constructors
public string DBTableName => "Transactions";
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 Transaction(string _currency, float _amount, string _type)
public int tYear { get; set; } {
public int tMonth { get; set; } Year = DateTime.Now.Year;
public int tDay { get; set; } Month = DateTime.Now.Month;
public int tHour { get; set; } Day = DateTime.Now.Day;
public int tMinute { get; set; } Hour = DateTime.Now.Hour;
public int tSecond { get; set; } Minute = DateTime.Now.Minute;
public string tDateTimeString { get; set; } Second = DateTime.Now.Second;
public string tCurrency { get; set; } DateTimeString = DateTime.Now.ToString();
public float tAmount { get; set; } Currency = _currency;
public string tTransactionType { get; set; } Amount = _amount;
public string tFeeCurrency { get; set; } TransactionType = _type;
public float tFeeAmount { get; set; }
public string tPlatform { get; set; } Platform = "";
public string tNote { get; set; } 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 #endregion
#region Functions #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 #endregion
} }
public class RawData : IDBClasses
public class Wallet : DB_Classes
{ {
#region Contructors #region Publics
public RawData() { } public override string dbVariables { get => base.dbVariables; set => base.dbVariables = value; }
public RawData(string _currency, float _amount, string _type) 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; CreationYear = _year;
Date_Month = DateTime.Now.Month; CreationMonth = _month;
Date_Day = DateTime.Now.Day; CreationDay = _day;
Time_Hour = DateTime.Now.Hour; Platform = _platform;
Time_Minute = DateTime.Now.Minute; Name = _name;
Time_Second = DateTime.Now.Second; Currency = _currency;
DateTimeString = DateTime.Now.ToString(); Balance = _balance;
CryptoCurrency = _currency; DefaultWallet = _default;
Amount = _amount; Notes = _note;
TransactionType = _type;
Service = ""; SetDBStrings();
Comment = "";
} }
#endregion #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 #region Publics
public string DBTableName => "RawData"; public override 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 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 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 string CryptoCurrency { get; set; } public string CryptoCurrency { get; set; }
public float Amount { get; set; } public float Amount { get; set; }
public string TransactionType { get; set; } public string TransactionType { get; set; }
@ -108,79 +209,52 @@ namespace CryptoCalc
#endregion #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 #region Functions
public string FullInfo => $"{ Index.ToString() } { DateTimeString } { CryptoCurrency } { Amount } { TransactionType }"; public string FullInfo => $"{ Index.ToString() } { DateTimeString } { CryptoCurrency } { Amount } { TransactionType }";
#endregion #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 public partial class MainWindow : Window
{ {
private readonly Random rand = new(); private readonly Random rand = new();
private List<RawData> transactions = new(); private List<Transaction> transactions = new();
public MainWindow() public MainWindow()
{ {
@ -18,48 +18,49 @@ namespace CryptoCalc
private void saveButton_Click(object sender, RoutedEventArgs e) private void saveButton_Click(object sender, RoutedEventArgs e)
{ {
RawData rd = DummyTransaction(); Transaction rd = DummyTransaction();
DBInteraction.SaveData(ref rd); DBInteraction.SaveData(ref rd);
} }
private void saveButtonFromInput_Click(object sender, RoutedEventArgs e) 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); DBInteraction.SaveData(ref rd);
} }
private void readButton_click(object sender, RoutedEventArgs e) private void readButton_click(object sender, RoutedEventArgs e)
{ {
transactions = DBInteraction.LoadTransactions(); 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) private void searchButton_Click(object sender, RoutedEventArgs e)
{ {
transactions = DBInteraction.LoadTransactionsOfCurrency(currencyText.Text); transactions = DBInteraction.LoadTransactionsOfCurrency(currencyText.Text);
transactionsFoundListBox.Items.Clear(); transactionsFoundListBox.Items.Clear();
foreach (RawData x in transactions) foreach (Transaction x in transactions)
{ {
transactionsFoundListBox.Items.Add(x.FullInfo); transactionsFoundListBox.Items.Add(x.FullInfo);
} }
} }
private RawData DummyTransaction()
private Transaction DummyTransaction()
{ {
RawData t = new(); Transaction t = new();
t.Date_Year = DateTime.Now.Year; t.Year = DateTime.Now.Year;
t.Date_Month = DateTime.Now.Month; t.Month = DateTime.Now.Month;
t.Date_Day = DateTime.Now.Day; t.Day = DateTime.Now.Day;
t.Time_Hour = DateTime.Now.Hour; t.Hour = DateTime.Now.Hour;
t.Time_Minute = DateTime.Now.Minute; t.Minute = DateTime.Now.Minute;
t.Time_Second = DateTime.Now.Second; t.Second = DateTime.Now.Second;
t.DateTimeString = DateTime.Now.ToString(); t.DateTimeString = DateTime.Now.ToString();
t.CryptoCurrency = "SOL"; t.Currency = "SOL";
t.Amount = Convert.ToSingle(30 * rand.NextDouble()); t.Amount = Convert.ToSingle(30 * rand.NextDouble());
t.TransactionType = "DEPOSIT"; t.TransactionType = "BUY";
t.Service = "Firi"; t.Platform = "Firi";
t.Comment = "Test"; t.Note = "Test";
return t; return t;
} }

BIN
data.db

Binary file not shown.

Binary file not shown.

BIN
wallet.db

Binary file not shown.