Generic method to save DB data
This commit is contained in:
parent
26f711f60d
commit
934ad28fa4
|
@ -1,7 +1,8 @@
|
||||||
<?xml version="1.0" encoding="utf-8" ?>
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<connectionStrings>
|
<connectionStrings>
|
||||||
<add name="data" connectionString="Data Source = ./data.db;version=3" providerName="System.Data.SqlClient"/>
|
<add name="RawData" connectionString="Data Source = ./data.db;version=3" providerName="System.Data.SqlClient"/>
|
||||||
<add name="wallet" connectionString="Data Source = ./wallet.db;version=3" providerName="System.Data.SqlClient"/>
|
<add name="Transactions" connectionString="Data Source = ./transactions.db;version=3" providerName="System.Data.SqlClient"/>
|
||||||
|
<add name="Wallets" connectionString="Data Source = ./wallets.db;version=3" providerName="System.Data.SqlClient"/>
|
||||||
</connectionStrings>
|
</connectionStrings>
|
||||||
</configuration>
|
</configuration>
|
|
@ -11,29 +11,53 @@ namespace CryptoCalc
|
||||||
{
|
{
|
||||||
public class DBInteraction
|
public class DBInteraction
|
||||||
{
|
{
|
||||||
public static void SaveTransaction(RawData _rawData)
|
|
||||||
|
public static void SaveData<T>(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"));
|
using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(_data.DBTableName));
|
||||||
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);
|
cnn.Execute($"insert into {_data.DBTableName} {_data.DBVariables}", _data);
|
||||||
Debug.WriteLine("Saved DB 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<RawData> LoadTransactions()
|
public static List<RawData> LoadTransactions()
|
||||||
{
|
{
|
||||||
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("data"));
|
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString("RawData"));
|
||||||
var output = _connection.Query<RawData>("select * from RawData", new DynamicParameters());
|
var output = _connection.Query<RawData>("select * from RawData", 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<RawData> 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("data"));
|
using IDbConnection connection = new SQLiteConnection(Util.GetConnectionString("RawData"));
|
||||||
return connection.Query<RawData>($"select * from RawData where CryptoCurrency = '{ _currency }'").ToList();
|
return connection.Query<RawData>($"select * from RawData where CryptoCurrency = '{ _currency }'").ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,58 +2,71 @@
|
||||||
|
|
||||||
namespace CryptoCalc
|
namespace CryptoCalc
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public interface IDBClasses
|
||||||
|
{
|
||||||
|
string DBTableName { get; }
|
||||||
|
string DBVariables { get; }
|
||||||
|
}
|
||||||
|
|
||||||
public class DB_Classes
|
public class DB_Classes
|
||||||
{
|
{
|
||||||
public class Transaction
|
public class Transaction : IDBClasses
|
||||||
{
|
{
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public Transaction() { }
|
public Transaction() { }
|
||||||
|
|
||||||
public Transaction(string _currency, float _amount, string _type, string _feeCurrency, float _feeAmount)
|
public Transaction(string _currency, float _amount, string _type, string _feeCurrency, float _feeAmount)
|
||||||
{
|
{
|
||||||
Year = DateTime.Now.Year;
|
tYear = DateTime.Now.Year;
|
||||||
Month = DateTime.Now.Month;
|
tMonth = DateTime.Now.Month;
|
||||||
Day = DateTime.Now.Day;
|
tDay = DateTime.Now.Day;
|
||||||
Hour = DateTime.Now.Hour;
|
tHour = DateTime.Now.Hour;
|
||||||
Minute = DateTime.Now.Minute;
|
tMinute = DateTime.Now.Minute;
|
||||||
Second = DateTime.Now.Second;
|
tSecond = DateTime.Now.Second;
|
||||||
DateTimeString = DateTime.Now.ToString();
|
tDateTimeString = DateTime.Now.ToString();
|
||||||
Currency = _currency;
|
tCurrency = _currency;
|
||||||
Amount = _amount;
|
tAmount = _amount;
|
||||||
TransactionType = _type;
|
tTransactionType = _type;
|
||||||
FeeCurrency = _feeCurrency;
|
tFeeCurrency = _feeCurrency;
|
||||||
FeeAmount = _feeAmount;
|
tFeeAmount = _feeAmount;
|
||||||
Service = "";
|
tPlatform = "";
|
||||||
Comment = "";
|
tNote = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Publics
|
#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 Index { get; set; }
|
||||||
public int Year { get; set; }
|
public int tYear { get; set; }
|
||||||
public int Month { get; set; }
|
public int tMonth { get; set; }
|
||||||
public int Day { get; set; }
|
public int tDay { get; set; }
|
||||||
public int Hour { get; set; }
|
public int tHour { get; set; }
|
||||||
public int Minute { get; set; }
|
public int tMinute { get; set; }
|
||||||
public int Second { get; set; }
|
public int tSecond { get; set; }
|
||||||
public string DateTimeString { get; set; }
|
public string tDateTimeString { get; set; }
|
||||||
public string Currency { get; set; }
|
public string tCurrency { get; set; }
|
||||||
public float Amount { get; set; }
|
public float tAmount { get; set; }
|
||||||
public string TransactionType { get; set; }
|
public string tTransactionType { get; set; }
|
||||||
public string FeeCurrency { get; set; }
|
public string tFeeCurrency { get; set; }
|
||||||
public float FeeAmount { get; set; }
|
public float tFeeAmount { get; set; }
|
||||||
public string Service { get; set; }
|
public string tPlatform { get; set; }
|
||||||
public string Comment { get; set; }
|
public string tNote { get; set; }
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Functions
|
#region Functions
|
||||||
public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";
|
|
||||||
|
public string FullInfo => $"{ Index.ToString() } { tDateTimeString } { tCurrency } { tAmount } { tTransactionType }";
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
public class RawData
|
public class RawData : IDBClasses
|
||||||
{
|
{
|
||||||
#region Contructors
|
#region Contructors
|
||||||
public RawData() { }
|
public RawData() { }
|
||||||
|
@ -77,6 +90,8 @@ namespace CryptoCalc
|
||||||
|
|
||||||
#region Publics
|
#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 Index { get; set; }
|
||||||
public int Date_Year { get; set; }
|
public int Date_Year { get; set; }
|
||||||
public int Date_Month { get; set; }
|
public int Date_Month { get; set; }
|
||||||
|
@ -100,8 +115,10 @@ namespace CryptoCalc
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Wallet
|
public class Wallet : IDBClasses
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public Wallet()
|
public Wallet()
|
||||||
{
|
{
|
||||||
|
@ -138,6 +155,9 @@ namespace CryptoCalc
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Publics
|
#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 Index { get; set; }
|
||||||
public int wCreationYear { get; set; }
|
public int wCreationYear { get; set; }
|
||||||
|
|
|
@ -18,11 +18,14 @@ namespace CryptoCalc
|
||||||
|
|
||||||
private void saveButton_Click(object sender, RoutedEventArgs e)
|
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)
|
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)
|
private void readButton_click(object sender, RoutedEventArgs e)
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue