Some checks failed
CI / lint-typecheck (pull_request) Failing after 4m51s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
/**
|
|
* SaveRatingUseCase
|
|
*
|
|
* Saves a driver's rating to the repository.
|
|
*/
|
|
|
|
import { RatingRepository } from '../../ports/RatingRepository';
|
|
import { Rating } from '../../domain/Rating';
|
|
import { RatingComponents } from '../../domain/RatingComponents';
|
|
import { DriverId } from '../../../racing/domain/entities/DriverId';
|
|
import { RaceId } from '../../../racing/domain/entities/RaceId';
|
|
|
|
export interface SaveRatingUseCasePorts {
|
|
ratingRepository: RatingRepository;
|
|
}
|
|
|
|
export interface SaveRatingRequest {
|
|
driverId: string;
|
|
raceId: string;
|
|
rating: number;
|
|
components: RatingComponents;
|
|
}
|
|
|
|
export class SaveRatingUseCase {
|
|
constructor(private readonly ports: SaveRatingUseCasePorts) {}
|
|
|
|
async execute(request: SaveRatingRequest): Promise<void> {
|
|
const { ratingRepository } = this.ports;
|
|
const { driverId, raceId, rating, components } = request;
|
|
|
|
try {
|
|
const ratingEntity = Rating.create({
|
|
driverId: DriverId.create(driverId),
|
|
raceId: RaceId.create(raceId),
|
|
rating,
|
|
components,
|
|
timestamp: new Date(),
|
|
});
|
|
|
|
await ratingRepository.save(ratingEntity);
|
|
} catch (error) {
|
|
throw new Error(`Failed to save rating: ${error}`);
|
|
}
|
|
}
|
|
}
|