home.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. let Watch = require('../models/Watch');
  2. /* Nunjucks custom filter */
  3. var nunjucks = require('nunjucks');
  4. var env = new nunjucks.Environment();
  5. /**
  6. * GET /
  7. */
  8. exports.index = function(req, res) {
  9. /*
  10. * Crazy code just to make a fresh look on the homepage everytime.
  11. * Very ugly solution, just temporary.
  12. */
  13. function random(low, high) {
  14. return Math.random() * (high - low) + low
  15. }
  16. random = random(0, 7)
  17. console.log(random);
  18. if (random > 0 && random < 1 ) {
  19. property = '-updatedAt'
  20. }
  21. if (random > 1 && random < 2 ) {
  22. property = 'updatedAt'
  23. }
  24. if (random > 2 && random < 3 ) {
  25. property = 'year'
  26. }
  27. if (random > 3 && random < 4 ) {
  28. property = '-year'
  29. }
  30. if (random > 4 && random < 5 ) {
  31. property = 'id'
  32. }
  33. if (random > 5 && random < 6 ) {
  34. property = '-id'
  35. }
  36. if (random > 6 && random < 7 ) {
  37. property = 'title'
  38. }
  39. if (random > 7 && random < 8 ) {
  40. property = '-title'
  41. }
  42. Promise.all([
  43. Watch.find({ $and : [{'featured': true}, {'top': 'hot'}]}, null, {sort: property}).limit(4),
  44. Watch.find({ $and : [{'featured': true}, {'tags': 'diadotrabalhador'}]}, null, {sort: property}).limit(4),
  45. Watch.find({ $and : [{'featured': true}, {'tags': 'ditadura'}]}, null, {sort: property}).limit(4*2),
  46. Watch.find({ $and : [{'featured': true}, {'top': 'new-l'}]}, null, {sort: property}).limit(4*2),
  47. Watch.find({'featured': true}, null, {sort: '-createdAt'}).limit(8),
  48. Watch.find({ $and : [{'featured': true}, {'top': 'top-l'}]}, null, {sort: property}).limit(4*4),
  49. Watch.find({ $and : [{'featured': true}, {'top': 'top-c'}]}, null, {sort: property}).limit(4*4),
  50. Watch.find({ $and : [{'featured': true}, {'top': 'top-s'}]}, null, {sort: property}).limit(4*4),
  51. Watch.find({ $and : [{'featured': true}, {'top': 'new-c'}]}, null, {sort: property}).limit(4*3)
  52. ]).then(([
  53. hot, diadotrabalhador, ditadura, new_l, fresh_all, top_l, top_c, top_s, new_c
  54. ]
  55. ) => {
  56. let options = {
  57. title: "Início",
  58. hot: hot,
  59. diadotrabalhador: diadotrabalhador,
  60. ditadura: ditadura,
  61. new_l: new_l,
  62. fresh_all: fresh_all,
  63. top_l: top_l,
  64. top_c: top_c,
  65. top_s: top_s,
  66. new_c: new_c
  67. }
  68. res.render('inicio', options);
  69. })
  70. };