Update CSV data fetching to use API endpoint in Mannschaften components
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 5s
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 5s
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.
This commit is contained in:
@@ -125,7 +125,7 @@ async function fetchCsvText(url) {
|
|||||||
|
|
||||||
const loadMannschaften = async () => {
|
const loadMannschaften = async () => {
|
||||||
try {
|
try {
|
||||||
const csv = await fetchCsvText('/data/mannschaften.csv')
|
const csv = await fetchCsvText('/api/mannschaften')
|
||||||
|
|
||||||
// Vereinfachter CSV-Parser
|
// Vereinfachter CSV-Parser
|
||||||
const lines = csv.split('\n').filter(line => line.trim() !== '')
|
const lines = csv.split('\n').filter(line => line.trim() !== '')
|
||||||
|
|||||||
@@ -924,7 +924,7 @@ const toggleMobileSubmenu = (menu) => {
|
|||||||
const loadMannschaften = async () => {
|
const loadMannschaften = async () => {
|
||||||
try {
|
try {
|
||||||
const attempt = async () => {
|
const attempt = async () => {
|
||||||
const url = `/data/mannschaften.csv?_t=${Date.now()}`
|
const url = `/api/mannschaften?_t=${Date.now()}`
|
||||||
const response = await fetch(url, { cache: 'no-store' })
|
const response = await fetch(url, { cache: 'no-store' })
|
||||||
if (!response.ok) return null
|
if (!response.ok) return null
|
||||||
return await response.text()
|
return await response.text()
|
||||||
|
|||||||
@@ -508,7 +508,7 @@ function getPendingSpielerNamesForTeamIndex(teamIndex) {
|
|||||||
const loadMannschaften = async () => {
|
const loadMannschaften = async () => {
|
||||||
isLoading.value = true
|
isLoading.value = true
|
||||||
try {
|
try {
|
||||||
const csv = await fetchCsvText('/data/mannschaften.csv')
|
const csv = await fetchCsvText('/api/mannschaften')
|
||||||
const lines = csv.split('\n').filter(line => line.trim() !== '')
|
const lines = csv.split('\n').filter(line => line.trim() !== '')
|
||||||
|
|
||||||
if (lines.length < 2) {
|
if (lines.length < 2) {
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ async function fetchCsvText(url) {
|
|||||||
|
|
||||||
const loadMannschaften = async () => {
|
const loadMannschaften = async () => {
|
||||||
try {
|
try {
|
||||||
const csv = await fetchCsvText('/data/mannschaften.csv')
|
const csv = await fetchCsvText('/api/mannschaften')
|
||||||
if (!csv) return
|
if (!csv) return
|
||||||
const lines = csv.split('\n').filter(line => line.trim() !== '')
|
const lines = csv.split('\n').filter(line => line.trim() !== '')
|
||||||
|
|
||||||
|
|||||||
@@ -324,7 +324,7 @@ const loadData = async () => {
|
|||||||
// Lade Spielplandaten und Mannschaften parallel
|
// Lade Spielplandaten und Mannschaften parallel
|
||||||
const [spielplanResponse, mannschaftenResponse] = await Promise.all([
|
const [spielplanResponse, mannschaftenResponse] = await Promise.all([
|
||||||
fetch('/api/spielplan'),
|
fetch('/api/spielplan'),
|
||||||
fetchCsvText('/data/mannschaften.csv')
|
fetchCsvText('/api/mannschaften')
|
||||||
])
|
])
|
||||||
|
|
||||||
// Spielplandaten verarbeiten
|
// Spielplandaten verarbeiten
|
||||||
|
|||||||
58
server/api/mannschaften.get.js
Normal file
58
server/api/mannschaften.get.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
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'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
Reference in New Issue
Block a user