const API_URL = 'http://localhost:8080/api'; export async function login(email: string, password: string) { const res = await fetch(`${API_URL}/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), }); if (!res.ok) throw new Error('Login fehlgeschlagen'); return res.json(); // { token: string } } export async function fetchTournaments() { const res = await fetch(`${API_URL}/tournaments`); if (!res.ok) throw new Error('Fehler beim Laden der Turniere'); return res.json(); } export async function fetchTournament(id: string, token?: string) { const res = await fetch(`${API_URL}/tournaments/${id}`, { headers: token ? { Authorization: `Bearer ${token}` } : undefined, }); if (!res.ok) throw new Error('Fehler beim Laden des Turniers'); return res.json(); } export async function updateTournament(id: string, data: any, token: string) { const res = await fetch(`${API_URL}/tournaments/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify(data), }); if (!res.ok) throw new Error('Update fehlgeschlagen'); return res.json(); } export async function createTournament(data: any, token: string) { const res = await fetch(`${API_URL}/tournaments`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify(data), }); if (!res.ok) throw new Error('Turnier-Erstellung fehlgeschlagen'); return res.json(); } export async function deleteTournament(id: string, token: string) { const res = await fetch(`${API_URL}/tournaments/${id}`, { method: 'DELETE', headers: { Authorization: `Bearer ${token}` }, }); if (!res.ok) throw new Error('Turnier-Löschung fehlgeschlagen'); return res.json(); } export async function fetchPlayer(token: string, id: string) { const res = await fetch(`${API_URL}/players/${id}`, { headers: { Authorization: `Bearer ${token}` }, }); if (!res.ok) throw new Error('Fehler beim Laden des Spielers'); return res.json(); } export async function fetchPlayers(token: string) { const res = await fetch(`${API_URL}/players`, { headers: { Authorization: `Bearer ${token}` }, }); if (!res.ok) throw new Error('Fehler beim Laden der Spieler'); return res.json(); } export async function createPlayer(player: { Username: string, Email: string }, token: string) { const res = await fetch(`${API_URL}/players`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify(player), }); if (!res.ok) throw new Error('Spieler-Erstellung fehlgeschlagen'); return res.json(); } export async function updatePlayer(id: string, player: { name?: string, email?: string }, token: string) { const res = await fetch(`${API_URL}/players/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify(player), }); if (!res.ok) throw new Error('Spieler-Aktualisierung fehlgeschlagen'); return res.json(); } export async function deletePlayer(id: string, token: string) { const res = await fetch(`${API_URL}/players/${id}`, { method: 'DELETE', headers: { Authorization: `Bearer ${token}` }, }); if (!res.ok) throw new Error('Spieler-Löschung fehlgeschlagen'); return res.json(); } export async function registerTeam(id: string, team: { name: string }, token: string) { const res = await fetch(`${API_URL}/tournaments/${id}/register`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify(team), }); if (!res.ok) throw new Error('Team-Anmeldung fehlgeschlagen'); return res.json(); }