refactor racing use cases
This commit is contained in:
@@ -1,30 +1,45 @@
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { TotalDriversOutputPort } from '../ports/output/TotalDriversOutputPort';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { AsyncUseCase , Logger } from '@core/shared/application';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
|
||||
/**
|
||||
* Use Case for retrieving total number of drivers.
|
||||
* Input type for retrieving total number of drivers.
|
||||
*/
|
||||
export class GetTotalDriversUseCase implements AsyncUseCase<void, TotalDriversOutputPort, 'REPOSITORY_ERROR'>
|
||||
{
|
||||
export type GetTotalDriversInput = {};
|
||||
|
||||
/**
|
||||
* Domain result model for the total number of drivers.
|
||||
*/
|
||||
export type GetTotalDriversResult = {
|
||||
totalDrivers: number;
|
||||
};
|
||||
|
||||
export type GetTotalDriversErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetTotalDriversUseCase {
|
||||
constructor(
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetTotalDriversResult>,
|
||||
) {}
|
||||
|
||||
async execute(): Promise<Result<TotalDriversOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
|
||||
async execute(
|
||||
_input: GetTotalDriversInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetTotalDriversErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const drivers = await this.driverRepository.findAll();
|
||||
const output: TotalDriversOutputPort = {
|
||||
totalDrivers: drivers.length,
|
||||
};
|
||||
const result: GetTotalDriversResult = { totalDrivers: drivers.length };
|
||||
|
||||
return Result.ok(output);
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error('Error retrieving total drivers', error as Error);
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'Failed to retrieve total drivers' } });
|
||||
const message = (error as Error | undefined)?.message ?? 'Failed to compute total drivers';
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user