refactor
This commit is contained in:
@@ -23,7 +23,7 @@ export class TeamController {
|
||||
@ApiResponse({ status: 200, description: 'List of all teams', type: GetAllTeamsOutputDTO })
|
||||
async getAll(): Promise<GetAllTeamsOutputDTO> {
|
||||
const presenter = await this.teamService.getAll();
|
||||
return presenter.viewModel;
|
||||
return presenter.responseModel;
|
||||
}
|
||||
|
||||
@Get(':teamId')
|
||||
@@ -33,7 +33,7 @@ export class TeamController {
|
||||
async getDetails(@Param('teamId') teamId: string, @Req() req: Request): Promise<GetTeamDetailsOutputDTO | null> {
|
||||
const userId = req['user']?.userId;
|
||||
const presenter = await this.teamService.getDetails(teamId, userId);
|
||||
return presenter.getViewModel();
|
||||
return presenter.getResponseModel();
|
||||
}
|
||||
|
||||
@Get(':teamId/members')
|
||||
@@ -41,7 +41,7 @@ export class TeamController {
|
||||
@ApiResponse({ status: 200, description: 'Team members', type: GetTeamMembersOutputDTO })
|
||||
async getMembers(@Param('teamId') teamId: string): Promise<GetTeamMembersOutputDTO> {
|
||||
const presenter = await this.teamService.getMembers(teamId);
|
||||
return presenter.getViewModel()!;
|
||||
return presenter.getResponseModel()!;
|
||||
}
|
||||
|
||||
@Get(':teamId/join-requests')
|
||||
@@ -49,7 +49,7 @@ export class TeamController {
|
||||
@ApiResponse({ status: 200, description: 'Team join requests', type: GetTeamJoinRequestsOutputDTO })
|
||||
async getJoinRequests(@Param('teamId') teamId: string): Promise<GetTeamJoinRequestsOutputDTO> {
|
||||
const presenter = await this.teamService.getJoinRequests(teamId);
|
||||
return presenter.getViewModel()!;
|
||||
return presenter.getResponseModel()!;
|
||||
}
|
||||
|
||||
@Post()
|
||||
@@ -58,7 +58,7 @@ export class TeamController {
|
||||
async create(@Body() input: CreateTeamInputDTO, @Req() req: Request): Promise<CreateTeamOutputDTO> {
|
||||
const userId = req['user']?.userId;
|
||||
const presenter = await this.teamService.create(input, userId);
|
||||
return presenter.viewModel;
|
||||
return presenter.responseModel;
|
||||
}
|
||||
|
||||
@Patch(':teamId')
|
||||
@@ -67,7 +67,7 @@ export class TeamController {
|
||||
async update(@Param('teamId') teamId: string, @Body() input: UpdateTeamInputDTO, @Req() req: Request): Promise<UpdateTeamOutputDTO> {
|
||||
const userId = req['user']?.userId;
|
||||
const presenter = await this.teamService.update(teamId, input, userId);
|
||||
return presenter.viewModel;
|
||||
return presenter.responseModel;
|
||||
}
|
||||
|
||||
@Get('driver/:driverId')
|
||||
@@ -76,15 +76,15 @@ export class TeamController {
|
||||
@ApiResponse({ status: 404, description: 'Team not found' })
|
||||
async getDriverTeam(@Param('driverId') driverId: string): Promise<GetDriverTeamOutputDTO | null> {
|
||||
const presenter = await this.teamService.getDriverTeam(driverId);
|
||||
return presenter.getViewModel();
|
||||
return presenter.getResponseModel();
|
||||
}
|
||||
|
||||
|
||||
@Get(':teamId/members/:driverId')
|
||||
@ApiOperation({ summary: 'Get team membership for a driver' })
|
||||
@ApiResponse({ status: 200, description: 'Team membership', type: GetTeamMembershipOutputDTO })
|
||||
@ApiResponse({ status: 404, description: 'Membership not found' })
|
||||
async getMembership(@Param('teamId') teamId: string, @Param('driverId') driverId: string): Promise<GetTeamMembershipOutputDTO | null> {
|
||||
const presenter = await this.teamService.getMembership(teamId, driverId);
|
||||
return presenter.viewModel;
|
||||
return presenter.responseModel;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
import { Injectable, Inject } from '@nestjs/common';
|
||||
import { CreateTeamInputDTO } from './dtos/CreateTeamInputDTO';
|
||||
import { UpdateTeamInputDTO } from './dtos/UpdateTeamInputDTO';
|
||||
import { GetAllTeamsOutputDTO } from './dtos/GetAllTeamsOutputDTO';
|
||||
import { GetTeamDetailsOutputDTO } from './dtos/GetTeamDetailsOutputDTO';
|
||||
import { GetTeamMembersOutputDTO } from './dtos/GetTeamMembersOutputDTO';
|
||||
import { GetTeamJoinRequestsOutputDTO } from './dtos/GetTeamJoinRequestsOutputDTO';
|
||||
import { CreateTeamOutputDTO } from './dtos/CreateTeamOutputDTO';
|
||||
import { UpdateTeamOutputDTO } from './dtos/UpdateTeamOutputDTO';
|
||||
import { GetDriverTeamOutputDTO } from './dtos/GetDriverTeamOutputDTO';
|
||||
import { GetTeamMembershipOutputDTO } from './dtos/GetTeamMembershipOutputDTO';
|
||||
|
||||
// Core imports
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
@@ -42,19 +50,19 @@ export class TeamService {
|
||||
@Inject(LOGGER_TOKEN) private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async getAll(): Promise<AllTeamsPresenter> {
|
||||
async getAll(): Promise<any> { // TODO: type
|
||||
this.logger.debug('[TeamService] Fetching all teams.');
|
||||
|
||||
const presenter = new AllTeamsPresenter();
|
||||
const result = await this.getAllTeamsUseCase.execute();
|
||||
const presenter = new AllTeamsPresenter();
|
||||
if (result.isErr()) {
|
||||
this.logger.error('Error fetching all teams', result.error);
|
||||
await presenter.present({ teams: [], totalCount: 0 });
|
||||
return presenter;
|
||||
return presenter.responseModel;
|
||||
}
|
||||
|
||||
await presenter.present(result.value);
|
||||
return presenter;
|
||||
return presenter.responseModel;
|
||||
}
|
||||
|
||||
async getDetails(teamId: string, userId?: string): Promise<TeamDetailsPresenter> {
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
import { GetAllTeamsOutputPort } from '@core/racing/application/ports/output/GetAllTeamsOutputPort';
|
||||
import type { GetAllTeamsErrorCode, GetAllTeamsResult } from '@core/racing/application/use-cases/GetAllTeamsUseCase';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { GetAllTeamsOutputDTO } from '../dtos/GetAllTeamsOutputDTO';
|
||||
|
||||
export class AllTeamsPresenter {
|
||||
private result: GetAllTeamsOutputDTO | null = null;
|
||||
export type GetAllTeamsError = ApplicationErrorCode<GetAllTeamsErrorCode, { message: string }>;
|
||||
|
||||
reset() {
|
||||
this.result = null;
|
||||
export class AllTeamsPresenter {
|
||||
private model: GetAllTeamsOutputDTO | null = null;
|
||||
|
||||
reset(): void {
|
||||
this.model = null;
|
||||
}
|
||||
|
||||
async present(output: GetAllTeamsOutputPort) {
|
||||
this.result = {
|
||||
present(result: Result<GetAllTeamsResult, GetAllTeamsError>): void {
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
throw new Error(error.details?.message ?? 'Failed to get teams');
|
||||
}
|
||||
|
||||
const output = result.unwrap();
|
||||
|
||||
this.model = {
|
||||
teams: output.teams.map(team => ({
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
@@ -17,18 +28,18 @@ export class AllTeamsPresenter {
|
||||
description: team.description,
|
||||
memberCount: team.memberCount,
|
||||
leagues: team.leagues || [],
|
||||
// Note: specialization, region, languages not available in output port
|
||||
// Note: specialization, region, languages not available in output
|
||||
})),
|
||||
totalCount: output.totalCount || output.teams.length,
|
||||
totalCount: output.totalCount ?? output.teams.length,
|
||||
};
|
||||
}
|
||||
|
||||
getViewModel(): GetAllTeamsOutputDTO | null {
|
||||
return this.result;
|
||||
getResponseModel(): GetAllTeamsOutputDTO | null {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
get viewModel(): GetAllTeamsOutputDTO {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
return this.result;
|
||||
get responseModel(): GetAllTeamsOutputDTO {
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,49 @@
|
||||
import type { CreateTeamOutputPort } from '@core/racing/application/ports/output/CreateTeamOutputPort';
|
||||
import type { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { CreateTeamErrorCode, CreateTeamResult } from '@core/racing/application/use-cases/CreateTeamUseCase';
|
||||
import type { CreateTeamOutputDTO } from '../dtos/CreateTeamOutputDTO';
|
||||
|
||||
export type CreateTeamError = ApplicationErrorCode<CreateTeamErrorCode, { message: string }>;
|
||||
|
||||
export class CreateTeamPresenter {
|
||||
private result: CreateTeamOutputDTO | null = null;
|
||||
private model: CreateTeamOutputDTO | null = null;
|
||||
|
||||
reset(): void {
|
||||
this.result = null;
|
||||
this.model = null;
|
||||
}
|
||||
|
||||
presentSuccess(output: CreateTeamOutputPort): void {
|
||||
this.result = {
|
||||
present(result: Result<CreateTeamResult, CreateTeamError>): void {
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
// Validation and expected domain errors map to an unsuccessful DTO
|
||||
if (error.code === 'VALIDATION_ERROR' || error.code === 'LEAGUE_NOT_FOUND') {
|
||||
this.model = {
|
||||
id: '',
|
||||
success: false,
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(error.details?.message ?? 'Failed to create team');
|
||||
}
|
||||
|
||||
const output = result.unwrap();
|
||||
|
||||
this.model = {
|
||||
id: output.team.id,
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
presentError(): void {
|
||||
this.result = {
|
||||
id: '',
|
||||
success: false,
|
||||
};
|
||||
getResponseModel(): CreateTeamOutputDTO | null {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
getViewModel(): CreateTeamOutputDTO | null {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
get viewModel(): CreateTeamOutputDTO {
|
||||
if (!this.result) {
|
||||
get responseModel(): CreateTeamOutputDTO {
|
||||
if (!this.model) {
|
||||
throw new Error('Presenter not presented');
|
||||
}
|
||||
|
||||
return this.result;
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user