Browse Source

Add loaded for database.

cixo 1 year ago
parent
commit
10d25fd916
2 changed files with 83 additions and 1 deletions
  1. 1 1
      assets/database.js
  2. 82 0
      assets/loader.js

+ 1 - 1
assets/database.js

@@ -1,6 +1,6 @@
 class database {
     constructor() {
-        
+               
     }   
 } 
 

+ 82 - 0
assets/loader.js

@@ -0,0 +1,82 @@
+import { project } from "./project.js";
+import { submission } from "./submission.js";
+
+class loader {
+    #load_from;
+    #database;
+    #content;
+
+    constructor(database, project) {
+        if (typeof(database) !== "string") {
+            throw "Database URL must be string.";
+        }
+
+        if (typeof(project) !== "string") {
+            throw "Project file must be string.";
+        }
+
+        this.#database = database;
+        this.#load_from = database + "/" + project;
+        this.#content = null;
+    }
+
+    async load() {
+        const fetched = await fetch(this.#load_from);
+        const result = await fetched.json();
+
+        this.#content = result;
+    }
+
+    get loaded() {
+        if (this.#content === null) {
+            throw "Must load database before trying to access it.";
+        }
+        
+        const content = this.#content;
+        const result = new project(content.name);
+
+        if (typeof(content.description) !== "undefined") {
+            result.description = content.description;
+        }
+
+        if (typeof(content.submissions) === "undefined") {
+            return result;
+        }
+
+        const submissions = Array.from(content.submissions);
+
+        submissions.forEach(submission => {
+            const item = this.#prepare_one(submission);
+
+            if (item === null) {
+                return;
+            }
+
+            result.add(item);
+        });
+    }
+
+    #prepare_image(url) {
+        return this.#database + "/" + url;
+    }
+
+    #prepare_one(input) {
+        if (typeof(input.name) === "undefined") {
+            return null;
+        }
+
+        const submission = new submission(input.name)
+        
+        if (typeof(input.description) !== "undefined") {
+            submission.description = input.description;
+        }
+
+        if (typeof(input.thumbnail) !== "undefined") {
+            submission.thumbnail = this.#prepare_image(input.thumbnail);
+        }
+
+        return submission;
+    }
+}
+
+export { loader };