SearchPage.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const BasePage = require('./BasePage');
  2. const utils = require('../utils');
  3. class SearchPage extends BasePage {
  4. constructor(config) {
  5. super(config);
  6. this.id = 'search';
  7. this.title = 'Поиск';
  8. }
  9. async body(req) {
  10. const result = {};
  11. const query = {
  12. type: req.query.type || '',
  13. term: req.query.term || '',
  14. page: parseInt(req.query.page, 10) || 1,
  15. };
  16. let entry = [];
  17. if (query.type) {
  18. if (['author', 'series', 'title'].includes(query.type)) {
  19. const from = query.type;
  20. const page = query.page;
  21. const limit = 100;
  22. const offset = (page - 1)*limit;
  23. const queryRes = await this.webWorker.search(from, {[from]: query.term, del: 0, offset, limit});
  24. const found = queryRes.found;
  25. for (let i = 0; i < found.length; i++) {
  26. const row = found[i];
  27. if (!row.bookCount)
  28. continue;
  29. entry.push(
  30. this.makeEntry({
  31. id: row.id,
  32. title: `${(from === 'series' ? 'Серия: ': '')}${row[from]}`,
  33. link: this.navLink({href: `/${from}?${from}==${encodeURIComponent(row[from])}`}),
  34. content: {
  35. '*ATTRS': {type: 'text'},
  36. '*TEXT': `${row.bookCount} книг${utils.wordEnding(row.bookCount, 8)}`,
  37. },
  38. }),
  39. );
  40. }
  41. if (queryRes.totalFound > offset + found.length) {
  42. entry.push(
  43. this.makeEntry({
  44. id: 'next_page',
  45. title: '[Следующая страница]',
  46. link: this.navLink({href: `/${this.id}?type=${from}&term=${encodeURIComponent(query.term)}&page=${page + 1}`}),
  47. }),
  48. );
  49. }
  50. }
  51. } else {
  52. //корневой раздел
  53. entry = [
  54. this.makeEntry({
  55. id: 'search_author',
  56. title: 'Поиск авторов',
  57. link: this.navLink({href: `/${this.id}?type=author&term=${encodeURIComponent(query.term)}`}),
  58. content: {
  59. '*ATTRS': {type: 'text'},
  60. '*TEXT': `Искать по именам авторов`,
  61. },
  62. }),
  63. this.makeEntry({
  64. id: 'search_series',
  65. title: 'Поиск серий',
  66. link: this.navLink({href: `/${this.id}?type=series&term=${encodeURIComponent(query.term)}`}),
  67. content: {
  68. '*ATTRS': {type: 'text'},
  69. '*TEXT': `Искать по названиям серий`,
  70. },
  71. }),
  72. this.makeEntry({
  73. id: 'search_title',
  74. title: 'Поиск книг',
  75. link: this.navLink({href: `/${this.id}?type=title&term=${encodeURIComponent(query.term)}`}),
  76. content: {
  77. '*ATTRS': {type: 'text'},
  78. '*TEXT': `Искать по названиям книг`,
  79. },
  80. }),
  81. ]
  82. }
  83. result.entry = entry;
  84. return this.makeBody(result, req);
  85. }
  86. }
  87. module.exports = SearchPage;