database.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { project } from "./project.js";
  2. import { element } from "./element.js";
  3. import { elements_list } from "./elements_list.js";
  4. import { submission } from "./submission.js";
  5. import { submissions_list } from "./submissions_list.js";
  6. import { parser } from "./parser.js";
  7. class database {
  8. #project;
  9. #submissions;
  10. constructor(content) {
  11. if (!(content instanceof Object)) {
  12. throw "Content to into database from must be an object.";
  13. }
  14. this.#load(content);
  15. }
  16. #load(content) {
  17. const loader = new parser(content, "root");
  18. const project = loader.get_parser("project");
  19. const submissions = loader.get_parser("submissions");
  20. this.#project = this.#parse_project(project);
  21. this.#submissions = this.#parse_submissions(submissions);
  22. }
  23. #parse_project(content) {
  24. const name = content.get("name");
  25. let description = "";
  26. if (content.exists("description")) {
  27. description = content.get("description");
  28. }
  29. return new project(name, description);
  30. }
  31. #parse_elements(content) {
  32. const list = new elements_list();
  33. content.properties.forEach(name => {
  34. const loader = content.get_parser(name);
  35. const result = new element(
  36. name
  37. );
  38. list.append(result);
  39. });
  40. return list;
  41. }
  42. #parse_submissions(content) {
  43. const list = new submissions_list();
  44. content.properties.forEach(title => {
  45. const loader = content.get_parser(title);
  46. const description = loader.get("description");
  47. const picture = loader.get("picture");
  48. const elements_parser = loader.get_parser("elements");
  49. const elements = this.#parse_elements(elements_parser);
  50. const result = new submission(
  51. title,
  52. description,
  53. picture,
  54. elements
  55. );
  56. list.append(result);
  57. });
  58. return list;
  59. }
  60. get project() {
  61. return this.#project;
  62. }
  63. }
  64. export { database };