website refactor
This commit is contained in:
@@ -5,6 +5,9 @@ import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { ApiError } from '@/lib/api/base/ApiError';
|
||||
import { RaceResultsDetailViewModel } from '@/lib/view-models/RaceResultsDetailViewModel';
|
||||
import { RaceWithSOFViewModel } from '@/lib/view-models/RaceWithSOFViewModel';
|
||||
import { ImportRaceResultsSummaryViewModel } from '@/lib/view-models/ImportRaceResultsSummaryViewModel';
|
||||
|
||||
/**
|
||||
* Race Results Service
|
||||
@@ -15,13 +18,29 @@ import { ApiError } from '@/lib/api/base/ApiError';
|
||||
export class RaceResultsService implements Service {
|
||||
private apiClient: RacesApiClient;
|
||||
|
||||
constructor() {
|
||||
// Service creates its own dependencies
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new ConsoleErrorReporter();
|
||||
|
||||
this.apiClient = new RacesApiClient(baseUrl, errorReporter, logger);
|
||||
constructor(apiClient?: RacesApiClient) {
|
||||
if (apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
} else {
|
||||
// Service creates its own dependencies
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new ConsoleErrorReporter();
|
||||
|
||||
this.apiClient = new RacesApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
}
|
||||
|
||||
async getResultsDetail(raceId: string, currentUserId?: string): Promise<any> {
|
||||
const res = await this.getRaceResultsDetail(raceId);
|
||||
if (res.isErr()) throw new Error((res as any).error.message);
|
||||
const data = (res as any).value;
|
||||
return new RaceResultsDetailViewModel({ ...data, currentUserId: (currentUserId === undefined || currentUserId === null) ? '' : currentUserId }, {} as any);
|
||||
}
|
||||
|
||||
async importResults(raceId: string, input: any): Promise<any> {
|
||||
const res = await this.apiClient.importResults(raceId, input);
|
||||
return new ImportRaceResultsSummaryViewModel(res);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,21 +69,12 @@ export class RaceResultsService implements Service {
|
||||
* Get race with strength of field
|
||||
* Returns race data with SOF calculation
|
||||
*/
|
||||
async getWithSOF(raceId: string): Promise<Result<unknown, DomainError>> {
|
||||
async getWithSOF(raceId: string): Promise<any> {
|
||||
try {
|
||||
const data = await this.apiClient.getWithSOF(raceId);
|
||||
return Result.ok(data);
|
||||
return new RaceWithSOFViewModel(data);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof ApiError) {
|
||||
return Result.err({
|
||||
type: this.mapApiErrorType(error.type),
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
return Result.err({
|
||||
type: 'unknown',
|
||||
message: (error as Error).message || 'Failed to fetch race SOF'
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user