Переглянути джерело

Start working on database processing.

Cixo 1 рік тому
батько
коміт
d835805c1b
2 змінених файлів з 96 додано та 0 видалено
  1. 58 0
      assets/database.js
  2. 38 0
      assets/parser.js

+ 58 - 0
assets/database.js

@@ -1,7 +1,65 @@
+import { element } from "./element.js";
+import { submission } from "./submission.js";
+
 class database {
+    #submissions;
+
     constructor() {
+        this.clean();
+    }
+
+    search(input) {
+        const phrase = input.trim();
+        const found = [];
         
+        this.submissions.forEach(name => {
+            const current = this.#submissions[name];
+
+            if (name.search(phrase) !== -1) {
+                found.push(current);
+            }
+
+            current.elemenets.forEach(name => {
+                if (name.search(phrase) !== -1) {
+                    found.push(current.get(name));
+                }
+            });
+        });
+
+        return found;
+    }
+    
+    clean() {
+        this.#submissions = {};
     }   
+
+    add(container) {
+        if (!(container instanceof submission)) {
+            throw "Trying to add something which is not an submission.";
+        }
+        
+        if (container.name in this.#submissions) {
+            throw "Trying to add submission which already exists.";
+        }   
+
+        this.#submissions[container.name] = container;
+    }
+
+    get submissions() {
+        return Object.keys(this.#submissions);
+    }
+
+    get(name) {
+        if (typeof(name) !== "string") {
+            throw "Submission name must be an string.";
+        }
+
+        if (!(name in this.#submissions)) {
+            throw "Trying to get submission which not exists.";
+        }
+
+        return this.#submissions[name];
+    }
 } 
 
 export { database };

+ 38 - 0
assets/parser.js

@@ -0,0 +1,38 @@
+import { element } from "./element.js";
+import { submission } from "./submission.js";
+
+class parser {
+    #source_url;
+    #database;
+
+    constructor(source_url) {
+        if (typeof(source_url) !== "string") {
+            throw "Source URL must be an string.";
+        }   
+
+        this.#source = source;
+        this.#database = undefined;
+    }
+
+    async load() {
+        const result = await fetch(this.#source);
+
+        if (!result.ok) {
+            throw "Can not load database from server.";
+        }
+
+        if (result.status !== 200) {
+            throw "Can not load database from server. Result not 200.";
+        }
+
+        const database = await result.json();
+
+        this.#fetch_result(database);
+    }
+
+    #fetch_result(database) {
+        
+    }
+}
+
+export { parser };