:api added seasonApi to fetch seasons for a show

This commit is contained in:
Cron1cle
2025-10-07 16:14:11 +02:00
parent 9da89e6b90
commit de2778d6db
11 changed files with 462 additions and 156 deletions

77
apis/seasonApi.ts Normal file
View File

@@ -0,0 +1,77 @@
export type RawSeasonParticipant = {
id: { seasonId: number; personId: number };
person: {
personId: number;
name: string;
birthDate: string;
imageUrl: string | null;
};
partner: unknown | null;
};
export type RawSeason = {
seasonId: number;
show: number;
seasonNumber: number;
startDate?: string;
endDate?: string | null;
moderators: unknown[];
seasonParticipants: RawSeasonParticipant[];
};
export type SeasonParticipant = {
id: number;
name: string;
birthYear?: number;
imageUri: string;
};
export type Season = {
id: number;
showId: number;
seasonNumber: number;
startDate?: string;
endDate?: string | null;
participants: SeasonParticipant[];
};
const SEASON_BASE_URL = "http://45.157.177.99:8080/shows";
export async function getSeason(
showId: number,
seasonNumber: number
): Promise<Season | null> {
// WICHTIG: trailing Slash entfernt
const url = `${SEASON_BASE_URL}/${showId}/seasons/${seasonNumber}`;
try {
console.log("[getSeason] Fetch:", url);
const res = await fetch(url);
console.log("[getSeason] Status:", res.status);
if (res.status === 404) return null;
if (!res.ok) throw new Error(`Season fetch failed: ${res.status}`);
const raw: RawSeason = await res.json();
const participants: SeasonParticipant[] = raw.seasonParticipants.map(
(p) => ({
id: p.person.personId,
name: p.person.name,
birthYear: p.person.birthDate
? Number(p.person.birthDate.slice(0, 4))
: undefined,
imageUri:
p.person.imageUrl ??
"https://via.placeholder.com/300x400.png?text=No+Image",
})
);
return {
id: raw.seasonId,
showId: raw.show,
seasonNumber: raw.seasonNumber,
startDate: raw.startDate,
endDate: raw.endDate,
participants,
};
} catch (e) {
console.error("getSeason error:", e);
throw e;
}
}

View File

@@ -26,11 +26,11 @@ export type Show = {
running: boolean;
};
const API_URL = "http://45.157.177.99:8080/shows";
const SHOW_API_URL = "http://45.157.177.99:8080/shows";
export async function getShows(): Promise<Show[]> {
try {
const response = await fetch(API_URL);
const response = await fetch(SHOW_API_URL);
if (!response.ok) {
throw new Error("Network response was not ok");
}