RoutingService.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const knex = require('../models/database');
  2. const FilterService = require('./FilterService');
  3. const paginator = require('../helpers/paginator');
  4. class RoutingService {
  5. constructor(router, table, personal = false){
  6. this.router = router;
  7. this.table = table;
  8. this.personal = personal;
  9. }
  10. searchFn(table, perPage = 10) {
  11. var search = (req,res) => {
  12. console.log(req.body)
  13. let page = req.params.page || 1;
  14. var query = this.basicQuery(req).select('*');
  15. if(req.body.length){
  16. query = FilterService.parseFilters(query, req.body);
  17. }
  18. return paginator(knex)(query, {
  19. perPage,
  20. page
  21. });
  22. }
  23. return search;
  24. }
  25. search(perPage = 10, presentationFn) {
  26. let router = this.router;
  27. let table = this.table;
  28. let ArrangedFunction = this.searchFn(table)
  29. console.log("hmm")
  30. let FullFn = (req,res) => ArrangedFunction(req, res)
  31. .then((result) => {
  32. if(presentationFn)
  33. result.data = result.data.map(presentationFn);
  34. return result;
  35. })
  36. .then((result) => {
  37. res.status(200).send(result);
  38. })
  39. .catch((e) => this.handleErrors(e, req, res));
  40. router.post('/search/:page(\\d+)/', FullFn);
  41. router.post('/search/', FullFn);
  42. }
  43. basicQuery(req) {
  44. if (this.personal && req.user.role < 3) {
  45. console.log('das');
  46. if (!req.user.id) {
  47. throw 401;
  48. }
  49. return knex.table(this.table).where('userId', req.user.id);
  50. }
  51. return knex.table(this.table);
  52. }
  53. crud(roles = [1,0,1,2]) {
  54. let router = this.router;
  55. router.get('/:id',(req,res) => {
  56. let ids = req.params.id;
  57. let multiple = false;
  58. if (ids.includes(',')) {
  59. ids = ids.split(',');
  60. multiple = true;
  61. } else {
  62. ids = [ids];
  63. }
  64. return this.checkUser(roles[1], req)
  65. .then(() => this.basicQuery(req).whereIn('id',ids))
  66. .then((data)=>{
  67. if (!data.length) throw 404;
  68. multiple ? res.send(data) : res.send(data[0]);
  69. })
  70. .catch((e) => this.handleErrors(e, req, res));
  71. });
  72. router.post('/',(req,res) => {
  73. return this.checkUser(roles[0], req)
  74. .then(() => this.basicQuery(req).insert(req.body))
  75. .then((data)=>{
  76. res.status(200).send(data);
  77. })
  78. .catch((e) => this.handleErrors(e, req, res));
  79. });
  80. router.put('/:id',(req,res) => {
  81. return this.checkUser(roles[2], req)
  82. .then(() => this.basicQuery(req).where('id',req.params.id).update(req.body))
  83. .then((data)=>{
  84. console.log(data);
  85. res.send(req.body);
  86. })
  87. .catch((e) => this.handleErrors(e, req, res));
  88. });
  89. router.delete('/:id',(req,res) => {
  90. return this.checkUser(roles[3], req)
  91. .then(() => this.basicQuery(req).where('id',req.params.id).del())
  92. .then((data)=>{
  93. res.sendStatus(204);
  94. })
  95. .catch((e) => this.handleErrors(e, req, res));
  96. });
  97. }
  98. // checkUserRole
  99. checkUser(role, req) {
  100. if (req.user.role < role) {
  101. return Promise.reject(401);
  102. }
  103. return Promise.resolve(true);
  104. }
  105. // ERROR HANDLING
  106. handleErrors(e, req, res) {
  107. switch(parseInt(e/100,10)) {
  108. case 4:
  109. console.log('Bad Request! Error:', e);
  110. res.sendStatus(e);
  111. break;
  112. case 5:
  113. console.log('Internal Server Error! Error:', e);
  114. res.status(e).send("Sorry my problem! Sorry... :(, im dyin./|\\-----");
  115. throw e;
  116. break;
  117. }
  118. console.log(e);
  119. throw e;
  120. }
  121. }
  122. module.exports = (router, table, personal) => new RoutingService(router, table, personal);