Bläddra i källkod

Add elements parser to database loader.

Cixo 11 månader sedan
förälder
incheckning
d1d3eaac3b
6 ändrade filer med 193 tillägg och 27 borttagningar
  1. 55 25
      assets/database.js
  2. 58 1
      assets/element.js
  3. 27 1
      assets/element_builder.js
  4. 4 0
      assets/functions.js
  5. 35 0
      assets/order.js
  6. 14 0
      tests/order.js

+ 55 - 25
assets/database.js

@@ -1,5 +1,7 @@
+import { type_manager } from "./functions.js";
 import { project } from "./project.js";
 import { element } from "./element.js";
+import { element_builder } from "./element_builder.js";
 import { elements_list } from "./elements_list.js";
 import { submission } from "./submission.js";
 import { submissions_list } from "./submissions_list.js";
@@ -9,32 +11,36 @@ class database {
     #project;
     #submissions;
 
-    constructor(content) {
-        if (!(content instanceof Object)) {
-            throw "Content to into database from must be an object.";
-        }
+    #parse_element(name, content) {
+        const builder = new element_builder(); 
 
-        this.#load(content);
-    }
+        builder.name = name;
+        builder.description = "";
 
-    #load(content) {
-        const loader = new parser(content, "root");
-        const project = loader.get_parser("project");
-        const submissions = loader.get_parser("submissions");
+        if (content.exists("description")) {
+            builder.description = content.get("description");
+        }
 
-        this.#project = this.#parse_project(project);
-        this.#submissions = this.#parse_submissions(submissions);
-    }
+        const adder = (property, how) => {
+            if (!content.exists(property)) {
+                return;
+            }
 
-    #parse_project(content) {
-        const name = content.get("name");
-        let description = "";
+            const list = content.get(property);
 
-        if (content.exists("description")) {
-            description = content.get("description");
+            if (!type_manager.is_array(list)) {
+                throw "Property " + property + " of " + name + " not array.";
+            }
+
+            list.forEach((item) => how(item));
         }
 
-        return new project(name, description);
+        adder("pictures", (target) => builder.add_picture(target)); 
+        adder("order", (target) => builder.add_order(target));
+        adder("meshes", (target) => builder.add_mesh(target));
+        adder("solids", (target) => builder.add_solid(target));
+
+        return builder.result;
     }
 
     #parse_elements(content) {
@@ -42,12 +48,8 @@ class database {
 
         content.properties.forEach(name => {
             const loader = content.get_parser(name);
-
-            const result = new element(
-                name
-            );
-
-            list.append(result);
+            
+            list.append(this.#parse_element(name, loader));
         });
 
         return list;
@@ -76,9 +78,37 @@ class database {
         return list;
     }
 
+    #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(project);
+        this.#submissions = this.#parse_submissions(submissions);
+    }
+
+    #parse_project(content) {
+        const name = content.get("name");
+        let description = "";
+
+        if (content.exists("description")) {
+            description = content.get("description");
+        }
+
+        return new project(name, description);
+    }
+
     get project() {
         return this.#project;
     }
+
+    constructor(content) {
+        if (!(content instanceof Object)) {
+            throw "Content to into database from must be an object.";
+        }
+
+        this.#load(content);
+    }
 }
 
 export { database };

+ 58 - 1
assets/element.js

