1 module konnexengine.money.blueprint; 2 3 import std.conv: to; 4 import std.uuid: UUID, sha1UUID; 5 import std.datetime: SysTime, Clock, DateTime; 6 import std.format; 7 8 import vibe.core.log: logInfo; 9 import money; 10 11 class Amount 12 { 13 import money; 14 15 this(string symbol, double amount) 16 { 17 this.timestamp = Clock.currTime(); 18 auto payload = "" ~ symbol ~ amount.to!string; 19 DateTime ts = cast(DateTime)timestamp; 20 this.id = sha1UUID(payload, sha1UUID(payload ~ ts.toISOExtString())); 21 this.symbol = symbol; 22 this.amount = amount; 23 } 24 25 UUID id; 26 string symbol; 27 double amount; 28 SysTime timestamp; 29 } 30 31 class Currency 32 { 33 this (string symbol, string name, ubyte decimals) { 34 this.id = sha1UUID(symbol, sha1UUID("name: " ~ name)); 35 this.symbol = symbol; 36 this.name = name; 37 this.decimals = decimals; 38 } 39 UUID id; 40 string symbol; 41 string name; 42 ubyte decimals; 43 } 44 45 /// `createAmountIn(T)(T t, double amount)`: `template function returns Amount` 46 Amount createAmountIn(T)(T t, double amount) { 47 return new Amount(t.symbol, amount); 48 } 49 /// 50 unittest { 51 auto GBP = new Currency("GBP", "Great British Pound", 2); 52 import vibe.core.log: logInfo; 53 logInfo("" ~ GBP.symbol ~ " - " ~ GBP.name ~ ""); 54 auto amount = createAmountIn!Currency(GBP, 1000); 55 logInfo(amount.id.to!string); 56 logInfo(amount.symbol.to!string); 57 logInfo(amount.amount.to!string); 58 auto ts = cast(DateTime) amount.timestamp; 59 logInfo(ts.toISOExtString()); 60 61 62 assert(is(typeof(GBP) == Currency)); 63 assert(is(typeof(amount) == Amount)); 64 } 65 66 /** `createMoneyIn (T)(T t)` 67 ```d 68 template function returns currency 69 ``` 70 */ 71 // string createMoneyIn (T)(T t) 72 // { 73 // alias CUR = currency!(t.symbol); 74 // logInfo(format("%.2f", CUR(t.amount))); 75 // return format("%.2f", CUR(t.amount)); 76 // } 77 /// 78 unittest { 79 alias GBP = currency!("GBP"); 80 // auto a = createAmountIn!Currency(GBP, 1000); 81 // auto money = createMoneyIn!Amount(a); 82 logInfo(format("%.2f", GBP(1000)+GBP(2341.64))); 83 // assert(is(typeof(money) == currency)); 84 // assert(is(typeof(a) == Amount)); 85 // assert(is(typeof(GBP) == currency)); 86 }