SearchPage.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. genre: req.query.genre || '',
  15. page: parseInt(req.query.page, 10) || 1,
  16. };
  17. let entry = [];
  18. if (query.type) {
  19. if (['author', 'series', 'title'].includes(query.type)) {
  20. try {
  21. const from = query.type;
  22. const page = query.page;
  23. const limit = 100;
  24. const offset = (page - 1)*limit;
  25. const queryRes = await this.webWorker.search(from, {[from]: query.term, genre: query.genre, del: '0', offset, limit});
  26. const found = queryRes.found;
  27. for (let i = 0; i < found.length; i++) {
  28. const row = found[i];
  29. if (!row.bookCount)
  30. continue;
  31. entry.push(
  32. this.makeEntry({
  33. id: row.id,
  34. title: `${(from === 'series' ? 'Серия: ': '')}${from === 'author' ? this.bookAuthor(row[from]) : row[from]}`,
  35. link: this.navLink({href: `/${from}?${from}==${encodeURIComponent(row[from])}`}),
  36. content: {
  37. '*ATTRS': {type: 'text'},
  38. '*TEXT': `${row.bookCount} книг${utils.wordEnding(row.bookCount, 8)}`,
  39. },
  40. }),
  41. );
  42. }
  43. if (queryRes.totalFound > offset + found.length) {
  44. entry.push(
  45. this.makeEntry({
  46. id: 'next_page',
  47. title: '[Следующая страница]',
  48. link: this.navLink({href: `/${this.id}?type=${from}&term=${encodeURIComponent(query.term)}&page=${page + 1}`}),
  49. })
  50. );
  51. }
  52. } catch(e) {
  53. entry.push(
  54. this.makeEntry({
  55. id: 'error',
  56. title: `Ошибка: ${e.message}`,
  57. link: this.navLink({href: `/fake-error-link`}),
  58. })
  59. );
  60. }
  61. }
  62. } else {
  63. //корневой раздел
  64. entry = [
  65. this.makeEntry({
  66. id: 'search_author',
  67. title: 'Поиск авторов',
  68. link: this.navLink({href: `/${this.id}?type=author&term=${encodeURIComponent(query.term)}`}),
  69. content: {
  70. '*ATTRS': {type: 'text'},
  71. '*TEXT': `Искать по именам авторов`,
  72. },
  73. }),
  74. this.makeEntry({
  75. id: 'search_series',
  76. title: 'Поиск серий',
  77. link: this.navLink({href: `/${this.id}?type=series&term=${encodeURIComponent(query.term)}`}),
  78. content: {
  79. '*ATTRS': {type: 'text'},
  80. '*TEXT': `Искать по названиям серий`,
  81. },
  82. }),
  83. this.makeEntry({
  84. id: 'search_title',
  85. title: 'Поиск книг',
  86. link: this.navLink({href: `/${this.id}?type=title&term=${encodeURIComponent(query.term)}`}),
  87. content: {
  88. '*ATTRS': {type: 'text'},
  89. '*TEXT': `Искать по названиям книг`,
  90. },
  91. }),
  92. this.makeEntry({
  93. id: 'search_genre',
  94. title: 'Поиск книг в жанре',
  95. link: this.navLink({href: `/genre?from=search&term=${encodeURIComponent(query.term)}`}),
  96. content: {
  97. '*ATTRS': {type: 'text'},
  98. '*TEXT': `Искать по названиям книг в выбранном жанре`,
  99. },
  100. }),
  101. this.makeEntry({
  102. id: 'search_help',
  103. title: '[Памятка по поиску]',
  104. link: this.acqLink({href: `/search-help`}),
  105. content: {
  106. '*ATTRS': {type: 'text'},
  107. '*TEXT': `Описание формата поискового значения`,
  108. },
  109. }),
  110. ]
  111. }
  112. result.entry = entry;
  113. return this.makeBody(result, req);
  114. }
  115. }
  116. module.exports = SearchPage;