Эх сурвалжийг харах

Continue work on database.

cixo 11 сар өмнө
parent
commit
54eb97b940

+ 36 - 0
assets/database.js

@@ -0,0 +1,36 @@
+import { project } from "./project.js";
+import { element } from "./element.js";
+import { elements_list } from "./elements_list.js";
+import { submission } from "./submission.js";
+import { parser } from "./parser.js";
+
+class database {
+    #project;
+
+    constructor(content) {
+        if (!(content instanceof Object)) {
+            throw "Content to into database from must be an object.";
+        }
+
+        this.#load(content);
+    }
+
+    #load(content) {
+        const loader = new parser(content, "root");
+
+        this.#project = this.#parse_project(loader.get_parser("project"));
+    }
+
+    #parse_project(content) {
+        const name = content.get("name");
+        let description = "";
+
+        if (content.exists("description")) {
+            description = content.get("description");
+        }
+
+        return new project(name, description);
+    }
+}
+
+export { database };

+ 41 - 0
assets/parser.js

@@ -0,0 +1,41 @@
+import { type_manager } from "./functions.js";
+
+class parser {
+    #locale;
+    #content;
+
+    constructor(content, locale) {
+        if (!(content instanceof Object)) {
+            throw "Content to create parser from must be an object.";
+        }
+
+        if (type_manager.is_string(locale)) {
+            throw "Locale of the content to show errors must be string.";
+        }
+
+        this.#content = content;
+        this.#locale = locale;
+    }
+
+    exists(target) {
+        if (!type_manager.is_string(target)) {
+            throw "Name of the element to parse must be an string.";
+        }
+
+        return this.#content.hasOwnProperty(target);
+    }
+
+    get(target) {
+        if (!this.exists(target)) {
+            throw "Can not get " + target + " from " + this.#locale + ".";
+        }
+
+        return this.#content[target];
+    }
+
+    get_parser(target) {
+        return new self(this.get(target), this.#locale + "." + target);
+    }
+}
+
+export { parser };

+ 29 - 0
assets/project.js

@@ -0,0 +1,29 @@
+import { type_manager } from "./functions.js";
+
+class project {
+    #name;
+    #description;
+    
+    constructor(name, description) {
+        if (!type_manager.is_string(name)) {
+            throw "Name of the project must be an string.";
+        }
+
+        if (!type_manager.is_string(description)) {
+            throw "Description of the project must be an string.";
+        }
+
+        this.#name = name;
+        this.#description = description;
+    }
+
+    get name() {
+        return this.#name;
+    }
+
+    get description() {
+        return this.#description;
+    }
+}
+
+export { project };

+ 1 - 1
assets/submission.js

@@ -44,7 +44,7 @@ class submission {
     }
 
     get elements() {
-        return Array.from(this.#elements);
+        return this.#elements;
     }
 }