CryptoCalc/DBClasses.cs

221 lines
6.8 KiB
C#

using System;
using System.Text.RegularExpressions;
namespace CryptoCalc
{
public interface IDBClasses
{
static string DBName { get; }
string DBTableName { get; }
string DBVariables { get; set; }
string DBSaveDataString { get; }
}
public class DBClasses : IDBClasses
{
#region Publics
public static string DBName => "CryptoCalc";
public virtual string DBTableName { get; }
public virtual string DBVariables { get; set; }
public string DBSaveDataString => $"({Regex.Replace(DBVariables, "@", "")}) values ({DBVariables})";
public int Index { get; set; }
public long UnixTime { get; set; }
#endregion
#region Functions
public void SaveUnixTimeNow()
{
UnixTime = (long)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalSeconds;
}
public static long GetUnixTime(DateTime dateTime)
{
return (long)dateTime.Subtract(DateTime.UnixEpoch).TotalSeconds;
}
public static DateTime GetUTCTimeFromUnixTime(long unix)
{
return DateTime.UnixEpoch.AddSeconds(unix).ToUniversalTime();
}
public static DateTime GetLocalTimeFromUnixTime(long unix)
{
return DateTime.UnixEpoch.AddSeconds(unix).ToLocalTime();
}
#endregion
}
public class Transaction : DBClasses
{
#region Publics
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 string TransactionType { get; set; }
public string FeeCurrency { get; set; }
public float FeeAmount { get; set; }
public string Platform { get; set; }
public string Note { get; set; }
#endregion
#region Constructors
public Transaction() { SetDBStrings(); }
public Transaction(string currency, float amount, string type)
{
SaveUnixTimeNow();
Currency = currency;
Amount = amount;
TransactionType = type;
Platform = "";
Note = "";
SetDBStrings();
}
public Transaction(string currency, float amount, string type, string feeCurrency, float feeAmount)
{
SaveUnixTimeNow();
Currency = currency;
Amount = amount;
TransactionType = type;
FeeCurrency = feeCurrency;
FeeAmount = feeAmount;
Platform = "";
Note = "";
SetDBStrings();
}
#endregion
#region Functions
private void SetDBStrings()
{
DBVariables =
$"@{nameof(WalletID)},"
+ $"@{nameof(UnixTime)},"
+ $"@{nameof(Currency)},"
+ $"@{nameof(Amount)},"
+ $"@{nameof(TransactionType)},"
+ $"@{nameof(FeeCurrency)},"
+ $"@{nameof(FeeAmount)},"
+ $"@{nameof(Platform)},"
+ $"@{nameof(Note)}";
}
public string FullInfo => $"{ Index } { 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
}
public class Wallet : DBClasses
{
#region Publics
public override string DBVariables { get => base.DBVariables; set => base.DBVariables = value; }
public override string DBTableName => "Wallets";
public long UnixTimeCreated { get; set; }
public string Platform { get; set; }
public string Name { get; set; }
public string Currency { get; set; }
public float Balance { get; set; }
public int DefaultWallet { get; set; }
public string Note { get; set; }
#endregion
#region Constructors
public Wallet() { SetDBStrings(); }
public Wallet(DateTime dateTime, string platform, string name, string currency, float balance, int defaultWallet, string note)
{
SaveUnixTimeNow();
UnixTimeCreated = GetUnixTime(dateTime);
Platform = platform;
Name = name;
Currency = currency;
Balance = balance;
DefaultWallet = defaultWallet;
Note = note;
SetDBStrings();
}
#endregion
#region Functions
private void SetDBStrings()
{
DBVariables =
$"@{nameof(UnixTime)},"
+ $"@{nameof(UnixTimeCreated)},"
+ $"@{nameof(Platform)},"
+ $"@{nameof(Name)},"
+ $"@{nameof(Currency)},"
+ $"@{nameof(Balance)},"
+ $"@{nameof(DefaultWallet)},"
+ $"@{nameof(Note)}";
}
//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
}
}