Browse Source

Add database downloader.

Cixo 11 months ago
parent
commit
db154a988b
2 changed files with 39 additions and 0 deletions
  1. 4 0
      assets/database.js
  2. 35 0
      assets/database_downloader.js

+ 4 - 0
assets/database.js

@@ -102,6 +102,10 @@ class database {
         return this.#project;
     }
 
+    get submissions() {
+        return this.#submissions;
+    }
+
     constructor(content) {
         if (!(content instanceof Object)) {
             throw "Content to into database from must be an object.";

+ 35 - 0
assets/database_downloader.js

@@ -0,0 +1,35 @@
+import { type_manager } from "./functions.js";
+import { database } from "./database.js";
+
+class database_downloader {
+    #url;
+    #database;
+
+    constructor(url) {
+        if (!type_manager.is_string(url)) {
+            throw "Url to the database must be an string.";
+        }
+
+        this.#url = url;
+    }
+
+    async download() {
+        const response = await fetch(this.#url);
+    
+        if (!response.ok) {
+            throw "Can not download database. Error: " + response.status;
+        }
+
+        if (response.headers.get("content-type") !== "application/json") {
+            throw "Response content type of the database must be an json.";
+        }
+
+        this.#database = new database(await response.json());
+    }
+
+    get result() {
+        return this.#database;
+    }
+}
+
+export { database_downloader };