Jelajahi Sumber

see also #379

ali 2 tahun lalu
induk
melakukan
b2fa4d0021
6 mengubah file dengan 348 tambahan dan 33 penghapusan
  1. 4 0
      src/api/APIGateway.js
  2. 100 0
      src/api/models/Package.js
  3. 101 0
      src/api/models/Product.js
  4. 52 33
      src/models/Media.js
  5. 38 0
      src/models/Package.js
  6. 53 0
      src/models/Product.js

+ 4 - 0
src/api/APIGateway.js

@@ -16,6 +16,8 @@ import PaymentAPI from 'src/api/models/Payment.js'
 import SessionAPI from 'src/api/models/Session.js'
 import ContentAPI from 'src/api/models/Content.js'
 import ProjectAPI from 'src/api/models/Project.js'
+import PackageAPI from 'src/api/models/Package.js'
+import ProductAPI from 'src/api/models/Product.js'
 import DonationAPI from 'src/api/models/Donation.js'
 import QuestionApi from 'src/api/models/Question.js'
 import ClassroomAPI from 'src/api/models/Classroom.js'
@@ -60,6 +62,8 @@ export const APIGateway = {
   wallet: new WalletAPI(),
   testSet: new TestSetAPI(),
   project: new ProjectAPI(),
+  package: new PackageAPI(),
+  product: new ProductAPI(),
   invoice: new InvoiceAPI(),
   payment: new PaymentAPI(),
   session: new SessionAPI(),

+ 100 - 0
src/api/models/Package.js

@@ -0,0 +1,100 @@
+import { appApi } from 'src/boot/axios.js'
+import APIRepository from '../classes/APIRepository.js'
+import { Package, PackageList } from 'src/models/Package.js'
+
+export default class PackageAPI extends APIRepository {
+  constructor() {
+    super('package', appApi)
+    this.APIAdresses = {
+      base: '/store/packages',
+      byId: (id) => '/store/packages/' + id
+    }
+    this.CacheList = {
+      base: this.name + this.APIAdresses.base
+    }
+    this.restUrl = (id) => this.APIAdresses.base + '/' + id
+    /* Setting the callback functions for the CRUD operations. */
+    this.setCrudCallbacks({
+      get: (response) => { return new Package(response.data) },
+      post: (response) => { return new Package(response.data) },
+      put: (response) => { return new Package(response.data) },
+      delete: (response) => { return response.data }
+    })
+  }
+
+  index(data) {
+    return this.sendRequest({
+      apiMethod: 'get',
+      api: this.api,
+      request: this.APIAdresses.base,
+      data: this.getNormalizedSendData({
+        category: null, // Number
+        per_page: 10, // Number
+        page: 1 // Number
+      }, data),
+      resolveCallback: (response) => {
+        const paginate = response.data
+        const results = response.data.results
+        delete paginate.results
+        return {
+          list: new PackageList(results),
+          paginate
+          // {
+          //   "count": 1,
+          //   "num_pages": 1,
+          //   "per_page": 10,
+          //   "current": 1,
+          //   "next": null,
+          //   "previous": null,
+          // }
+        }
+      },
+      rejectCallback: (error) => {
+        return error
+      }
+    })
+  }
+
+  get (id) {
+    return this.sendRequest({
+      apiMethod: 'get',
+      api: this.api,
+      request: this.APIAdresses.byId(id),
+      resolveCallback: (response) => {
+        return new Package(response.data)
+      },
+      rejectCallback: (error) => {
+        return error
+      }
+    })
+  }
+
+  add (data) {
+    return this.sendRequest({
+      apiMethod: 'post',
+      api: this.api,
+      request: this.APIAdresses.base,
+      data: this.getNormalizedSendData({
+        store_category: null, // Number
+        content_category: null, // Number
+        inventory: 0, // Number
+        unit_price: 0, // Number
+        discount_percent: 0, // Number
+        maximum_order_count: 0, // Number
+        weight: 0, // Number
+        thumbnail: null, // String
+        title: null, // String
+        description: null, // String
+        specification: null, // String
+        products: [], // Array of Number
+        page: 1 // Number
+      }, data),
+      resolveCallback: (response) => {
+        return new Package(response.data)
+      },
+      rejectCallback: (error) => {
+        return error
+      }
+    })
+  }
+}

+ 101 - 0
src/api/models/Product.js

@@ -0,0 +1,101 @@
+import { appApi } from 'src/boot/axios.js'
+import APIRepository from '../classes/APIRepository.js'
+import { Product, ProductList } from 'src/models/Product.js'
+
+export default class ProductAPI extends APIRepository {
+  constructor() {
+    super('product', appApi)
+    this.APIAdresses = {
+      base: '/store/products',
+      byId: (id) => '/store/products/' + id
+    }
+    this.CacheList = {
+      base: this.name + this.APIAdresses.base
+    }
+    this.restUrl = (id) => this.APIAdresses.base + '/' + id
+    /* Setting the callback functions for the CRUD operations. */
+    this.setCrudCallbacks({
+      get: (response) => { return new Product(response.data) },
+      post: (response) => { return new Product(response.data) },
+      put: (response) => { return new Product(response.data) },
+      delete: (response) => { return response.data }
+    })
+  }
+
+  index(data) {
+    return this.sendRequest({
+      apiMethod: 'get',
+      api: this.api,
+      request: this.APIAdresses.base,
+      data: this.getNormalizedSendData({
+        category: null, // Number
+        per_page: 10, // Number
+        page: 1 // Number
+      }, data),
+      resolveCallback: (response) => {
+        const paginate = response.data
+        const results = response.data.results
+        delete paginate.results
+        return {
+          list: new ProductList(results),
+          paginate
+          // {
+          //   "count": 1,
+          //   "num_pages": 1,
+          //   "per_page": 10,
+          //   "current": 1,
+          //   "next": null,
+          //   "previous": null,
+          // }
+        }
+      },
+      rejectCallback: (error) => {
+        return error
+      }
+    })
+  }
+
+  get (id) {
+    return this.sendRequest({
+      apiMethod: 'get',
+      api: this.api,
+      request: this.APIAdresses.byId(id),
+      resolveCallback: (response) => {
+        return new Product(response.data)
+      },
+      rejectCallback: (error) => {
+        return error
+      }
+    })
+  }
+
+  add (data) {
+    return this.sendRequest({
+      apiMethod: 'post',
+      api: this.api,
+      request: this.APIAdresses.base,
+      data: this.getNormalizedSendData({
+        store_category: null, // Number
+        content_category: null, // Number
+        inventory: 0, // Number
+        unit_price: 0, // Number
+        discount_percent: 0, // Number
+        maximum_order_count: 0, // Number
+        weight: 0, // Number
+        thumbnail: null, // String
+        title: null, // String
+        description: null, // String
+        physical_type: null, // String
+        specification: null, // String
+        is_physical: false, // Boolean
+        page: 1 // Number
+      }, data),
+      resolveCallback: (response) => {
+        return new Product(response.data)
+      },
+      rejectCallback: (error) => {
+        return error
+      }
+    })
+  }
+}

+ 52 - 33
src/models/Media.js

@@ -30,41 +30,60 @@ class Media extends Model {
         relatedModel: MediaHashtagList
       },
       { key: 'file' }
-    ])
-
-    this.typeEnums = [
-      {
-        label: 'ویدیو',
-        value: 'VIDEO'
-      },
-      // {
-      //   label: 'متن',
-      //   value: 'TEXT'
-      // },
-      {
-        label: 'صوت',
-        value: 'SOUND'
-      },
-      {
-        label: 'تصویر',
-        value: 'IMAGE'
+    ], {
+      type: {
+        infoKey: 'type_info',
+        enumListKey: 'typeEnums',
+        enums: [
+          {
+            label: 'ویدیو',
+            value: 'VIDEO'
+          },
+          // {
+          //   label: 'متن',
+          //   value: 'TEXT'
+          // },
+          {
+            label: 'صوت',
+            value: 'SOUND'
+          },
+          {
+            label: 'تصویر',
+            value: 'IMAGE'
+          },
+          {
+            label: 'سند',
+            value: 'DOCUMENT'
+          }
+        ]
       },
-      {
-        label: 'سند',
-        value: 'DOCUMENT'
+      source_type: {
+        infoKey: 'source_type_info',
+        enumListKey: 'source_typeEnums',
+        enums: [
+          {
+            label: 'ویدیو',
+            value: 'VIDEO'
+          },
+          {
+            label: 'متن',
+            value: 'TEXT'
+          },
+          {
+            label: 'صوت',
+            value: 'SOUND'
+          },
+          {
+            label: 'عکس',
+            value: 'IMAGE'
+          },
+          {
+            label: 'محصول',
+            value: 'PRODUCT'
+          }
+        ]
       }
-    ]
-
-    this.loadTypeInfo()
-  }
-
-  loadTypeInfo () {
-    const target = this.typeEnums.find(type => type.value === this.type)
-    if (!target) {
-      this.type_info = null
-    } else {
-      this.type_info = target
-    }
+    })
   }
 }
 

