Merge branch 'Dev/ConnectTransactionsAndWallets'

This commit is contained in:
Stedd 2022-02-12 20:35:01 +01:00
commit 39aa1eff9b
6 changed files with 194 additions and 50 deletions

Binary file not shown.

View File

@ -6,16 +6,17 @@ namespace CryptoCalc
public interface IDBClasses
{
string DBName { get; }
static string DBName { get; }
string DBTableName { get; }
string DBVariables { get; set; }
string DBSaveDataString { get; }
}
public class DBClasses : IDBClasses
{
#region Publics
public string DBName => "CryptoCalc";
public static string DBName => "CryptoCalc";
public virtual string DBTableName { get; }
public virtual string DBVariables { get; set; }
public string DBSaveDataString => $"({Regex.Replace(DBVariables, "@", "")}) values ({DBVariables})";
@ -33,16 +34,17 @@ namespace CryptoCalc
UnixTime = (long)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalSeconds;
}
public long GetUnixTime(DateTime dateTime)
public static long GetUnixTime(DateTime dateTime)
{
return (long)dateTime.Subtract(DateTime.UnixEpoch).TotalSeconds;
}
public DateTime GetUTCTimeFromUnixTime(long unix)
public static DateTime GetUTCTimeFromUnixTime(long unix)
{
return DateTime.UnixEpoch.AddSeconds(unix).ToUniversalTime();
}
public DateTime GetLocalTimeFromUnixTime(long unix)
public static DateTime GetLocalTimeFromUnixTime(long unix)
{
return DateTime.UnixEpoch.AddSeconds(unix).ToLocalTime();
}
@ -58,11 +60,12 @@ namespace CryptoCalc
public override string DBVariables { get => base.DBVariables; set => base.DBVariables = value; }
public override string DBTableName => "Transactions";
public int WalletID { get; set; }
public string Currency { get; set; }
public float Amount { get; set; }
public decimal Amount { get; set; }
public string TransactionType { get; set; }
public string FeeCurrency { get; set; }
public float FeeAmount { get; set; }
public decimal FeeAmount { get; set; }
public string Platform { get; set; }
public string Note { get; set; }
@ -72,12 +75,13 @@ namespace CryptoCalc
public Transaction() { SetDBStrings(); }
public Transaction(string _currency, float _amount, string _type)
public Transaction(int walletID, string currency, decimal amount, string type)
{
SaveUnixTimeNow();
Currency = _currency;
Amount = _amount;
TransactionType = _type;
WalletID = walletID;
Currency = currency;
Amount = amount;
TransactionType = type;
Platform = "";
Note = "";
@ -85,7 +89,7 @@ namespace CryptoCalc
SetDBStrings();
}
public Transaction(string currency, float amount, string type, string feeCurrency, float feeAmount)
public Transaction(string currency, decimal amount, string type, string feeCurrency, decimal feeAmount)
{
SaveUnixTimeNow();
Currency = currency;
@ -106,7 +110,8 @@ namespace CryptoCalc
private void SetDBStrings()
{
DBVariables =
$"@{nameof(UnixTime)},"
$"@{nameof(WalletID)},"
+ $"@{nameof(UnixTime)},"
+ $"@{nameof(Currency)},"
+ $"@{nameof(Amount)},"
+ $"@{nameof(TransactionType)},"
@ -116,7 +121,26 @@ namespace CryptoCalc
+ $"@{nameof(Note)}";
}
public string FullInfo => $"{ Index } { GetLocalTimeFromUnixTime(UnixTime) } { Currency } { Amount } { TransactionType }";
public string FullInfo => $"{ Index } {WalletID} { GetLocalTimeFromUnixTime(UnixTime) } { Currency } { Amount } { TransactionType }";
public string CreateTable(string walletTableName)
{
return
$"CREATE TABLE IF NOT EXISTS \"{DBTableName}\" ("
+ $"\"{nameof(Index)}\" INTEGER NOT NULL UNIQUE,"
+ $"\"{nameof(WalletID)}\" INTEGER NOT NULL,"
+ $"\"{nameof(UnixTime)}\" INTEGER NOT NULL,"
+ $"\"{nameof(Currency)}\" TEXT NOT NULL,"
+ $"\"{nameof(Amount)}\" REAL NOT NULL,"
+ $"\"{nameof(TransactionType)}\" TEXT NOT NULL,"
+ $"\"{nameof(FeeCurrency)}\" TEXT,"
+ $"\"{nameof(FeeAmount)}\" REAL,"
+ $"\"{nameof(Platform)}\" TEXT,"
+ $"\"{nameof(Note)}\" TEXT,"
+ $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT),"
+ $"FOREIGN KEY(\"{nameof(WalletID)}\") REFERENCES \"{walletTableName}\"(\"{nameof(Index)}\")"
+ $"); ";
}
#endregion
}
@ -132,7 +156,7 @@ namespace CryptoCalc
public string Platform { get; set; }
public string Name { get; set; }
public string Currency { get; set; }
public float Balance { get; set; }
public decimal Balance { get; set; }
public int DefaultWallet { get; set; }
public string Note { get; set; }
@ -142,15 +166,15 @@ namespace CryptoCalc
public Wallet() { SetDBStrings(); }
public Wallet(DateTime dateTime, string platform, string name, string currency, float balance, int defaultWallet, string note)
public Wallet(DateTime dateTime, string platform, string name, string currency, string note)
{
SaveUnixTimeNow();
UnixTimeCreated = GetUnixTime(dateTime);
Platform = platform;
Name = name;
Currency = currency;
Balance = balance;
DefaultWallet = defaultWallet;
Balance = 0M;
DefaultWallet = 0;
Note = note;
SetDBStrings();
@ -175,6 +199,22 @@ namespace CryptoCalc
//public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";
public string CreateTable()
{
return
$"CREATE TABLE IF NOT EXISTS \"{DBTableName}\" ("
+ $"\"{nameof(Index)}\" INTEGER NOT NULL UNIQUE,"
+ $"\"{nameof(UnixTime)}\" INTEGER NOT NULL,"
+ $"\"{nameof(UnixTimeCreated)}\" INTEGER NOT NULL,"
+ $"\"{nameof(Platform)}\" TEXT,"
+ $"\"{nameof(Name)}\" TEXT,"
+ $"\"{nameof(Currency)}\" TEXT NOT NULL,"
+ $"\"{nameof(Balance)}\" REAL NOT NULL,"
+ $"\"{nameof(DefaultWallet)}\" INTEGER,"
+ $"\"{nameof(Note)}\" TEXT,"
+ $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT)"
+ $"); ";
}
#endregion
}

