Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

91 Zeilen
3.3 KiB

  1. import fs from 'fs/promises';
  2. import path from 'path';
  3. import { Portal, Card } from '@/types';
  4. const DATA_DIR = path.join(process.cwd(), 'data');
  5. const PORTALS_FILE = path.join(DATA_DIR, 'portals.txt');
  6. const CARDS_FILE = path.join(DATA_DIR, 'cards.txt');
  7. // Helper to ensure files/folders exist
  8. async function ensureDb() {
  9. try { await fs.access(DATA_DIR); } catch { await fs.mkdir(DATA_DIR); }
  10. try { await fs.access(PORTALS_FILE); } catch { await fs.writeFile(PORTALS_FILE, '[]'); }
  11. try { await fs.access(CARDS_FILE); } catch { await fs.writeFile(CARDS_FILE, '[]'); }
  12. const FONTS_DIR = path.join(DATA_DIR, 'fonts');
  13. try { await fs.access(FONTS_DIR); } catch { await fs.mkdir(FONTS_DIR, { recursive: true }); }
  14. }
  15. export async function getPortals(): Promise<Portal[]> {
  16. await ensureDb();
  17. const data = await fs.readFile(PORTALS_FILE, 'utf-8');
  18. return JSON.parse(data || '[]');
  19. }
  20. export async function getCards(portalId?: string): Promise<Card[]> {
  21. await ensureDb();
  22. const data = await fs.readFile(CARDS_FILE, 'utf-8');
  23. let cards: Card[] = JSON.parse(data || '[]');
  24. // Backward-compat: convert old string[] extraImages → MediaItem[] extraMedia
  25. cards = cards.map(c => {
  26. const legacy = (c as any).extraImages;
  27. if (Array.isArray(legacy) && !c.extraMedia) {
  28. c.extraMedia = legacy.map((url: string) => ({ url }));
  29. delete (c as any).extraImages;
  30. }
  31. return c;
  32. });
  33. if (portalId) {
  34. cards = cards.filter(c => c.portalId === portalId);
  35. }
  36. return cards.sort((a, b) => a.displayOrder - b.displayOrder);
  37. }
  38. export async function saveCards(cards: Card[]): Promise<void> {
  39. await ensureDb();
  40. await fs.writeFile(CARDS_FILE, JSON.stringify(cards, null, 2));
  41. }
  42. // Seed function for "Casa della scuola"
  43. export async function seedDatabase() {
  44. const portalId = 'uuid-casa-della-scuola';
  45. const portals: Portal[] = [{
  46. id: portalId,
  47. tenantId: 'casa-della-scuola',
  48. title: 'Benvenuti alla Casa della Scuola',
  49. welcomeText: 'Discover the rich history and beautiful landscapes of our heritage.',
  50. heroImageUrl: '/hero-bg.jpg',
  51. logoUrl: '/logo.png',
  52. themeColor: '#1e3a8a'
  53. }];
  54. const cards: Card[] = [
  55. {
  56. id: 'uuid-card-1', portalId, title: 'History before the war',
  57. imageUrl: '/history.jpg', shortDescription: 'Explore the origins.',
  58. fullContent: '<p>Long text about the history...</p>', cardType: 'INFO_PAGE', displayOrder: 1
  59. },
  60. {
  61. id: 'uuid-card-2', portalId, title: 'Pettinicchio Biography',
  62. imageUrl: '/pettinicchio.jpg', shortDescription: 'Life and legacy.',
  63. fullContent: '<p>Biography details...</p>', cardType: 'INFO_PAGE', displayOrder: 2
  64. },
  65. {
  66. id: 'uuid-card-3', portalId, title: 'Campobasso Landscapes',
  67. imageUrl: '/campobasso.jpg', shortDescription: 'Scenic views of the region.',
  68. fullContent: '', cardType: 'IMAGE_GALLERY', displayOrder: 3
  69. }
  70. ];
  71. await fs.writeFile(PORTALS_FILE, JSON.stringify(portals, null, 2));
  72. await fs.writeFile(CARDS_FILE, JSON.stringify(cards, null, 2));
  73. }
  74. export async function savePortals(portals: Portal[]): Promise<void> {
  75. const fs = require('fs/promises');
  76. const path = require('path');
  77. const PORTALS_FILE = path.join(process.cwd(), 'data', 'portals.txt');
  78. await fs.writeFile(PORTALS_FILE, JSON.stringify(portals, null, 2));
  79. }