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 06224e9..df345eb 100644 Binary files a/data.db and b/data.db differ diff --git a/transactions.db b/transactions.db index 8ec933f..5b55604 100644 Binary files a/transactions.db and b/transactions.db differ diff --git a/wallet.db b/wallet.db index 30a84f8..713854c 100644 Binary files a/wallet.db and b/wallet.db differ