123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- const express = require('express');
- const path = require('path');
- const CrudModule = require('../../base/CrudModule');
- const RepositorySystem = require('../../systems/RepositorySystem');
- const RoutingSystem = require('../../systems/RoutingSystem');
- const Actions = require('../../systems/ActionSystem');
- const Filter = require('../Filters/FilterService');
- let ProductScheme = {
- fields: [
- {
- key: "name",
- type: "text"
- },
- {
- key:"active",
- type: "boolean"
- },
- {
- key: "price",
- type: "decimal"
- },
- {
- key: "wprice",
- type: "decimal"
- },
- {
- key: "quantity",
- type: "integer"
- },
- {
- key: "weight",
- type: "decimal"
- },
- {
- key: "description",
- type: "text"
- },
- {
- key: "shortdescription",
- type: "text"
- },
- {
- key: "reference",
- type: "text"
- },
- {
- key: "ean13",
- type: "text"
- },
- {
- key: "urlseo",
- type: "text"
- },
- {
- key: "image",
- type: "text"
- }
- ]
- };
- class ProductsModule extends CrudModule {
- constructor(){
- super('/products', [2,0,3,4,0], 'Products', 'fa-shopping-basket');
- this.repository = RepositorySystem.create('Products', ProductScheme);
- this.setRepo(this.repository);
- // this.users = RepositorySystem.create('Users'); // You can get any repo you want!!
- this.router = express.Router();
- this.init();
- this.Model = {
- ...this.Model,
- price: {
- Filter: {
- type: "range",
- options: [0, 200],
- name: "Price",
- field: 'price',
- order: 20
- }
- },
- name: {
- Filter: {
- type: "search",
- options: [],
- name: "Product Name",
- field: 'name',
- order: 10
- }
- },
- deleted: { disabled: true },
- description: {
- Filter: {
- type: "editor",
- options: [],
- name: "Description",
- field: 'description',
- order: -1
- }
- },
- shortdescription: {
- Filter: {
- type: "editor",
- options: [],
- name: "Short description",
- field: 'shortdescription',
- order: -1
- }
- },
- reference: {
- Filter: {
- type: "search",
- options: [],
- name: "Reference Code",
- field: 'reference',
- order: -1
- }
- },
- active: {
- Filter: {
- type: "select",
- options: [0,1],
- name: "Active",
- field: 'active',
- order: -1
- }
- }
- }
- }
- init() {
- let routes = [
- {
- type: 'custom',
- method: 'get',
- endpoint: '/select/:name',
- customFn: () => {}
- },
- {
- type: 'custom',
- method: 'get',
- endpoint: '/search/:search',
- customFn: (req, res) => {
- this.search(req, res);
- }
- }
- ];
- RoutingSystem.loadRoutes(routes, this.router, '/products');
- }
- search(req, res) {
- let searchParam = req.params.search;
-
- Filter.parseFilters(this.repository.raw(), req.filters).where((builder) =>
- searchParam.split(" ").reduce((res, opt) => {
- return res.where((b) => b.where('name', 'like', '%' + opt + '%').orWhere('description', 'like', '%' + opt + '%'));
- }, builder)
- )
- .then((data) => Actions.emit('getProduct', data))
- .then((data) => res.send(data));
- }
- }
- module.exports = ProductsModule;
|