api client refactor
This commit is contained in:
@@ -1,23 +1,46 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentRaceResultsDetail } from '../../presenters';
|
||||
import { RaceResultsDetailPresenter, RaceWithSOFPresenter, ImportRaceResultsPresenter } from '../../presenters';
|
||||
import { RaceResultsDetailViewModel } from '../../view-models';
|
||||
|
||||
export class RaceResultsService {
|
||||
constructor(
|
||||
private readonly apiClient = api.races,
|
||||
private readonly resultsDetailPresenter = new RaceResultsDetailPresenter(),
|
||||
private readonly sofPresenter = new RaceWithSOFPresenter(),
|
||||
private readonly importPresenter = new ImportRaceResultsPresenter()
|
||||
) {}
|
||||
|
||||
async importRaceResults(raceId: string, input: any): Promise<any> {
|
||||
const dto = await this.apiClient.importResults(raceId, input);
|
||||
return this.importPresenter.present(dto);
|
||||
}
|
||||
|
||||
async getResultsDetail(raceId: string, currentUserId?: string): Promise<RaceResultsDetailViewModel> {
|
||||
const dto = await this.apiClient.getResultsDetail(raceId);
|
||||
return this.resultsDetailPresenter.present(dto, currentUserId);
|
||||
}
|
||||
|
||||
async getWithSOF(raceId: string): Promise<any> {
|
||||
const dto = await this.apiClient.getWithSOF(raceId);
|
||||
return this.sofPresenter.present(dto);
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton instance
|
||||
export const raceResultsService = new RaceResultsService();
|
||||
|
||||
// Backward compatibility functions
|
||||
export async function getRaceResults(
|
||||
raceId: string,
|
||||
currentUserId?: string
|
||||
): Promise<RaceResultsDetailViewModel> {
|
||||
const dto = await api.races.getResultsDetail(raceId);
|
||||
return presentRaceResultsDetail(dto, currentUserId);
|
||||
return raceResultsService.getResultsDetail(raceId, currentUserId);
|
||||
}
|
||||
|
||||
export async function getRaceSOF(raceId: string): Promise<any> {
|
||||
const dto = await api.races.getWithSOF(raceId);
|
||||
// TODO: use presenter
|
||||
return dto;
|
||||
return raceResultsService.getWithSOF(raceId);
|
||||
}
|
||||
|
||||
export async function importRaceResults(raceId: string, input: any): Promise<any> {
|
||||
const dto = await api.races.importResults(raceId, input);
|
||||
// TODO: use presenter
|
||||
return dto;
|
||||
return raceResultsService.importRaceResults(raceId, input);
|
||||
}
|
||||
Reference in New Issue
Block a user