product.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Represents a product with various attributes.
  3. *
  4. * @class
  5. * @property {string|null} name - The name of the product
  6. * @property {string|null} description - A description of the product
  7. * @property {string|null} author - The author or creator of the product
  8. * @property {string|null} image - An image URL or reference for the product
  9. * @property {number|null} stock_count - The current inventory count
  10. * @property {string|null} barcode - The unique barcode identifier
  11. */
  12. export class product {
  13. name;
  14. description;
  15. author;
  16. image;
  17. stock_count;
  18. barcode;
  19. thumbnail;
  20. on_stock;
  21. constructor(target) {
  22. this.name = null;
  23. this.description = null;
  24. this.author = null;
  25. this.image = null;
  26. this.stock_count = null;
  27. this.barcode = null;
  28. this.thumbnail = null;
  29. this.on_stock = null;
  30. if ("name" in target) this.name = target["name"];
  31. if ("description" in target) this.description = target["description"];
  32. if ("author" in target) this.author = target["author"];
  33. if ("image" in target) this.image = target["image"];
  34. if ("stock_count" in target) this.stock_count = target["stock_count"];
  35. if ("barcode" in target) this.barcode = target["barcode"];
  36. if ("thumbnail" in target) this.thumbnail = target["thumbnail"];
  37. if ("on_stock" in target) this.on_stock = target["on_stock"];
  38. try {
  39. this.stock_count = Number(this.stock_count);
  40. } catch {
  41. this.stock_count = 0;
  42. }
  43. try {
  44. this.on_stock = Number(this.on_stock);
  45. } catch {
  46. this.on_stock = 0;
  47. }
  48. }
  49. get dump() {
  50. const dumped = {
  51. "name": new String(this.name),
  52. "description": new String(this.description),
  53. "author": new String(this.author),
  54. "image": new String(this.image),
  55. "barcode": new String(this.barcode),
  56. "thumbnail": new String(this.thumbnail),
  57. "stock_count": new String(this.stock_count)
  58. };
  59. if (this.on_stock !== null) {
  60. dumped["on_stock"] = new String(this.on_stock);
  61. }
  62. return dumped;
  63. }
  64. get ready() {
  65. if (this.name === null || this.description === null) return false;
  66. if (this.author === null || this.image === null) return false;
  67. if (this.stock_count === null || this.barcode === null) return false;
  68. if (this.thumbnail === null) return false;
  69. return true;
  70. }
  71. copy() {
  72. return new product(this.dump);
  73. }
  74. }