refactor driver module (wip)

This commit is contained in:
2025-12-22 10:24:40 +01:00
parent e7dbec4a85
commit 9da528d5bd
108 changed files with 842 additions and 947 deletions

View File

@@ -1,9 +1,5 @@
import type { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type {
GetRaceDetailResult,
GetRaceDetailErrorCode,
} from '@core/racing/application/use-cases/GetRaceDetailUseCase';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { GetRaceDetailResult } from '@core/racing/application/use-cases/GetRaceDetailUseCase';
import type { DriverRatingProvider } from '@core/racing/application/ports/DriverRatingProvider';
import type { IImageServicePort } from '@core/racing/application/ports/IImageServicePort';
import type { GetRaceDetailParamsDTO } from '../dtos/GetRaceDetailParamsDTO';
@@ -16,47 +12,26 @@ import type { RaceDetailUserResultDTO } from '../dtos/RaceDetailUserResultDTO';
export type GetRaceDetailResponseModel = RaceDetailDTO;
export type GetRaceDetailApplicationError = ApplicationErrorCode<
GetRaceDetailErrorCode,
{ message: string }
>;
export class RaceDetailPresenter {
private model: GetRaceDetailResponseModel | null = null;
export class RaceDetailPresenter implements UseCaseOutputPort<GetRaceDetailResult> {
private result: GetRaceDetailResult | null = null;
constructor(
private readonly driverRatingProvider: DriverRatingProvider,
private readonly imageService: IImageServicePort,
private readonly params: GetRaceDetailParamsDTO,
) {}
reset(): void {
this.model = null;
present(result: GetRaceDetailResult): void {
this.result = result;
}
async present(
result: Result<GetRaceDetailResult, GetRaceDetailApplicationError>,
params: GetRaceDetailParamsDTO,
): Promise<void> {
if (result.isErr()) {
const error = result.unwrapErr();
if (error.code === 'RACE_NOT_FOUND') {
this.model = {
race: null,
league: null,
entryList: [],
registration: {
isUserRegistered: false,
canRegister: false,
},
userResult: null,
} as RaceDetailDTO;
return;
}
throw new Error(error.details?.message ?? 'Failed to get race detail');
async getResponseModel(): Promise<GetRaceDetailResponseModel | null> {
if (!this.result) {
return null;
}
const output = result.unwrap();
const output = this.result;
const params = this.params;
const raceDTO: RaceDetailRaceDTO | null = output.race
? {
@@ -118,7 +93,7 @@ export class RaceDetailPresenter {
}
: null;
this.model = {
return {
race: raceDTO,
league: leagueDTO,
entryList: entryListDTO,
@@ -127,16 +102,11 @@ export class RaceDetailPresenter {
} as RaceDetailDTO;
}
getResponseModel(): GetRaceDetailResponseModel | null {
return this.model;
}
get responseModel(): GetRaceDetailResponseModel {
if (!this.model) {
throw new Error('Presenter not presented');
}
return this.model;
get viewModel(): Promise<GetRaceDetailResponseModel> {
return this.getResponseModel().then(model => {
if (!model) throw new Error('Presenter not presented');
return model;
});
}
private calculateRatingChange(position: number): number {