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

Continue working on database processing.

Cixo 11 сар өмнө
parent
commit
8ae1ad19c1

+ 1 - 1
assets/app_parts.js

@@ -26,7 +26,7 @@ class submission_part {
         this.#applet_close = this.#description.querySelector(close_selector);
 
         if (!dom_manager.is_element(this.#header)) {
-            throw "Could not found header by selector.";
+            throw new Error("Could not found header by selector.");
         }
 
         if (!dom_manager.is_element(this.#image)) {

+ 49 - 1
assets/database.js

@@ -2,10 +2,12 @@ import { project } from "./project.js";
 import { element } from "./element.js";
 import { elements_list } from "./elements_list.js";
 import { submission } from "./submission.js";
+import { submissions_list } from "./submissions_list.js";
 import { parser } from "./parser.js";
 
 class database {
     #project;
+    #submissions;
 
     constructor(content) {
         if (!(content instanceof Object)) {
@@ -17,8 +19,11 @@ class database {
 
     #load(content) {
         const loader = new parser(content, "root");
+        const project = loader.get_parser("project");
+        const submissions = loader.get_parser("submissions");
 
-        this.#project = this.#parse_project(loader.get_parser("project"));
+        this.#project = this.#parse_project(project);
+        this.#submissions = this.#parse_submissions(submissions);
     }
 
     #parse_project(content) {
@@ -31,6 +36,49 @@ class database {
 
         return new project(name, description);
     }
+
+    #parse_elements(content) {
+        const list = new elements_list();
+
+        content.properties.forEach(name => {
+            const loader = content.get_parser(name);
+
+            const result = new element(
+                name
+            );
+
+            list.append(result);
+        });
+
+        return list;
+    }
+
+    #parse_submissions(content) {
+        const list = new submissions_list();
+
+        content.properties.forEach(title => {
+            const loader = content.get_parser(title);
+            const description = loader.get("description");
+            const picture = loader.get("picture");
+            const elements_parser = loader.get_parser("elements");
+            const elements = this.#parse_elements(elements_parser);
+
+            const result = new submission(
+                title,
+                description,
+                picture,
+                elements
+            );
+
+            list.append(result);
+        });
+
+        return list;
+    }
+
+    get project() {
+        return this.#project;
+    }
 }
 
 export { database };

+ 7 - 3
assets/parser.js

@@ -9,8 +9,8 @@ class parser {
             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.";
+        if (!type_manager.is_string(locale)) {
+            throw "Location of potential missing object must be string.";
         }
 
         this.#content = content;
@@ -33,8 +33,12 @@ class parser {
         return this.#content[target];
     }
 
+    get properties() {
+        return Object.getOwnPropertyNames(this.#content);
+    }
+
     get_parser(target) {
-        return new self(this.get(target), this.#locale + "." + target);
+        return new parser(this.get(target), this.#locale + "." + target);
     }
 }
 

+ 48 - 0
assets/submissions_list.js

@@ -0,0 +1,48 @@
+import { type_manager } from "./functions.js";
+import { submission } from "./submission.js";
+
+class submissions_list {
+    #content;
+
+    constructor() {
+        this.#content = {};
+    }
+
+    append(target) {
+        if (!(target instanceof submission)) {
+            throw "New target on list must be instance of an submission.";
+        }   
+
+        if (this.exists(target)) {
+            throw "This submission already exists on list.";
+        }   
+
+        this.#content[target.title] = target;
+    }
+
+    exists(target) {
+        if (target instanceof submission) {
+            target = target.title;
+        }
+
+        if (!(type_manager.is_string(target))) {
+            throw "Target must be instance of string or submission.";
+        }
+
+        return this.#content.hasOwnProperty(target);
+    }
+
+    get(target) {
+        if (!this.exists(target)) {
+            throw "Submission with that name not exists in the list.";
+        }
+
+        return this.#content[target];
+    }
+
+    get content() {
+        return Object.assign({}, this.#content);
+    }
+}
+
+export { submissions_list };

+ 39 - 0
tests/database.js

@@ -0,0 +1,39 @@
+import { database } from "../assets/database.js";
+
+const sample = {
+    "project": {
+        "name": "Sample test project",
+        "description": "This is super simple test project."
+    },
+
+    "submissions": {
+        "First": {
+            "description": "This is first test submission.",
+            "picture": "thumbnail_1.png",
+            "elements": {
+                "what": {},
+                "is": {},
+                "it": {}
+            }
+        },
+        
+        "Second": {
+            "description": "This is second test submission.",
+            "picture": "thumbnail_2.png",
+            "elements": {
+                "mini": {},
+                "maxi": {}
+            }
+        }
+    }
+};
+
+console.log("Loading database...");
+
+try {
+    const test_db = new database(sample);
+} catch (exception) {
+    console.error(exception);
+}
+
+console.log("Database loaded.");