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