diff --git a/backend/donation_tracker_canister/src/Main.mo b/backend/donation_tracker_canister/src/Main.mo index eb24f4f..05f4625 100644 --- a/backend/donation_tracker_canister/src/Main.mo +++ b/backend/donation_tracker_canister/src/Main.mo @@ -46,10 +46,8 @@ actor class DonationTracker(_donation_canister_id : Text) { stable var donationsStable : [Donation] = []; // Map each principal to a list of donation indices (DTIs) - private var donationsByPrincipal = HashMap.HashMap>(0, Principal.equal, Principal.hash); - //TODO: stable var donationsByPrincipalStable : [(Principal, Buffer.Buffer)] = []; - // Alternative: use [DTI] instead of Buffer.Buffer; less efficient but straightforward to make stable - // Or: stable Buffer implementation? + private var donationsByPrincipal = HashMap.HashMap(0, Principal.equal, Principal.hash); + stable var donationsByPrincipalStable : [(Principal, [DTI])] = []; // Alternative: Buffer.Buffer instead of [DTI]; more efficient but less straightforward to make stable // Store recipients and map each recipientId to the corresponding Recipient record private var recipientsById = HashMap.HashMap(0, Text.equal, Text.hash); @@ -222,12 +220,12 @@ actor class DonationTracker(_donation_canister_id : Text) { // Update the map for the caller's principal if (Principal.isAnonymous(msg.caller)) {} else { - let existingDonations = switch (donationsByPrincipal.get(msg.caller)) { - case (null) { Buffer.Buffer(0) }; + let existingDonations : [DTI] = switch (donationsByPrincipal.get(msg.caller)) { + case (null) { [] }; case (?ds) { ds }; }; - let addDonationResult = existingDonations.add(newDti); - donationsByPrincipal.put(msg.caller, existingDonations); + let addDonationResult : [DTI] = Array.append(existingDonations, [newDti]); + donationsByPrincipal.put(msg.caller, addDonationResult); }; let associatedDonations = switch (donationsByTxId.get(donationInput.paymentTransactionId)) { @@ -273,12 +271,12 @@ actor class DonationTracker(_donation_canister_id : Text) { // No donations found return #Ok({ donations = [] }); }; - case (?dtiBuffer) { + case (?dtiArray) { // Donations found for user - let dtis : [DTI] = Buffer.toArray(dtiBuffer); + let dtis : [DTI] = dtiArray; // Iterate over dtis, get donation for each dti // push to return array - let userDonations : Buffer.Buffer = Buffer.Buffer(dtiBuffer.capacity()); + let userDonations : Buffer.Buffer = Buffer.Buffer(dtiArray.size()); for (i : Nat in dtis.keys()) { userDonations.add(donations.get(dtis[i])); }; @@ -587,7 +585,7 @@ actor class DonationTracker(_donation_canister_id : Text) { system func preupgrade() { // Copy the runtime state back into the stable variable before upgrade. donationsStable := Buffer.toArray(donations); - //TODO: donationsByPrincipalStable := Iter.toArray(donationsByPrincipal.entries()); + donationsByPrincipalStable := Iter.toArray(donationsByPrincipal.entries()); recipientsByIdStable := Iter.toArray(recipientsById.entries()); studentsBySchoolStable := Iter.toArray(studentsBySchool.entries()); donationsByTxIdStable := Iter.toArray(donationsByTxId.entries()); @@ -599,8 +597,8 @@ actor class DonationTracker(_donation_canister_id : Text) { // After upgrade, reload the runtime state from the stable variable. donations := Buffer.fromArray(donationsStable); donationsStable := []; - //TODO: donationsByPrincipal := HashMap.fromIter(Iter.fromArray(donationsByPrincipalStable), donationsByPrincipalStable.size(), Text.equal, Text.hash); - //TODO: donationsByPrincipalStable := []; + donationsByPrincipal := HashMap.fromIter(Iter.fromArray(donationsByPrincipalStable), donationsByPrincipalStable.size(), Principal.equal, Principal.hash); + donationsByPrincipalStable := []; recipientsById := HashMap.fromIter(Iter.fromArray(recipientsByIdStable), recipientsByIdStable.size(), Text.equal, Text.hash); recipientsByIdStable := []; studentsBySchool := HashMap.fromIter(Iter.fromArray(studentsBySchoolStable), studentsBySchoolStable.size(), Text.equal, Text.hash);