website refactor
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import { RacesApiClient } from '@/lib/api/races/RacesApiClient';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
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';
|
||||
|
||||
/**
|
||||
* Race Service - DTO Only
|
||||
@@ -6,20 +12,83 @@ import { RacesApiClient } from '@/lib/api/races/RacesApiClient';
|
||||
* Returns raw API DTOs. No ViewModels or UX logic.
|
||||
* All client-side presentation logic must be handled by hooks/components.
|
||||
*/
|
||||
export class RaceService {
|
||||
constructor(private readonly apiClient: RacesApiClient) {}
|
||||
export class RaceService implements Service {
|
||||
private apiClient: RacesApiClient;
|
||||
|
||||
async getRaceById(raceId: string): Promise<any> {
|
||||
// This would need a driverId, but for now we'll use a placeholder
|
||||
return this.apiClient.getDetail(raceId, 'placeholder-driver-id');
|
||||
constructor() {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new ConsoleErrorReporter();
|
||||
this.apiClient = new RacesApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
|
||||
async getRacesByLeagueId(leagueId: string): Promise<any> {
|
||||
return this.apiClient.getPageData(leagueId);
|
||||
async getRaceById(raceId: string): Promise<Result<unknown, DomainError>> {
|
||||
try {
|
||||
// This would need a driverId, but for now we'll use a placeholder
|
||||
const data = await this.apiClient.getDetail(raceId, 'placeholder-driver-id');
|
||||
return Result.ok(data);
|
||||
} catch (error: unknown) {
|
||||
return Result.err(this.mapError(error, 'Failed to fetch race by ID'));
|
||||
}
|
||||
}
|
||||
|
||||
async findByLeagueId(leagueId: string): Promise<any[]> {
|
||||
const result = await this.apiClient.getPageData(leagueId);
|
||||
return result.races || [];
|
||||
async getRacesByLeagueId(leagueId: string): Promise<Result<unknown, DomainError>> {
|
||||
try {
|
||||
const data = await this.apiClient.getPageData(leagueId);
|
||||
return Result.ok(data);
|
||||
} catch (error: unknown) {
|
||||
return Result.err(this.mapError(error, 'Failed to fetch races by league ID'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async findByLeagueId(leagueId: string): Promise<Result<unknown[], DomainError>> {
|
||||
try {
|
||||
const result = await this.apiClient.getPageData(leagueId);
|
||||
return Result.ok(result.races || []);
|
||||
} catch (error: unknown) {
|
||||
return Result.err(this.mapError(error, 'Failed to find races by league ID'));
|
||||
}
|
||||
}
|
||||
|
||||
async registerForRace(_: string, __: string, ___: string): Promise<Result<void, DomainError>> {
|
||||
return Result.err({ type: 'notImplemented', message: 'registerForRace' });
|
||||
}
|
||||
|
||||
async withdrawFromRace(_: string, __: string): Promise<Result<void, DomainError>> {
|
||||
return Result.err({ type: 'notImplemented', message: 'withdrawFromRace' });
|
||||
}
|
||||
|
||||
async fileProtest(__: unknown): Promise<Result<void, DomainError>> {
|
||||
return Result.err({ type: 'notImplemented', message: 'fileProtest' });
|
||||
}
|
||||
|
||||
private mapError(error: unknown, defaultMessage: string): DomainError {
|
||||
if (error instanceof ApiError) {
|
||||
return {
|
||||
type: this.mapApiErrorType(error.type),
|
||||
message: error.message
|
||||
};
|
||||
}
|
||||
return {
|
||||
type: 'unknown',
|
||||
message: defaultMessage
|
||||
};
|
||||
}
|
||||
|
||||
private mapApiErrorType(apiErrorType: string): DomainError['type'] {
|
||||
switch (apiErrorType) {
|
||||
case 'NOT_FOUND':
|
||||
return 'notFound';
|
||||
case 'AUTH_ERROR':
|
||||
return 'unauthorized';
|
||||
case 'VALIDATION_ERROR':
|
||||
return 'validation';
|
||||
case 'SERVER_ERROR':
|
||||
return 'serverError';
|
||||
case 'NETWORK_ERROR':
|
||||
return 'networkError';
|
||||
default:
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user