CryptoCalc/DBClasses.cs

246 lines
7.6 KiB
C#

using System;
using System.Globalization;
using System.Text.RegularExpressions;
namespace CryptoCalc
{
public enum TransactionTypes
{
Buy,
Sell,
Transfer,
Mining
}
public interface IDBClasses
{
static string DBName { get; }
string DBTableName { get; }
string DBVariables { get; }
string DBSaveDataString { get; }
}
public class DBClasses : IDBClasses
{
#region Protected
protected string dbVariables;
#endregion
#region Publics
public static string DBName => "CryptoCalc";
public virtual string DBTableName { get; }
public string DBVariables => dbVariables;
public string DBSaveDataString => $"({Regex.Replace(dbVariables, "@", "")}) values ({dbVariables})";
public long Index { get; set; }
public ulong UnixTime { get; set; }
#endregion
#region Functions
public void SaveUnixTimeNow()
{
UnixTime = (ulong)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalSeconds;
}
public static ulong GetUnixTime(DateTime dateTime)
{
return (ulong)dateTime.Subtract(DateTime.UnixEpoch).TotalSeconds;
}
public static DateTime GetUTCTimeFromUnixTime(ulong unix)
{
return DateTime.UnixEpoch.AddSeconds(unix).ToUniversalTime();
}
public static DateTime GetLocalTimeFromUnixTime(ulong unix)
{
return DateTime.UnixEpoch.AddSeconds(unix).ToLocalTime();
}
#endregion
}
public class Transaction : DBClasses
{
#region Publics
public override string DBTableName => "Transactions";
public long WalletID { get; set; }
public string Currency { get; set; }
public long Amount { get; set; }
public ulong AmountDecimal { get; set; }
public string TransactionType { get; set; }
public string FeeCurrency { get; set; }
public long FeeAmount { get; set; }
public ulong FeeAmountDecimal { get; set; }
public string Platform { get; set; }
public string Note { get; set; }
#endregion
#region Constructors
public Transaction() { SetDBStrings(); }
public Transaction(long walletID, string currency, decimal amount, string type)
{
SaveUnixTimeNow();
WalletID = walletID;
Currency = currency;
(Amount, AmountDecimal) = Util.SplitDecimal(amount);
TransactionType = type;
Platform = "";
Note = "";
SetDBStrings();
}
public Transaction(string currency, decimal amount, string type, string feeCurrency, decimal feeAmount)
{
SaveUnixTimeNow();
Currency = currency;
(Amount, AmountDecimal) = Util.SplitDecimal(amount);
TransactionType = type;
FeeCurrency = feeCurrency;
(FeeAmount, FeeAmountDecimal) = Util.SplitDecimal(feeAmount);
Platform = "";
Note = "";
SetDBStrings();
}
#endregion
#region Functions
private void SetDBStrings()
{
dbVariables =
$"@{nameof(WalletID)},"
+ $"@{nameof(UnixTime)},"
+ $"@{nameof(Currency)},"
+ $"@{nameof(Amount)},"
+ $"@{nameof(AmountDecimal)},"
+ $"@{nameof(TransactionType)},"
+ $"@{nameof(FeeCurrency)},"
+ $"@{nameof(FeeAmount)},"
+ $"@{nameof(FeeAmountDecimal)},"
+ $"@{nameof(Platform)},"
+ $"@{nameof(Note)}";
}
public string FullInfo => $"{ Index } {WalletID} { GetLocalTimeFromUnixTime(UnixTime) } { Currency } { Util.CombineDecimal(Amount, AmountDecimal) } { 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)}\" INTEGER NOT NULL,"
+ $"\"{nameof(AmountDecimal)}\" INTEGER NOT NULL,"
+ $"\"{nameof(TransactionType)}\" TEXT NOT NULL,"
+ $"\"{nameof(FeeCurrency)}\" TEXT,"
+ $"\"{nameof(FeeAmount)}\" INTEGER,"
+ $"\"{nameof(FeeAmountDecimal)}\" INTEGER,"
+ $"\"{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 DBTableName => "Wallets";
public ulong UnixTimeCreated { get; set; }
public string Platform { get; set; }
public string Name { get; set; }
public string Currency { get; set; }
public long Balance { get; set; }
public ulong BalanceDecimal { 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, string note)
{
SaveUnixTimeNow();
UnixTimeCreated = GetUnixTime(dateTime);
Platform = platform;
Name = name;
Currency = currency;
(Balance, BalanceDecimal) = Util.SplitDecimal(0);
DefaultWallet = 0;
Note = note;
SetDBStrings();
}
#endregion
#region Functions
private void SetDBStrings()
{
dbVariables =
$"@{nameof(UnixTime)},"
+ $"@{nameof(UnixTimeCreated)},"
+ $"@{nameof(Platform)},"
+ $"@{nameof(Name)},"
+ $"@{nameof(Currency)},"
+ $"@{nameof(Balance)},"
+ $"@{nameof(BalanceDecimal)},"
+ $"@{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)}\" INTEGER NOT NULL,"
+ $"\"{nameof(BalanceDecimal)}\" INTEGER NOT NULL,"
+ $"\"{nameof(DefaultWallet)}\" INTEGER,"
+ $"\"{nameof(Note)}\" TEXT,"
+ $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT)"
+ $"); ";
}
#endregion
}
}