diff --git a/transactions.db b/CryptoCalc.db similarity index 90% rename from transactions.db rename to CryptoCalc.db index a3b99f3..db156a6 100644 Binary files a/transactions.db and b/CryptoCalc.db differ diff --git a/DBClasses.cs b/DBClasses.cs index 88e1ff8..6d939a1 100644 --- a/DBClasses.cs +++ b/DBClasses.cs @@ -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 } diff --git a/DBInteraction.cs b/DBInteraction.cs index 4d27355..8d8edef 100644 --- a/DBInteraction.cs +++ b/DBInteraction.cs @@ -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 where T : DBClasses { - public static void SaveData(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 where T : DBClasses + public void ExecuteQuery(T data, string query) { - public IEnumerable QueryData(T data, string query) + if (query is null) { - if (data is null) - { - throw new ArgumentNullException(nameof(data), "DBclass is null"); - } - - using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBName)); - return _connection.Query($"{query}", new DynamicParameters()); + 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 ReturnQuery(T data, string query) + { + if (data is null) + { + throw new ArgumentNullException(nameof(data), "DBclass is null"); + } + + using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(DBClasses.DBName)); + IEnumerable output = _connection.Query($"{query}", new DynamicParameters()); + Debug.WriteLine($"Executed query - DB: {DBClasses.DBName} - Query: {query}"); + + return output; } } } diff --git a/MainWindow.xaml b/MainWindow.xaml index 02c105a..29fd908 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -11,7 +11,6 @@