From 934ad28fa4e7495c89daad680c557d3878d998a2 Mon Sep 17 00:00:00 2001 From: Stedd Date: Sat, 15 Jan 2022 15:28:59 +0100 Subject: [PATCH] Generic method to save DB data --- App.config | 5 +-- DBInteraction.cs | 42 ++++++++++++++++++----- DB_Classes.cs | 84 ++++++++++++++++++++++++++++----------------- MainWindow.xaml.cs | 7 ++-- transactions.db | Bin 0 -> 24576 bytes wallet.db | Bin 24576 -> 24576 bytes 6 files changed, 93 insertions(+), 45 deletions(-) create mode 100644 transactions.db diff --git a/App.config b/App.config index 657cbdc..924c79d 100644 --- a/App.config +++ b/App.config @@ -1,7 +1,8 @@  - - + + + \ No newline at end of file diff --git a/DBInteraction.cs b/DBInteraction.cs index e9c3246..6829d1c 100644 --- a/DBInteraction.cs +++ b/DBInteraction.cs @@ -11,29 +11,53 @@ namespace CryptoCalc { public class DBInteraction { - public static void SaveTransaction(RawData _rawData) + + public static void SaveData(ref T _data) where T : IDBClasses { - if (_rawData is null) + if (_data is null) { - throw new ArgumentNullException(nameof(_rawData)); + throw new ArgumentNullException(nameof(_data)); } - using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString("data")); - cnn.Execute("insert into RawData (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)", _rawData); - Debug.WriteLine("Saved DB data"); + using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(_data.DBTableName)); + cnn.Execute($"insert into {_data.DBTableName} {_data.DBVariables}", _data); + Debug.WriteLine($"Saved {_data.DBTableName} DB data"); } + + //public static void SaveTransaction(RawData _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} (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 LoadTransactions() { - using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("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 List LoadTransactionsOfCurrency(string _currency) { Debug.WriteLine($"Fetching all {_currency} transactions from Database"); - using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("data")); + using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("RawData")); return connection.Query($"select * from RawData where CryptoCurrency = '{ _currency }'").ToList(); } } diff --git a/DB_Classes.cs b/DB_Classes.cs index 7229171..2ad75b0 100644 --- a/DB_Classes.cs +++ b/DB_Classes.cs @@ -2,58 +2,71 @@ namespace CryptoCalc { + + public interface IDBClasses + { + string DBTableName { get; } + string DBVariables { get; } + } + public class DB_Classes { - public class Transaction + public class Transaction : IDBClasses { #region Constructors + public Transaction() { } 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; - Service = ""; - Comment = ""; + 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 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 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 Service { get; set; } - public string Comment { 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() } { DateTimeString } { Currency } { Amount } { TransactionType }"; + + public string FullInfo => $"{ Index.ToString() } { tDateTimeString } { tCurrency } { tAmount } { tTransactionType }"; #endregion } - public class RawData + public class RawData : IDBClasses { #region Contructors public RawData() { } @@ -77,6 +90,8 @@ namespace CryptoCalc #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; } @@ -100,8 +115,10 @@ namespace CryptoCalc } - public class Wallet + public class Wallet : IDBClasses { + + #region Constructors public Wallet() { @@ -138,6 +155,9 @@ namespace CryptoCalc #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; } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 866e300..3808592 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -18,11 +18,14 @@ namespace CryptoCalc private void saveButton_Click(object sender, RoutedEventArgs e) { - DBInteraction.SaveTransaction(DummyTransaction()); + RawData rd = DummyTransaction(); + DBInteraction.SaveData(ref rd); } + private void saveButtonFromInput_Click(object sender, RoutedEventArgs e) { - DBInteraction.SaveTransaction(new RawData(inputCurrency.Text, Convert.ToSingle(inputAmount.Text), inputType.Text)); + RawData rd = new(inputCurrency.Text, Convert.ToSingle(inputAmount.Text), inputType.Text); + DBInteraction.SaveData(ref rd); } private void readButton_click(object sender, RoutedEventArgs e) diff --git a/transactions.db b/transactions.db new file mode 100644 index 0000000000000000000000000000000000000000..8ec933f474d6cd1454fcb7701ec889ff89a641d6 GIT binary patch literal 24576 zcmeI)Z%@-e90%~0!DNtHo-I5pO{Nft1WoX%brfCL3XCqgN6i7JHrZO}ZXyvMnaEr5 z74Q~(?K4=*d0DlJ;cxi(w43!u*wHe4oEEf0!5M#XU6;f&c^{ z009U<00MasFb31r^7^`d`I(W!4(*+G4g*HJCxI*mPctWGs=UgAtWp)%@Y0klZO9tN zpzvE3SMSWXIi7EGs@ilN-?e<_W!<&xOx(7X=Sp@hI;NlOQqA+-S8mhtc74lh)$3Nv ztL?YkHGTzaQ<7w_kfBB#u@4hsTjYE~mD$Po+U$Ui^fEy`BjPAE+$ zmpQj7Rny9IdmX~WuiHwqyja}lu$6nwTEl6!tsS?$JnRz7Y5BV~&lu=`WVbJUjUPTJ zS1S7Dk{kd^1AFT8v2IsG=y98=UGsnTIG?r+*Yj8AR`v?T^6IMI9!+@urY-2v(>@=~ zVm(od)2Ycm7F>=A^{?B;Q7+}xb}*Aa78O(+p_CAC(F zp?@1mFBPefx>CqMo%EQ)=ibMVoo6$u(L1uL;d?WumZ8kgZKPMCOnhuHlJV>`Kl3>K z?KI~aj#(_X)^#lu>+Z0hNT0>B9Ap;hU?X#?z+W95@e28zr`oAZN~KQxU_&hx3{iVQ zJYa(W1Rwwb2tWV=5P$##AOHafKp?LIbK=Hs-v5iw__09%0uX=z1Rwwb2tWV=5P$## zAdo`=@%g`QUTfyH`74LFhg*UG1Rwwb2tWV=5P$##AOHafV!Z literal 0 HcmV?d00001 diff --git a/wallet.db b/wallet.db index 016fa242bec93734c6f825833b9084445d02eab4..30a84f886ec87679619213c2f56da5960993c1f5 100644 GIT binary patch delta 269 zcmZoTz}Rqrae}lUD+2=q8x%7EY3_+S#^S6Ddj7S%yuTQjx#u$QuHc@_f04h9zingV zKMoe&Uo0k*XK?B=GHpJ_d4zFtDX+Xc?=L2KaeaNp_Vkj(q@2{^!knb|77jLcacOCo zSa@PiPHIUplwxuYa&-)GRS0o(@^MvAf{Jdwz^%)~czyFf-VR14NA}50{0{ugKsT8K zF@r8Uo4BL& Z3*y1fiZ|q9VwUEBYTYCN)X56g2>?}nOXdIo delta 244 zcmZoTz}Rqrae}lU3j+fK8xS)CF%uB8Pt-9MXJOFuujS?a#lXzHfPr@f_X7UQ{0;mK zn*{~rI9Ygqv4~8r;M8Sg-MozR2%{12FD5y0eSOCE)RM%coYdmNoTT`Y)D~72c5z8b zm}q!nPEKkGgk;?;#+Su7S(-a<@)X`o#+#e}@pdpy?%}r)2RXzRh#9n**u?E685y)$ zK{QZKV)A=_HqLlMhzV?)9JmD-nKlbLeBqyDz{c{Gf&VMeagX_h#W*1Lb22hZazGSs N65tlt%q{Sh9{?ApKokG~