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'); let ImagesScheme = { fields: [ { key: "url", type: "text" }, { key: "product_id", type: "integer", fk: { "table": "products", "key": "id" } } ] }; class ImagesModule extends CrudModule { constructor(){ super('/images', [3,0,3,4,0], 'Images'); this.repository = RepositorySystem.create('Images', ImagesScheme); this.products = RepositorySystem.create('Products'); this.setRepo(this.repository); // this.users = RepositorySystem.create('Users'); // You can get any repo you want!! this.router = express.Router(); this.init(); } init() { Actions.on('getProducts', (p) => this.decorate(p)); let routes = [{ type: 'custom', method: 'post', endpoint: '/uploadimage', customFn: (req, res) => this.upload(req, res) }]; RoutingSystem.loadRoutes(routes, this.router, '/Images'); } upload(req, res) { } decorate(product) { return this.repository.get('product_id', product.id) .then((images) => { if(images.length) { product.images = images.map((item) => item.url); } return product; }); } } module.exports = ImagesModule;