website refactor
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import { PenaltiesApiClient } from '@/lib/api/penalties/PenaltiesApiClient';
|
||||
import type { PenaltyTypesReferenceDTO } from '@/lib/types/PenaltyTypesReferenceDTO';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
|
||||
/**
|
||||
* Penalty Service
|
||||
@@ -7,30 +12,50 @@ import type { PenaltyTypesReferenceDTO } from '@/lib/types/PenaltyTypesReference
|
||||
* Orchestrates penalty operations by coordinating API calls and view model creation.
|
||||
* All dependencies are injected via constructor.
|
||||
*/
|
||||
export class PenaltyService {
|
||||
constructor(
|
||||
private readonly apiClient: PenaltiesApiClient
|
||||
) {}
|
||||
export class PenaltyService implements Service {
|
||||
private readonly apiClient: PenaltiesApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger);
|
||||
this.apiClient = new PenaltiesApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find penalties by race ID
|
||||
*/
|
||||
async findByRaceId(raceId: string): Promise<any[]> {
|
||||
const dto = await this.apiClient.getRacePenalties(raceId);
|
||||
return dto.penalties;
|
||||
async findByRaceId(raceId: string): Promise<Result<unknown[], DomainError>> {
|
||||
try {
|
||||
const dto = await this.apiClient.getRacePenalties(raceId);
|
||||
return Result.ok(dto.penalties);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to find penalties' });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get allowed penalty types and semantics
|
||||
*/
|
||||
async getPenaltyTypesReference(): Promise<PenaltyTypesReferenceDTO> {
|
||||
return this.apiClient.getPenaltyTypesReference();
|
||||
async getPenaltyTypesReference(): Promise<Result<PenaltyTypesReferenceDTO, DomainError>> {
|
||||
try {
|
||||
const data = await this.apiClient.getPenaltyTypesReference();
|
||||
return Result.ok(data);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get penalty types' });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a penalty
|
||||
*/
|
||||
async applyPenalty(input: any): Promise<void> {
|
||||
await this.apiClient.applyPenalty(input);
|
||||
async applyPenalty(input: unknown): Promise<Result<void, DomainError>> {
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
await this.apiClient.applyPenalty(input as any);
|
||||
return Result.ok(undefined);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to apply penalty' });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user