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
{
string dbVariables { get; set; }
string variables { get; set; }
string DBVariables { get; set; }
string DBTableName { get; }
string DBVariables { get; }
string DBSaveDataString { get; }
}
public class DB_Classes : IDBClasses
public class DBClasses : IDBClasses
{
public virtual string dbVariables { get; set ; }
public virtual string variables { get; set; }
#region Publics
public virtual string DBVariables { get; set; }
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 Year { get; set; }
@ -28,14 +27,31 @@ namespace CryptoCalc
public int Minute { get; set; }
public int Second { 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();
}
public class Transaction : DB_Classes
#endregion
}
public class Transaction : DBClasses
{
#region Publics
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 DBVariables { get => base.DBVariables; set => base.DBVariables = value; }
public override string DBTableName => "Transactions";
public string Currency { get; set; }
@ -54,13 +70,7 @@ namespace CryptoCalc
public Transaction(string _currency, float _amount, string _type)
{
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();
SaveDateTime();
Currency = _currency;
Amount = _amount;
TransactionType = _type;
@ -73,13 +83,7 @@ namespace CryptoCalc
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();
SaveDateTime();
Currency = _currency;
Amount = _amount;
TransactionType = _type;
@ -97,7 +101,7 @@ namespace CryptoCalc
private void SetDBStrings()
{
dbVariables =
DBVariables =
$"@{nameof(Year)},"
+ $"@{nameof(Month)},"
+ $"@{nameof(Day)},"
@ -112,8 +116,6 @@ namespace CryptoCalc
+ $"@{nameof(FeeAmount)},"
+ $"@{nameof(Platform)},"
+ $"@{nameof(Note)}";
variables = Regex.Replace(dbVariables, "@", "");
}
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
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 DBVariables { get => base.DBVariables; set => base.DBVariables = value; }
public override string DBTableName => "Wallets";
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)
{
SaveDateTime();
CreationYear = _year;
CreationMonth = _month;
CreationDay = _day;
@ -166,25 +168,23 @@ namespace CryptoCalc
private void SetDBStrings()
{
dbVariables =
$"@{nameof(Year)}, " +
$"@{nameof(Month)}," +
$"@{nameof(Day)}," +
$"@{nameof(Hour)}," +
$"@{nameof(Minute)}," +
$"@{nameof(Second)}," +
$"@{nameof(DateTimeString)}," +
$"@{nameof(CreationYear)}," +
$"@{nameof(CreationMonth)}," +
$"@{nameof(CreationDay)}," +
$"@{nameof(Platform)}," +
$"@{nameof(Name)}," +
$"@{nameof(Currency)}," +
$"@{nameof(Balance)}," +
$"@{nameof(DefaultWallet)}," +
$"@{nameof(Notes)}";
variables = Regex.Replace(dbVariables, "@", "");
DBVariables =
$"@{nameof(Year)},"
+ $"@{nameof(Month)},"
+ $"@{nameof(Day)},"
+ $"@{nameof(Hour)},"
+ $"@{nameof(Minute)},"
+ $"@{nameof(Second)},"
+ $"@{nameof(DateTimeString)},"
+ $"@{nameof(CreationYear)},"
+ $"@{nameof(CreationMonth)},"
+ $"@{nameof(CreationDay)},"
+ $"@{nameof(Platform)},"
+ $"@{nameof(Name)},"
+ $"@{nameof(Currency)},"
+ $"@{nameof(Balance)},"
+ $"@{nameof(DefaultWallet)},"
+ $"@{nameof(Notes)}";
}
//public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";

View File

@ -5,21 +5,21 @@ using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Linq;
using static CryptoCalc.DB_Classes;
using static CryptoCalc.DBClasses;
namespace CryptoCalc
{
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)
{
throw new ArgumentNullException(nameof(_data));
}
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");
}

View File

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