index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 GlobalsScheme = {
  8. fields: [
  9. {
  10. key: "name",
  11. type: "text"
  12. },
  13. {
  14. key: "en",
  15. type: "text"
  16. },
  17. {
  18. key: "gr",
  19. type: "text"
  20. }
  21. ]
  22. };
  23. class GlobalModule extends CrudModule {
  24. constructor(){
  25. super('/globals', [3,0,3,4,0], 'Globals', '', GlobalModule.Middleware);
  26. this.repository = RepositorySystem.create('Globals', GlobalsScheme);
  27. this.setRepo(this.repository);
  28. this.router = express.Router();
  29. this.init();
  30. }
  31. init() {
  32. let routes = [
  33. {
  34. type: 'custom',
  35. method: 'get',
  36. endpoint: '/dictionary',
  37. customFn: this.getDictionary.bind(this)
  38. }
  39. ];
  40. RoutingSystem.loadRoutes(routes, this.router, '/', GlobalModule.Middleware);
  41. Actions.on('ArraygetGlobals', this.parse.bind(this));
  42. Actions.on('getGlobals', this.parse.bind(this));
  43. Actions.on('postGlobals', this.prepare.bind(this));
  44. }
  45. getDictionary(req,res) {
  46. console.log("ASD");
  47. this.repository.getPage(1, 10000)
  48. .then((data) => {
  49. return data.reduce((initial, item) => {
  50. let value = item[req.language];
  51. initial[item.name] = value;
  52. return initial;
  53. }, {});
  54. })
  55. .then((data) => res.send(data));
  56. }
  57. prepare(glob, req, res) {
  58. glob[req.language] = glob.value;
  59. delete glob.value;
  60. return glob;
  61. }
  62. parse(glob, req, res) {
  63. let value = glob[req.language];
  64. return {
  65. name: glob.name,
  66. value
  67. }
  68. }
  69. }
  70. GlobalModule.Middleware = (req, res, next) => {
  71. let lang = req.headers.language || 'gr';
  72. req.language = lang;
  73. next();
  74. };
  75. module.exports = GlobalModule;