refactor
This commit is contained in:
@@ -5,45 +5,45 @@
|
||||
*/
|
||||
|
||||
import type { IPrizeRepository } from '../../domain/repositories/IPrizeRepository';
|
||||
import type {
|
||||
IDeletePrizePresenter,
|
||||
DeletePrizeResultDTO,
|
||||
DeletePrizeViewModel,
|
||||
} from '../presenters/IDeletePrizePresenter';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export interface DeletePrizeInput {
|
||||
prizeId: string;
|
||||
}
|
||||
|
||||
export interface DeletePrizeResult {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export type DeletePrizeErrorCode = 'PRIZE_NOT_FOUND' | 'CANNOT_DELETE_AWARDED_PRIZE';
|
||||
|
||||
export class DeletePrizeUseCase
|
||||
implements UseCase<DeletePrizeInput, DeletePrizeResultDTO, DeletePrizeViewModel, IDeletePrizePresenter>
|
||||
implements UseCase<DeletePrizeInput, void, DeletePrizeErrorCode>
|
||||
{
|
||||
constructor(private readonly prizeRepository: IPrizeRepository) {}
|
||||
|
||||
async execute(
|
||||
input: DeletePrizeInput,
|
||||
presenter: IDeletePrizePresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
constructor(
|
||||
private readonly prizeRepository: IPrizeRepository,
|
||||
private readonly output: UseCaseOutputPort<DeletePrizeResult>,
|
||||
) {}
|
||||
|
||||
async execute(input: DeletePrizeInput): Promise<Result<void, ApplicationErrorCode<DeletePrizeErrorCode>>> {
|
||||
const { prizeId } = input;
|
||||
|
||||
const prize = await this.prizeRepository.findById(prizeId);
|
||||
if (!prize) {
|
||||
throw new Error('Prize not found');
|
||||
return Result.err({ code: 'PRIZE_NOT_FOUND' as const });
|
||||
}
|
||||
|
||||
if (prize.awarded) {
|
||||
throw new Error('Cannot delete an awarded prize');
|
||||
return Result.err({ code: 'CANNOT_DELETE_AWARDED_PRIZE' as const });
|
||||
}
|
||||
|
||||
await this.prizeRepository.delete(prizeId);
|
||||
|
||||
const dto: DeletePrizeResultDTO = {
|
||||
success: true,
|
||||
};
|
||||
this.output.present({ success: true });
|
||||
|
||||
presenter.present(dto);
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user