You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

80 rivejä
2.9 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. if (portalId) {
  23. cards = cards.filter(c => c.portalId === portalId);
  24. }
  25. // ALWAYS sort, regardless of whether portalId was passed
  26. return cards.sort((a, b) => a.displayOrder - b.displayOrder);
  27. }
  28. export async function saveCards(cards: Card[]): Promise<void> {
  29. await ensureDb();
  30. await fs.writeFile(CARDS_FILE, JSON.stringify(cards, null, 2));
  31. }
  32. // Seed function for "Casa della scuola"
  33. export async function seedDatabase() {
  34. const portalId = 'uuid-casa-della-scuola';
  35. const portals: Portal[] = [{
  36. id: portalId,
  37. tenantId: 'casa-della-scuola',
  38. title: 'Benvenuti alla Casa della Scuola',
  39. welcomeText: 'Discover the rich history and beautiful landscapes of our heritage.',
  40. heroImageUrl: '/hero-bg.jpg',
  41. logoUrl: '/logo.png',
  42. themeColor: '#1e3a8a'
  43. }];
  44. const cards: Card[] = [
  45. {
  46. id: 'uuid-card-1', portalId, title: 'History before the war',
  47. imageUrl: '/history.jpg', shortDescription: 'Explore the origins.',
  48. fullContent: '<p>Long text about the history...</p>', cardType: 'INFO_PAGE', displayOrder: 1
  49. },
  50. {
  51. id: 'uuid-card-2', portalId, title: 'Pettinicchio Biography',
  52. imageUrl: '/pettinicchio.jpg', shortDescription: 'Life and legacy.',
  53. fullContent: '<p>Biography details...</p>', cardType: 'INFO_PAGE', displayOrder: 2
  54. },
  55. {
  56. id: 'uuid-card-3', portalId, title: 'Campobasso Landscapes',
  57. imageUrl: '/campobasso.jpg', shortDescription: 'Scenic views of the region.',
  58. fullContent: '', cardType: 'IMAGE_GALLERY', displayOrder: 3
  59. }
  60. ];
  61. await fs.writeFile(PORTALS_FILE, JSON.stringify(portals, null, 2));
  62. await fs.writeFile(CARDS_FILE, JSON.stringify(cards, null, 2));
  63. }
  64. export async function savePortals(portals: Portal[]): Promise<void> {
  65. const fs = require('fs/promises');
  66. const path = require('path');
  67. const PORTALS_FILE = path.join(process.cwd(), 'data', 'portals.txt');
  68. await fs.writeFile(PORTALS_FILE, JSON.stringify(portals, null, 2));
  69. }