@@ -2,18 +2,75 @@ import { type_manager } from "./functions.js";
 
 class element {
     #name;
+    #description;
+    #pictures;
+    #meshes;
+    #solids;
+    #order;
 
-    constructor(name) {
+    constructor(
+        name,
+        description,
+        pictures,
+        meshes,
+        solids,
+        order
+    ) {
         if (!type_manager.is_string(name)) {
             throw "Name of the element must be string.";
         }
 
+        if (!type_manager.is_string(description)) {
+            throw "Description of the element must be string.";
+        }
+
+        if (!type_manager.is_array(pictures)) {
+            throw "Pictures must be array of files.";
+        }
+
+        if (!type_manager.is_array(meshes)) {
+            throw "Meshes must be array of files.";
+        }
+
+        if (!type_manager.is_array(solids)) {
+            throw "Solids must be array of files.";
+        }
+
+        if (!type_manager.is_array(order)) {
+            throw "Order must be array of order.";
+        }
+
         this.#name = name;
+        this.#description = description;
+        this.#pictures = pictures;
+        this.#meshes = meshes;
+        this.#solids = solids;
+        this.#order = order;
     }
 
     get name() {
         return this.#name;
     }
+
+    get description() {
+        return this.#description;
+    }
+
+    get order() {
+        return [...this.#order];
+    }
+
+    get pictures() {
+        return [...this.#pictures];
+    }
+
+    get solids() {
+        return [...this.#solids];
+    }
+
+    get meshes() {
+        return [...this.#meshes];
+    }
 }
 
 export { element };

+ 27 - 1
assets/element_builder.js

@@ -1,5 +1,7 @@
 import { type_manager } from "./functions.js";
 import { element } from "./element.js";
+import { order } from "./order.js";
+import { file } from "./file.js";
 
 class element_builder {
     #name;
@@ -34,7 +36,31 @@ class element_builder {
         this.#description = target;
     }
 
-    add_solid(target) { 
+    add_solid(target) {
+        this.#solids.push(new file(target));
+    }
+
+    add_mesh(target) {
+        this.#meshes.push(new file(target));
+    }
+
+    add_picture(target) {
+        this.#pictures.push(new file(target));
+    }
+
+    add_order(target) {
+        this.#order.push(new order(target));
+    }
+
+    get result() {
+        return new element(
+            this.#name,
+            this.#description,
+            this.#pictures,
+            this.#meshes,
+            this.#solids,
+            this.#order
+        );
     }
 }
 

+ 4 - 0
assets/functions.js

@@ -18,6 +18,10 @@ class type_manager {
     static is_string(target) {
         return String(target) === target;
     }
+
+    static is_array(target) {
+        return target instanceof Array;
+    }
 }
 
 export { dom_manager, type_manager };

+ 35 - 0
assets/order.js

@@ -0,0 +1,35 @@
+import { type_manager } from "./functions.js";
+
+class order {
+    #shop;
+    #link;
+
+    constructor(link) {
+        if (!type_manager.is_string(link)) {
+            throw "Link to the order must be an string.";
+        }
+
+        this.#link = link.trim();
+
+        const link_without_protocol = link.split("//").pop();
+        const parts = link_without_protocol.split("/");
+        const name = parts.shift();
+        const subdomains = name.split(".");
+
+        if (subdomains[0].search("www") !== -1) {
+            subdomains.shift();
+        }
+
+        this.#shop = subdomains.join(".");
+    }
+
+    get shop() {
+        return this.#shop;
+    }
+
+    get link() {
+        return this.#link;
+    }
+}
+
+export { order };

+ 14 - 0
tests/order.js

@@ -0,0 +1,14 @@
+import { order } from "../assets/order.js";
+
+const check = (link) => {
+    const target = new order(link);
+
+    console.log("Input: " + link);
+    console.log("Shop: " + target.shop);
+    console.log("Link: " + target.link);
+    console.log("");
+};
+
+check("https://allegro.pl/oferta/lampa-sufitowa-listwa-szyna-spot-8x-gu10-premium-kinkiet-solidna-i-trwala-11299072947");
+check("https://www.ebay.pl/itm/355010415309?itmmeta=01JCDYH7KQCB6FE1ZZXVA5XW9P&hash=item52a8450acd%3Ag%3Asm0AAOSwE-Zk8Kc3");
+check("https://www.aliexpress.com/ssr/300001505/rEkz8fMwjY?spm=a2g0o.home.bigsave.2.edaf405fHQEGNG&disableNav=YES&pha_manifest=ssr&_immersiveMode=true&productIds=1005005827004033");