From 8a10fbbb5febd0839615236d2fd28c31d0ef5783 Mon Sep 17 00:00:00 2001 From: Stedd Date: Sat, 15 Jan 2022 17:07:16 +0100 Subject: [PATCH] Refactored DB classes --- DBInteraction.cs | 41 ++--- DB_Classes.cs | 414 ++++++++++++++++++++++++++------------------- MainWindow.xaml.cs | 37 ++-- data.db | Bin 32768 -> 32768 bytes transactions.db | Bin 24576 -> 24576 bytes wallet.db | Bin 24576 -> 24576 bytes 6 files changed, 277 insertions(+), 215 deletions(-) diff --git a/DBInteraction.cs b/DBInteraction.cs index 6829d1c..6e4cf70 100644 --- a/DBInteraction.cs +++ b/DBInteraction.cs @@ -12,7 +12,7 @@ namespace CryptoCalc public class DBInteraction { - public static void SaveData(ref T _data) where T : IDBClasses + public static void SaveData(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 _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"); + // using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("RawData")); + // var output = _connection.Query("select * from RawData", new DynamicParameters()); + // Debug.WriteLine("Loaded DB data"); + // return output.ToList(); //} - //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 LoadTransactions() + + + public static List LoadTransactions() { - using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("RawData")); - var output = _connection.Query("select * from RawData", new DynamicParameters()); + using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("Transactions")); + var output = _connection.Query("select * from Transactions", new DynamicParameters()); Debug.WriteLine("Loaded DB data"); return output.ToList(); } - public static List LoadTransactionsOfCurrency(string _currency) + public static List LoadTransactionsOfCurrency(string _currency) { Debug.WriteLine($"Fetching all {_currency} transactions from Database"); - using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("RawData")); - return connection.Query($"select * from RawData where CryptoCurrency = '{ _currency }'").ToList(); + using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("Transactions")); + return connection.Query($"select * from Transactions where Currency = '{ _currency.ToUpper() }'").ToList(); } } } diff --git a/DB_Classes.cs b/DB_Classes.cs index 2ad75b0..cc6daba 100644 --- a/DB_Classes.cs +++ b/DB_Classes.cs @@ -1,186 +1,260 @@ 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 = ""; - } - - #endregion - - #region Publics - 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 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; } - - #endregion - - #region Functions - - public string FullInfo => $"{ Index.ToString() } { tDateTimeString } { tCurrency } { tAmount } { tTransactionType }"; - - #endregion - } - public class RawData : IDBClasses - { - #region Contructors - public RawData() { } - public RawData(string _currency, float _amount, string _type) - { - 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 = ""; - } - - #endregion - - #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 string CryptoCurrency { get; set; } - public float Amount { get; set; } - public string TransactionType { get; set; } - public string Service { get; set; } - public string Comment { get; set; } - - #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 - } + 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 Constructors + + public Transaction() { SetDBStrings(); } + + 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 + + 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 Wallet : 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 => "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) + { + 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 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; } + public string Service { get; set; } + public string Comment { get; set; } + + #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 + } + */ + } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 3808592..4264682 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -9,7 +9,7 @@ namespace CryptoCalc public partial class MainWindow : Window { private readonly Random rand = new(); - private List transactions = new(); + private List 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; } diff --git a/data.db b/data.db index 06224e9afcea074a4457d74dc41da7afb4688776..df345eb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