-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.mo
executable file
·65 lines (53 loc) · 1.79 KB
/
main.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import Principal "mo:base/Principal";
import Time "mo:base/Time";
import Buffer "mo:base/Buffer";
import Map "mo:base/HashMap";
import Debug "mo:base/Debug";
actor class LoanProvider() = this {
// Loan Application
public type LoanApplication = {
id: Nat;
firstname: Text;
lastname: Text;
zipcode: Text;
ssn: Text;
amount: Float;
term: Nat16;
created: Int;
};
// Loan Offer
public type LoanOffer = {
providerid: Principal;
providername: Text;
applicationid: Nat;
apr: Float;
created: Int;
};
type Offers<LoanOffer> = Buffer.Buffer<LoanOffer>;
stable var counter : Nat = 0;
let offers = Map.HashMap<Principal, Offers<LoanOffer>>(0, Principal.equal, Principal.hash);
public shared (msg) func apply(input : LoanApplication) : async LoanOffer {
counter += 1;
Debug.print("Loan Application for user " #Principal.toText(msg.caller));
let offer : LoanOffer = {
providerid = Principal.fromActor(this);
providername = "Loan Provider";
applicationid = counter;
apr = 3.14;
created = Time.now();
};
var userOffers : ?Offers<LoanOffer> = offers.get(msg.caller);
switch userOffers {
case (null) { var userOffer : Offers<LoanOffer> = Buffer.Buffer(0); userOffer.add(offer); offers.put(msg.caller, userOffer)};
case (?userOffer) { userOffer.add(offer); };
};
return offer;
};
public query (msg) func getOffers() : async [LoanOffer] {
var userOffers : ?Offers<LoanOffer> = offers.get(msg.caller);
switch userOffers {
case (null) { return [] };
case (?userOffer) { return userOffer.toArray() };
};
};
}