View File

@ -4,37 +4,54 @@ using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Linq;
namespace CryptoCalc
{
public class DBInteraction
public class DBInteraction<T> where T : DBClasses
{
public static void SaveData<T>(T data) where T : DBClasses
public void CreateDB()
{
SQLiteConnection.CreateFile(DBClasses.DBName);
}
public void SaveData(T data)
{
if (data is null)
{
throw new ArgumentNullException(nameof(data), "No data passed to SQL");
}
using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(data.DBName));
cnn.Execute($"insert into {data.DBTableName} {data.DBSaveDataString}", data);
Debug.WriteLine($"Saved {data.DBTableName} DB data");
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(DBClasses.DBName));
string query = $"insert into {data.DBTableName} {data.DBSaveDataString}";
_connection.Execute(query, data);
Debug.WriteLine($"Saved data - DB: {DBClasses.DBName} - Table: {data.DBTableName} - Query: {query}");
}
public class GenericDB<T> where T : DBClasses
public void ExecuteQuery(T data, string query)
{
public IEnumerable<T> QueryData(T data, string query)
if (query is null)
{
throw new ArgumentNullException(nameof(query), "No query entered");
}
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(DBClasses.DBName));
_connection.Execute($"{query}");
Debug.WriteLine($"Executed query - DB: {DBClasses.DBName} - Query: {query}");
}
public IEnumerable<T> ReturnQuery(T data, string query)
{
if (data is null)
{
throw new ArgumentNullException(nameof(data), "DBclass is null");
}
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBName));
return _connection.Query<T>($"{query}", new DynamicParameters());
}
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(DBClasses.DBName));
IEnumerable<T> output = _connection.Query<T>($"{query}", new DynamicParameters());
Debug.WriteLine($"Executed query - DB: {DBClasses.DBName} - Query: {query}");
return output;
}
}
}

View File

