Refactoring and cleanup

This commit is contained in:
Stedd 2022-01-15 17:27:38 +01:00
parent 5c1ec6bfed
commit 4b6581fbd7
3 changed files with 69 additions and 75 deletions

View File

@ -6,19 +6,18 @@ namespace CryptoCalc
public interface IDBClasses public interface IDBClasses
{ {
string dbVariables { get; set; } string DBVariables { get; set; }
string variables { get; set; }
string DBTableName { get; } string DBTableName { get; }
string DBVariables { get; } string DBSaveDataString { get; }
} }
public class DB_Classes : IDBClasses public class DBClasses : IDBClasses
{ {
public virtual string dbVariables { get; set ; } #region Publics
public virtual string variables { get; set; } public virtual string DBVariables { get; set; }
public virtual string DBTableName => throw new NotImplementedException(); public virtual string DBTableName => throw new NotImplementedException();
public string DBVariables => $"({variables}) values ({dbVariables})"; public string DBSaveDataString => $"({Regex.Replace(DBVariables, "@", "")}) values ({DBVariables})";
public int Index { get; set; } public int Index { get; set; }
public int Year { get; set; } public int Year { get; set; }
@ -28,14 +27,31 @@ namespace CryptoCalc
public int Minute { get; set; } public int Minute { get; set; }
public int Second { get; set; } public int Second { get; set; }
public string DateTimeString { get; set; } public string DateTimeString { get; set; }
#endregion
#region Functions
public void SaveDateTime()
{
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();
}
#endregion
} }
public class Transaction : DB_Classes public class Transaction : DBClasses
{ {
#region Publics #region Publics
public override string dbVariables { get => base.dbVariables; set => base.dbVariables = value; } 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 override string DBTableName => "Transactions";
public string Currency { get; set; } public string Currency { get; set; }
@ -54,13 +70,7 @@ namespace CryptoCalc
public Transaction(string _currency, float _amount, string _type) public Transaction(string _currency, float _amount, string _type)
{ {
Year = DateTime.Now.Year; SaveDateTime();
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; Currency = _currency;
Amount = _amount; Amount = _amount;
TransactionType = _type; TransactionType = _type;
@ -73,13 +83,7 @@ namespace CryptoCalc
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; SaveDateTime();
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; Currency = _currency;
Amount = _amount; Amount = _amount;
TransactionType = _type; TransactionType = _type;
@ -97,23 +101,21 @@ namespace CryptoCalc
private void SetDBStrings() private void SetDBStrings()
{ {
dbVariables = DBVariables =
$"@{nameof(Year)}," $"@{nameof(Year)},"
+ $"@{nameof(Month)}," + $"@{nameof(Month)},"
+ $"@{nameof(Day)}," + $"@{nameof(Day)},"
+ $"@{nameof(Hour)}," + $"@{nameof(Hour)},"
+ $"@{nameof(Minute)}," + $"@{nameof(Minute)},"
+ $"@{nameof(Second)}," + $"@{nameof(Second)},"
+ $"@{nameof(DateTimeString)}," + $"@{nameof(DateTimeString)},"
+ $"@{nameof(Currency)}," + $"@{nameof(Currency)},"
+ $"@{nameof(Amount)}," + $"@{nameof(Amount)},"
+ $"@{nameof(TransactionType)}," + $"@{nameof(TransactionType)},"
+ $"@{nameof(FeeCurrency)}," + $"@{nameof(FeeCurrency)},"
+ $"@{nameof(FeeAmount)}," + $"@{nameof(FeeAmount)},"
+ $"@{nameof(Platform)}," + $"@{nameof(Platform)},"
+ $"@{nameof(Note)}"; + $"@{nameof(Note)}";
variables = Regex.Replace(dbVariables, "@", "");
} }
public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }"; public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";
@ -122,11 +124,10 @@ namespace CryptoCalc
} }
public class Wallet : DB_Classes public class Wallet : DBClasses
{ {
#region Publics #region Publics
public override string dbVariables { get => base.dbVariables; set => base.dbVariables = value; } 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 override string DBTableName => "Wallets";
public int CreationYear { get; set; } public int CreationYear { get; set; }
@ -147,6 +148,7 @@ namespace CryptoCalc
public Wallet(int _year, int _month, int _day, string _platform, string _name, string _currency, float _balance, int _default, string _note) public Wallet(int _year, int _month, int _day, string _platform, string _name, string _currency, float _balance, int _default, string _note)
{ {
SaveDateTime();
CreationYear = _year; CreationYear = _year;
CreationMonth = _month; CreationMonth = _month;
CreationDay = _day; CreationDay = _day;
@ -166,25 +168,23 @@ namespace CryptoCalc
private void SetDBStrings() private void SetDBStrings()
{ {
dbVariables = DBVariables =
$"@{nameof(Year)}, " + $"@{nameof(Year)},"
$"@{nameof(Month)}," + + $"@{nameof(Month)},"
$"@{nameof(Day)}," + + $"@{nameof(Day)},"
$"@{nameof(Hour)}," + + $"@{nameof(Hour)},"
$"@{nameof(Minute)}," + + $"@{nameof(Minute)},"
$"@{nameof(Second)}," + + $"@{nameof(Second)},"
$"@{nameof(DateTimeString)}," + + $"@{nameof(DateTimeString)},"
$"@{nameof(CreationYear)}," + + $"@{nameof(CreationYear)},"
$"@{nameof(CreationMonth)}," + + $"@{nameof(CreationMonth)},"
$"@{nameof(CreationDay)}," + + $"@{nameof(CreationDay)},"
$"@{nameof(Platform)}," + + $"@{nameof(Platform)},"
$"@{nameof(Name)}," + + $"@{nameof(Name)},"
$"@{nameof(Currency)}," + + $"@{nameof(Currency)},"
$"@{nameof(Balance)}," + + $"@{nameof(Balance)},"
$"@{nameof(DefaultWallet)}," + + $"@{nameof(DefaultWallet)},"
$"@{nameof(Notes)}"; + $"@{nameof(Notes)}";
variables = Regex.Replace(dbVariables, "@", "");
} }
//public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }"; //public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";

View File

@ -5,21 +5,21 @@ using System.Data;
using System.Data.SQLite; using System.Data.SQLite;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using static CryptoCalc.DB_Classes; using static CryptoCalc.DBClasses;
namespace CryptoCalc namespace CryptoCalc
{ {
public class DBInteraction public class DBInteraction
{ {
public static void SaveData<T>(ref T _data) where T : DB_Classes public static void SaveData<T>(ref T _data) where T : DBClasses
{ {
if (_data is null) if (_data is null)
{ {
throw new ArgumentNullException(nameof(_data)); throw new ArgumentNullException(nameof(_data));
} }
using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(_data.DBTableName)); using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(_data.DBTableName));
cnn.Execute($"insert into {_data.DBTableName} {_data.DBVariables}", _data); cnn.Execute($"insert into {_data.DBTableName} {_data.DBSaveDataString}", _data);
Debug.WriteLine($"Saved {_data.DBTableName} DB data"); Debug.WriteLine($"Saved {_data.DBTableName} DB data");
} }

View File

@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows; using System.Windows;
using System.Diagnostics; using System.Diagnostics;
using static CryptoCalc.DB_Classes; using static CryptoCalc.DBClasses;
namespace CryptoCalc namespace CryptoCalc
{ {
@ -49,13 +49,7 @@ namespace CryptoCalc
private Transaction DummyTransaction() private Transaction DummyTransaction()
{ {
Transaction t = new(); Transaction t = new();
t.Year = DateTime.Now.Year; t.SaveDateTime();
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.Currency = "SOL"; t.Currency = "SOL";
t.Amount = Convert.ToSingle(30 * rand.NextDouble()); t.Amount = Convert.ToSingle(30 * rand.NextDouble());
t.TransactionType = "BUY"; t.TransactionType = "BUY";