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.
 
 
 

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