@ -11,7 +11,6 @@
<TabItem Header="Debug">
<Grid Background="#FFE5E5E5" Width="Auto" Height="Auto">
<Button x:Name="saveButton" Content="Save Random" Margin="0,10,37,0" Click="saveButton_Click" FontSize="20" HorizontalAlignment="Right" Width="239" Height="38" VerticalAlignment="Top"/>
<Button x:Name="saveWalletButton" Content="Save Random Wallet" Margin="0,120,37,0" Click="saveWalletButton_Click" FontSize="20" HorizontalAlignment="Right" Width="239" Height="38" VerticalAlignment="Top"/>
<Button x:Name="readButton" Content="PrintToConsole" Margin="0,53,37,0" Click="readButton_click" FontSize="20" Height="36" VerticalAlignment="Top" HorizontalAlignment="Right" Width="239"/>
<Button x:Name="saveButton_fromInput" Content="Save Data" Margin="0,0,37,10" VerticalAlignment="Bottom" Height="39" Click="saveButtonFromInput_Click" FontSize="20" HorizontalAlignment="Right" Width="239"/>
<TextBox x:Name="inputCurrency" Margin="0,0,60,125" TextWrapping="Wrap" Text="BTC" Height="27" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="120" LostFocus="inputCurrency_LostFocus"/>
@ -30,7 +29,22 @@
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="Wallets">
<Grid Background="#FFE5E5E5"/>
<Grid Background="#FFE5E5E5">
<ListBox x:Name="listBox_Wallets" Margin="0,278,37,258" SelectionChanged="listBox_Wallets_SelectionChanged" HorizontalAlignment="Right" Width="239"/>
<Button x:Name="show_Wallets" Content="Show Wallets" HorizontalAlignment="Right" Margin="0,240,37,0" VerticalAlignment="Top" Click="show_Wallets_Click" Width="239" Height="28"/>
<Button x:Name="saveRandomWalletButton" Content="Save Random Wallet" Margin="0,38,37,0" Click="saveRandomWalletButton_Click" FontSize="20" HorizontalAlignment="Right" Width="239" Height="38" VerticalAlignment="Top"/>
<Button x:Name="saveWalletButton" Content="Save Wallet" Margin="45,125,0,0" Click="saveWalletButton_Click" FontSize="20" Height="38" VerticalAlignment="Top" HorizontalAlignment="Left" Width="239"/>
<TextBox x:Name="inputWalletName" HorizontalAlignment="Left" Margin="135,269,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="149" Height="31"/>
<Label x:Name="label1" Content="Name" HorizontalAlignment="Left" Height="31" Margin="34,269,0,0" VerticalAlignment="Top" Width="96"/>
<TextBox x:Name="inputWalletPlatform" HorizontalAlignment="Left" Margin="135,235,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="149" Height="31" RenderTransformOrigin="0.354,-0.643"/>
<Label x:Name="label1_Copy" Content="Platform" HorizontalAlignment="Left" Height="31" Margin="34,235,0,0" VerticalAlignment="Top" Width="96"/>
<TextBox x:Name="inputWalletCurrency" HorizontalAlignment="Left" Margin="135,304,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="149" Height="31"/>
<Label x:Name="label1_Copy1" Content="Currency" HorizontalAlignment="Left" Height="31" Margin="34,304,0,0" VerticalAlignment="Top" Width="96"/>
<DatePicker x:Name="inputWalletCreationDate" Margin="135,199,0,0" FirstDayOfWeek="Monday" Height="31" VerticalAlignment="Top" HorizontalAlignment="Left" Width="149"/>
<Label x:Name="label1_Copy3" Content="CreationDate" HorizontalAlignment="Left" Height="31" Margin="34,199,0,0" VerticalAlignment="Top" Width="96"/>
<TextBox x:Name="inputWalletNote" HorizontalAlignment="Left" Margin="135,340,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="149" Height="31"/>
<Label x:Name="label1_Copy4" Content="Note" HorizontalAlignment="Left" Height="31" Margin="34,340,0,0" VerticalAlignment="Top" Width="96"/>
</Grid>
</TabItem>
<TabItem Header="Transactions">
<Grid Background="#FFE5E5E5">

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace CryptoCalc
{
@ -9,44 +10,77 @@ namespace CryptoCalc
{
private readonly Random rand = new();
private DBInteraction.GenericDB<Transaction> transactionDB = new();
private DBInteraction.GenericDB<Wallet> walletDB = new();
private readonly DBInteraction<DBClasses> DB = new();
private readonly DBInteraction<Transaction> transactionDB = new();
private readonly DBInteraction<Wallet> walletDB = new();
private Transaction genericT = new();
private Wallet genericW = new();
private readonly Transaction genericT = new();
private readonly Wallet genericW = new();
private List<Wallet> walletList = new();
private Wallet selectedWallet = new();
public MainWindow()
{
InitializeComponent();
//Create the DB file if none exist
DB.CreateDB();
//Create tables in DB if none exist
transactionDB.ExecuteQuery(genericT, genericT.CreateTable(genericW.DBTableName));
walletDB.ExecuteQuery(genericW, genericW.CreateTable());
}
private void saveButton_Click(object sender, RoutedEventArgs e)
{
DBInteraction.SaveData(DummyTransaction());
transactionDB.SaveData(DummyTransaction());
}
private void saveWalletButton_Click(object sender, RoutedEventArgs e)
private void saveRandomWalletButton_Click(object sender, RoutedEventArgs e)
{
DBInteraction.SaveData(DummyWallet());
walletDB.SaveData(DummyWallet());
}
private void saveButtonFromInput_Click(object sender, RoutedEventArgs e)
{
DBInteraction.SaveData(new Transaction(inputCurrency.Text, Convert.ToSingle(inputAmount.Text), inputType.Text));
transactionDB.SaveData
(
new Transaction(
selectedWallet.Index,
inputCurrency.Text,
Convert.ToDecimal(inputAmount.Text),
inputType.Text
)
);
}
private void saveWalletButton_Click(object sender, RoutedEventArgs e)
{
walletDB.SaveData(
new Wallet(
(DateTime)inputWalletCreationDate.SelectedDate,
inputWalletPlatform.Text,
inputWalletName.Text,
inputWalletCurrency.Text,
inputWalletNote.Text
)
);
}
private void readButton_click(object sender, RoutedEventArgs e)
{
string query = $"select * from {genericT.DBTableName}";
IEnumerable <Transaction> transactions = transactionDB.QueryData(genericT, query);
IEnumerable<Transaction> transactions = transactionDB.ReturnQuery(genericT, query);
foreach (Transaction t in transactions)
{
Debug.WriteLine($"{t.GetLocalTimeFromUnixTime(t.UnixTime)} *** {t.Currency} - {t.Amount}");
Debug.WriteLine($"{DBClasses.GetLocalTimeFromUnixTime(t.UnixTime)} *** {t.Currency} - {t.Amount}");
}
}
private void searchButton_Click(object sender, RoutedEventArgs e)
{
string query = $"SELECT * from {genericT.DBTableName} WHERE {nameof(genericT.Currency)}=\'{currencyText.Text}\'";
IEnumerable<Transaction> transactions = transactionDB.QueryData(genericT, query);
IEnumerable<Transaction> transactions = transactionDB.ReturnQuery(genericT, query);
transactionsFoundListBox.Items.Clear();
foreach (Transaction x in transactions)
{
@ -54,12 +88,29 @@ namespace CryptoCalc
}
}
private void show_Wallets_Click(object sender, RoutedEventArgs e)
{
selectedWallet = null;
walletList.Clear();
string query = $"SELECT * from {genericW.DBTableName}";
IEnumerable<Wallet> wallets = walletDB.ReturnQuery(genericW, query);
listBox_Wallets.Items.Clear();
foreach (Wallet wallet in wallets)
{
walletList.Add(wallet);
listBox_Wallets.Items.Add($"{wallet.Name}-{wallet.Currency}-{wallet.Balance}");
}
}
private Transaction DummyTransaction()
{
Transaction t = new();
t.WalletID = selectedWallet.Index;
t.SaveUnixTimeNow();
t.Currency = "SOL";
t.Amount = Convert.ToSingle(30 * rand.NextDouble());
t.Amount = Convert.ToDecimal(30 * rand.NextDouble());
t.TransactionType = "BUY";
t.Platform = "Firi";
t.Note = "Test";
@ -70,11 +121,11 @@ namespace CryptoCalc
{
Wallet w = new();
w.SaveUnixTimeNow();
w.UnixTimeCreated = w.GetUnixTime(new DateTime(rand.Next(2011, DateTime.Now.Year), rand.Next(1,12), rand.Next(1, 28)));
w.UnixTimeCreated = DBClasses.GetUnixTime(new DateTime(rand.Next(2011, DateTime.Now.Year), rand.Next(1, 12), rand.Next(1, 28)));
w.Platform = "Ledger";
w.Name = "DefaultBTCWallet";
w.Currency = "BTC";
w.Balance = Convert.ToSingle(30 * rand.NextDouble());
w.Balance = 0;//Convert.ToSingle(30 * rand.NextDouble());
w.DefaultWallet = 1;
w.Note = "Main BTC Wallet";
return w;
@ -95,5 +146,27 @@ namespace CryptoCalc
{
inputCurrency.Text = inputCurrency.Text.ToUpper();
}
private void listBox_Wallets_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (listBox_Wallets.Items.Count > 0)
{
if (listBox_Wallets.SelectedIndex < walletList.Count)
{
selectedWallet = walletList[listBox_Wallets.SelectedIndex];
if (selectedWallet != null)
{
Debug.WriteLine($"{selectedWallet.Name} {selectedWallet.Balance}");
}
}
}
else
{
Debug.WriteLine("No wallet selected yet");
}
}
}
}

Binary file not shown.