Files
harheimertc/server/api/mannschaften.get.js
Torsten Schulz (local) 0fb6634b7f Update CSV data fetching to use API endpoint in Mannschaften components
This commit modifies the loadMannschaften function across multiple components to fetch CSV data from the new API endpoint '/api/mannschaften' instead of the previous static file path '/data/mannschaften.csv'. This change enhances data retrieval consistency and aligns with the updated data management strategy in the application.
2026-01-19 08:28:43 +01:00

59 lines
1.4 KiB
JavaScript

import { promises as fs } from 'fs'
import path from 'path'
async function exists(p) {
try {
await fs.access(p)
return true
} catch {
return false
}
}
export default defineEventHandler(async (event) => {
try {
const cwd = process.cwd()
const filename = 'mannschaften.csv'
const candidates = [
path.join(cwd, '.output/public/data', filename),
path.join(cwd, 'public/data', filename),
path.join(cwd, '../.output/public/data', filename),
path.join(cwd, '../public/data', filename)
]
let csvPath = null
for (const p of candidates) {
// eslint-disable-next-line no-await-in-loop
if (await exists(p)) {
csvPath = p
break
}
}
if (!csvPath) {
throw createError({
statusCode: 404,
statusMessage: 'Mannschaften-Datei nicht gefunden'
})
}
const csv = await fs.readFile(csvPath, 'utf-8')
setHeader(event, 'Content-Type', 'text/csv; charset=utf-8')
setHeader(event, 'Cache-Control', 'no-cache, no-store, must-revalidate')
setHeader(event, 'Pragma', 'no-cache')
setHeader(event, 'Expires', '0')
return csv
} catch (error) {
if (error?.statusCode) throw error
console.error('Fehler beim Laden der Mannschaften:', error)
throw createError({
statusCode: 500,
statusMessage: 'Fehler beim Laden der Mannschaften'
})
}
})