40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { Result } from '@/lib/contracts/Result';
|
|
import { PageQuery } from '@/lib/contracts/page-queries/PageQuery';
|
|
import { PresentationError, mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
|
import { TeamService } from '@/lib/services/teams/TeamService';
|
|
import { TeamSummaryViewModel } from '@/lib/view-models/TeamSummaryViewModel';
|
|
|
|
export interface TeamLeaderboardPageData {
|
|
teams: TeamSummaryViewModel[];
|
|
}
|
|
|
|
export class TeamLeaderboardPageQuery implements PageQuery<TeamLeaderboardPageData, void> {
|
|
private readonly service: TeamService;
|
|
|
|
constructor(service?: TeamService) {
|
|
this.service = service || new TeamService();
|
|
}
|
|
|
|
async execute(): Promise<Result<TeamLeaderboardPageData, PresentationError>> {
|
|
try {
|
|
const service = this.service;
|
|
const result = await service.getAllTeams();
|
|
|
|
if (result.isErr()) {
|
|
return Result.err(mapToPresentationError(result.getError()));
|
|
}
|
|
|
|
const teams = result.unwrap().map((t: any) => {
|
|
const vm = new TeamSummaryViewModel(t as any);
|
|
// Ensure it's a plain object for comparison in tests if needed,
|
|
// but here we just need it to match the expected viewData structure.
|
|
return vm;
|
|
});
|
|
|
|
return Result.ok({ teams });
|
|
} catch (error) {
|
|
return Result.err('unknown');
|
|
}
|
|
}
|
|
}
|