index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const express = require('express');
  2. const path = require('path');
  3. const CrudModule = require('../../base/CrudModule');
  4. const RepositorySystem = require('../../systems/RepositorySystem');
  5. const RoutingSystem = require('../../systems/RoutingSystem');
  6. const Actions = require('../../systems/ActionSystem');
  7. let ImagesScheme = {
  8. fields: [
  9. {
  10. key: "url",
  11. type: "text"
  12. },
  13. {
  14. key: "product_id",
  15. type: "integer",
  16. fk: {
  17. "table": "products",
  18. "key": "id"
  19. }
  20. }
  21. ]
  22. };
  23. class ImagesModule extends CrudModule {
  24. constructor(){
  25. super('/images', [3,0,3,4,0], 'Images');
  26. this.repository = RepositorySystem.create('Images', ImagesScheme);
  27. this.products = RepositorySystem.create('Products');
  28. this.setRepo(this.repository);
  29. // this.users = RepositorySystem.create('Users'); // You can get any repo you want!!
  30. this.router = express.Router();
  31. this.init();
  32. }
  33. init() {
  34. Actions.on('getProducts', (p) => this.decorate(p));
  35. let routes = [{
  36. type: 'custom',
  37. method: 'post',
  38. endpoint: '/uploadimage',
  39. customFn: (req, res) => this.upload(req, res)
  40. }];
  41. RoutingSystem.loadRoutes(routes, this.router, '/Images');
  42. }
  43. upload(req, res) {
  44. }
  45. decorate(product) {
  46. return this.repository.get('product_id', product.id)
  47. .then((images) => {
  48. if(images.length) {
  49. product.images = images.map((item) => item.url);
  50. }
  51. return product;
  52. });
  53. }
  54. }
  55. module.exports = ImagesModule;