products_loader.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { product } from "./product.js";
  2. export class products_loader {
  3. static async all() {
  4. const request = await fetch("/products/");
  5. const response = await request.json();
  6. return products_loader.#response_to_collection(response);
  7. }
  8. static #response_to_collection(response) {
  9. const result = new Array();
  10. if (response.result !== "success") {
  11. return result;
  12. }
  13. response.collection.forEach(serialized => {
  14. result.push(new product(serialized));
  15. });
  16. return result;
  17. }
  18. static async search_name(name) {
  19. return await products_loader.#search(
  20. "/product/search/name",
  21. name
  22. );
  23. }
  24. static async search_author(author) {
  25. return await products_loader.#search(
  26. "/product/search/author",
  27. author
  28. );
  29. }
  30. static async #search(path, parameter) {
  31. const coded = encodeURI(parameter);
  32. const request = await fetch(path + "/" + coded);
  33. const response = await request.json();
  34. return products_loader.#response_to_collection(response);
  35. }
  36. }