+ 38 - 0
src/models/Package.js

@@ -0,0 +1,38 @@
+import { ProductList } from 'src/models/Product'
+import { Collection, Model } from 'js-abstract-model'
+
+class Package extends Model {
+  constructor (data) {
+    super(data, [
+      { key: 'id' },
+      { key: 'thumbnail' },
+      { key: 'title' },
+      { key: 'inventory' },
+      { key: 'store_category' },
+      { key: 'content_category' },
+      { key: 'unit_price' },
+      { key: 'discount_percent' },
+      { key: 'description' },
+      { key: 'maximum_order_count' },
+      { key: 'weight' },
+      { key: 'specification' },
+      { key: 'products' },
+      {
+        key: 'products_info',
+        relatedModel: ProductList
+      }
+    ])
+
+    if (!this.thumbnail) {
+      this.thumbnail = '/img/sample-post-thumbnail.jpg'
+    }
+  }
+}
+
+class PackageList extends Collection {
+  model () {
+    return Package
+  }
+}
+
+export { Package, PackageList }

+ 53 - 0
src/models/Product.js

@@ -0,0 +1,53 @@
+import { Collection, Model } from 'js-abstract-model'
+
+class Product extends Model {
+  constructor (data) {
+    super(data, [
+      { key: 'id' },
+      { key: 'thumbnail' },
+      { key: 'title' },
+      { key: 'inventory' },
+      { key: 'store_category' },
+      { key: 'content_category' },
+      { key: 'unit_price' },
+      { key: 'discount_percent' },
+      { key: 'description' },
+      { key: 'maximum_order_count' },
+      { key: 'is_physical' },
+      { key: 'weight' },
+      { key: 'physical_type' },
+      { key: 'specification' }
+    ], {
+      physical_type: {
+        infoKey: 'physical_type_info',
+        enumListKey: 'physical_typeEnums',
+        enums: [
+          {
+            label: 'سی دی',
+            value: 'CD'
+          },
+          {
+            label: 'کتاب',
+            value: 'BOOK'
+          },
+          {
+            label: 'جزوه',
+            value: 'MANUSCRIPT'
+          }
+        ]
+      }
+    })
+
+    if (!this.thumbnail) {
+      this.thumbnail = '/img/sample-post-thumbnail.jpg'
+    }
+  }
+}
+
+class ProductList extends Collection {
+  model () {
+    return Product
+  }
+}
+
+export { Product, ProductList }