Skip to content

Commit

Permalink
fetch and merge all results of an athlete
Browse files Browse the repository at this point in the history
  • Loading branch information
nimarion committed Mar 15, 2024
1 parent 556cd73 commit 68798ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
24 changes: 23 additions & 1 deletion src/athletes/athletes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,31 @@ export class AthletesController {
@Param('id', ParseIntPipe) id: number,
@Query('year') year?: number,
): Promise<Performance[]> {
if (!year) {
const athlete = await this.athletesService.getAthlete(id);
if (!athlete) {
throw new NotFoundException();
}
const allResults = [];
const promises = athlete.activeSeasons.map(async (season) => {
try {
const results = await this.resultsService.getResultsFromAthlete(
id,
season,
);
if (results) {
allResults.push(results);
}
} catch (error) {
console.error(`Error fetching results for season ${season}:`, error);
}
});
await Promise.all(promises);
return allResults;
}
const results = await this.resultsService.getResultsFromAthlete(id, year);
if (!results) {
throw new NotFoundException();
return [];
}
return results;
}
Expand Down
2 changes: 1 addition & 1 deletion src/athletes/results/result.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class ResultsService {
});
},
);
return results;
return results.filter((result) => result.place > 0);
} catch (error) {
console.log(error);
}
Expand Down
8 changes: 6 additions & 2 deletions src/athletes/results/result.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ export const ResultsByEvent = z.object({
return 0;
}
try {
return Number(val);
const number = Number(val);
if (isNaN(number)) {
return -1;
}
return number;
} catch (error) {
console.log(val, error);
return -1;
}
}, z.number()),
category: z.string(),
category: z.string().nullable().default('F'),
}),
),
}),
Expand Down

0 comments on commit 68798ce

Please sign in to comment.