Skip to content

Commit

Permalink
feat: create stable to store all students
Browse files Browse the repository at this point in the history
  • Loading branch information
c0utin committed Sep 30, 2024
1 parent d9e1314 commit 975dd1a
Showing 1 changed file with 47 additions and 52 deletions.
99 changes: 47 additions & 52 deletions src/parsekoto_backend/main.mo
Original file line number Diff line number Diff line change
@@ -1,85 +1,80 @@
import Principal "mo:base/Principal";
import Float "mo:base/Float";
import Text "mo:base/Text";
import Int "mo:base/Int";
import Array "mo:base/Array";
import Blob "mo:base/Blob";
import Debug "mo:base/Debug";
import Float "mo:base/Float";
import JSON "mo:serde/JSON";

import Types "types";

actor {

stable var stable_decoded_student: [Types.StudentRecord] = [];

type networks_interface = actor {
get_raw_data: (Text) -> async Text;
get_stable_raw_json: () -> async Text;
};

var networks: ?networks_interface = null;
var networks: ?networks_interface = null;

public func setup_networks(c: Principal) {
networks := ?(actor (Principal.toText(c)) : networks_interface);
};

public func fetch_raw_data(url: Text) : async Text {
switch (networks) {
case (null) {
return "Networks not set up";
};
case (?n) {
return await n.get_raw_data(url);
};
case (null) { return "Networks not set up"; };
case (?n) { return await n.get_raw_data(url); };
}
};

public func fetch_stable_raw_json() : async Types.Result<Types.StudentRecord, Types.ParseError> {
switch (networks) {
case (?n) {

let decoded_text = await n.get_stable_raw_json();

let json_result = JSON.fromText(decoded_text, null);
let json_blob = switch (json_result) {
case (#ok(blob)) { blob };
case (#err(e)) {
return #err({ message = "Failed to parse JSON: " # e });
};
};

let student_record: ?Types.StudentRecord = from_candid(json_blob);
switch (student_record) {
case (?record) { return #ok(record) };
case null { return #err({ message = "Failed to convert JSON into StudentRecord" }) };
}

case (?n) {
let decoded_text = await n.get_stable_raw_json();

let json_result = JSON.fromText(decoded_text, null);
let json_blob = switch (json_result) {
case (#ok(blob)) { blob };
case (#err(e)) { return #err({ message = "Failed to parse JSON: " # e }); };
};

let student_record: ?Types.StudentRecord = from_candid(json_blob);
switch (student_record) {
case (?record) {
stable_decoded_student := Array.append(stable_decoded_student, [record]);
return #ok(record);
};
case null { return #err({ message = "Failed to convert JSON into StudentRecord" }); };
};
};
case null { return #err({ message = "Networks not set up" }); };
}
};

public query func get_stable_decoded_student(): async [Types.StudentRecord]{
return stable_decoded_student;
};



let mock: [Types.StudentRecord] = [
{
studytime = 10;
higher = "Yes";
absences = 2;
failures = "No";
},
{
studytime = 15;
higher = "No";
absences = 5;
failures = "Yes";
},
{
studytime = 8;
higher = "Yes";
absences = 1;
failures = "No";
}
];
let mock: [Types.StudentRecord] = [
{
studytime = 10;
higher = "Yes";
absences = 2;
failures = "No";
},
{
studytime = 15;
higher = "No";
absences = 5;
failures = "Yes";
},
{
studytime = 8;
higher = "Yes";
absences = 1;
failures = "No";
}
];

public func calculateDistance(a: Types.StudentRecord, b: Types.StudentRecord): async Float {
let studytimeDiff = Float.abs(Float.fromInt(a.studytime) - Float.fromInt(b.studytime));
Expand Down

0 comments on commit 975dd1a

Please sign in to comment.