GenrePage.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const BasePage = require('./BasePage');
  2. class GenrePage extends BasePage {
  3. constructor(config) {
  4. super(config);
  5. this.id = 'genre';
  6. this.title = 'Жанры';
  7. }
  8. async body(req) {
  9. const result = {};
  10. const query = {
  11. from: req.query.from || 'search',
  12. term: req.query.term || '*',
  13. section: req.query.section || '',
  14. };
  15. let searchQuery = '';
  16. if (query.from == 'search')
  17. searchQuery = `&type=title&term=${encodeURIComponent(query.term)}`;
  18. const entry = [];
  19. if (query.from) {
  20. if (query.section) {
  21. //выбираем подразделы
  22. const {genreSection} = await this.getGenres();
  23. const section = genreSection.get(query.section);
  24. if (section) {
  25. let id = 0;
  26. const all = [];
  27. for (const g of section) {
  28. all.push(g.value);
  29. entry.push(
  30. this.makeEntry({
  31. id: ++id,
  32. title: g.name,
  33. link: this.navLink({href: `/${encodeURIComponent(query.from)}?genre=${encodeURIComponent(g.value)}${searchQuery}`}),
  34. })
  35. );
  36. }
  37. entry.unshift(
  38. this.makeEntry({
  39. id: 'whole_section',
  40. title: '[Весь раздел]',
  41. link: this.navLink({href: `/${encodeURIComponent(query.from)}?genre=${encodeURIComponent(all.join(','))}${searchQuery}`}),
  42. })
  43. );
  44. }
  45. } else {
  46. //выбираем разделы
  47. const {genreTree} = await this.getGenres();
  48. let id = 0;
  49. for (const section of genreTree) {
  50. entry.push(
  51. this.makeEntry({
  52. id: ++id,
  53. title: section.name,
  54. link: this.navLink({href: `/genre?from=${encodeURIComponent(query.from)}&section=${encodeURIComponent(section.name)}${searchQuery}`}),
  55. })
  56. );
  57. }
  58. }
  59. }
  60. result.entry = entry;
  61. return this.makeBody(result, req);
  62. }
  63. }
  64. module.exports = GenrePage;