GenrePage.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 || '',
  12. section: req.query.section || '',
  13. };
  14. const entry = [];
  15. if (query.from) {
  16. if (query.section) {
  17. //выбираем подразделы
  18. const {genreSection} = await this.getGenres();
  19. const section = genreSection.get(query.section);
  20. if (section) {
  21. let id = 0;
  22. const all = [];
  23. for (const g of section) {
  24. all.push(g.value);
  25. entry.push(
  26. this.makeEntry({
  27. id: ++id,
  28. title: g.name,
  29. link: this.navLink({href: `/${encodeURIComponent(query.from)}?genre=${encodeURIComponent(g.value)}`}),
  30. })
  31. );
  32. }
  33. entry.unshift(
  34. this.makeEntry({
  35. id: 'whole_section',
  36. title: '[Весь раздел]',
  37. link: this.navLink({href: `/${encodeURIComponent(query.from)}?genre=${encodeURIComponent(all.join(','))}`}),
  38. })
  39. );
  40. }
  41. } else {
  42. //выбираем разделы
  43. const {genreTree} = await this.getGenres();
  44. let id = 0;
  45. for (const section of genreTree) {
  46. entry.push(
  47. this.makeEntry({
  48. id: ++id,
  49. title: section.name,
  50. link: this.navLink({href: `/genre?from=${encodeURIComponent(query.from)}&section=${encodeURIComponent(section.name)}`}),
  51. })
  52. );
  53. }
  54. }
  55. }
  56. result.entry = entry;
  57. return this.makeBody(result, req);
  58. }
  59. }
  60. module.exports = GenrePage;