refactor
This commit is contained in:
@@ -1,9 +1,56 @@
|
||||
import type {
|
||||
IAllTeamsPresenter,
|
||||
TeamListItemViewModel,
|
||||
AllTeamsViewModel,
|
||||
AllTeamsResultDTO,
|
||||
} from '@gridpilot/racing/application/presenters/IAllTeamsPresenter';
|
||||
/**
|
||||
* AllTeamsPresenter - Pure data transformer
|
||||
* Transforms API response to view model without DI dependencies.
|
||||
*/
|
||||
|
||||
import { apiClient, type AllTeamsViewModel as ApiAllTeamsViewModel } from '@/lib/apiClient';
|
||||
|
||||
export interface TeamListItemViewModel {
|
||||
id: string;
|
||||
name: string;
|
||||
tag?: string | undefined;
|
||||
description?: string | undefined;
|
||||
memberCount: number;
|
||||
logoUrl?: string | undefined;
|
||||
rating?: number | undefined;
|
||||
}
|
||||
|
||||
export interface AllTeamsViewModel {
|
||||
teams: TeamListItemViewModel[];
|
||||
totalCount: number;
|
||||
}
|
||||
|
||||
export interface IAllTeamsPresenter {
|
||||
reset(): void;
|
||||
getViewModel(): AllTeamsViewModel | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform API response to view model
|
||||
*/
|
||||
function transformApiResponse(apiResponse: ApiAllTeamsViewModel): AllTeamsViewModel {
|
||||
const teamItems: TeamListItemViewModel[] = apiResponse.teams.map((team) => {
|
||||
const viewModel: TeamListItemViewModel = {
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
memberCount: team.memberCount ?? 0,
|
||||
};
|
||||
|
||||
if (team.logoUrl) {
|
||||
viewModel.logoUrl = team.logoUrl;
|
||||
}
|
||||
if (team.rating) {
|
||||
viewModel.rating = team.rating;
|
||||
}
|
||||
|
||||
return viewModel;
|
||||
});
|
||||
|
||||
return {
|
||||
teams: teamItems,
|
||||
totalCount: teamItems.length,
|
||||
};
|
||||
}
|
||||
|
||||
export class AllTeamsPresenter implements IAllTeamsPresenter {
|
||||
private viewModel: AllTeamsViewModel | null = null;
|
||||
@@ -12,23 +59,20 @@ export class AllTeamsPresenter implements IAllTeamsPresenter {
|
||||
this.viewModel = null;
|
||||
}
|
||||
|
||||
present(input: AllTeamsResultDTO): void {
|
||||
const teamItems: TeamListItemViewModel[] = input.teams.map((team) => ({
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
tag: team.tag,
|
||||
description: team.description,
|
||||
memberCount: team.memberCount ?? 0,
|
||||
leagues: team.leagues,
|
||||
}));
|
||||
|
||||
this.viewModel = {
|
||||
teams: teamItems,
|
||||
totalCount: teamItems.length,
|
||||
};
|
||||
async fetchAndPresent(): Promise<void> {
|
||||
const apiResponse = await apiClient.teams.getAll();
|
||||
this.viewModel = transformApiResponse(apiResponse);
|
||||
}
|
||||
|
||||
getViewModel(): AllTeamsViewModel | null {
|
||||
return this.viewModel;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function to fetch and transform all teams
|
||||
*/
|
||||
export async function fetchAllTeams(): Promise<AllTeamsViewModel> {
|
||||
const apiResponse = await apiClient.teams.getAll();
|
||||
return transformApiResponse(apiResponse);
|
||||
}
|
||||
Reference in New Issue
Block a user