refactor
This commit is contained in:
@@ -7,22 +7,20 @@
|
||||
|
||||
import type { IEntity } from '@core/shared/domain';
|
||||
|
||||
import type { LiveryDecal } from '../value-objects/LiveryDecal';
|
||||
|
||||
export interface DecalOverride {
|
||||
leagueId: string;
|
||||
seasonId: string;
|
||||
decalId: string;
|
||||
newX: number;
|
||||
newY: number;
|
||||
}
|
||||
import { RacingDomainValidationError, RacingDomainInvariantError } from '../errors/RacingDomainError';
|
||||
import { LiveryDecal } from '../value-objects/LiveryDecal';
|
||||
import { DecalOverride } from '../value-objects/DecalOverride';
|
||||
import { DriverId } from '../value-objects/DriverId';
|
||||
import { GameId } from './GameId';
|
||||
import { CarId } from '../value-objects/CarId';
|
||||
import { ImageUrl } from '../value-objects/ImageUrl';
|
||||
|
||||
export interface DriverLiveryProps {
|
||||
id: string;
|
||||
driverId: string;
|
||||
gameId: string;
|
||||
carId: string;
|
||||
uploadedImageUrl: string;
|
||||
driverId: DriverId;
|
||||
gameId: GameId;
|
||||
carId: CarId;
|
||||
uploadedImageUrl: ImageUrl;
|
||||
userDecals: LiveryDecal[];
|
||||
leagueOverrides: DecalOverride[];
|
||||
createdAt: Date;
|
||||
@@ -32,10 +30,10 @@ export interface DriverLiveryProps {
|
||||
|
||||
export class DriverLivery implements IEntity<string> {
|
||||
readonly id: string;
|
||||
readonly driverId: string;
|
||||
readonly gameId: string;
|
||||
readonly carId: string;
|
||||
readonly uploadedImageUrl: string;
|
||||
readonly driverId: DriverId;
|
||||
readonly gameId: GameId;
|
||||
readonly carId: CarId;
|
||||
readonly uploadedImageUrl: ImageUrl;
|
||||
readonly userDecals: LiveryDecal[];
|
||||
readonly leagueOverrides: DecalOverride[];
|
||||
readonly createdAt: Date;
|
||||
@@ -55,7 +53,12 @@ export class DriverLivery implements IEntity<string> {
|
||||
this.validatedAt = props.validatedAt;
|
||||
}
|
||||
|
||||
static create(props: Omit<DriverLiveryProps, 'createdAt' | 'userDecals' | 'leagueOverrides'> & {
|
||||
static create(props: {
|
||||
id: string;
|
||||
driverId: string;
|
||||
gameId: string;
|
||||
carId: string;
|
||||
uploadedImageUrl: string;
|
||||
createdAt?: Date;
|
||||
userDecals?: LiveryDecal[];
|
||||
leagueOverrides?: DecalOverride[];
|
||||
@@ -63,14 +66,26 @@ export class DriverLivery implements IEntity<string> {
|
||||
this.validate(props);
|
||||
|
||||
return new DriverLivery({
|
||||
...props,
|
||||
createdAt: props.createdAt ?? new Date(),
|
||||
id: props.id,
|
||||
driverId: DriverId.create(props.driverId),
|
||||
gameId: GameId.create(props.gameId),
|
||||
carId: CarId.create(props.carId),
|
||||
uploadedImageUrl: ImageUrl.create(props.uploadedImageUrl),
|
||||
userDecals: props.userDecals ?? [],
|
||||
leagueOverrides: props.leagueOverrides ?? [],
|
||||
createdAt: props.createdAt ?? new Date(),
|
||||
updatedAt: undefined,
|
||||
validatedAt: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
private static validate(props: Omit<DriverLiveryProps, 'createdAt' | 'userDecals' | 'leagueOverrides'>): void {
|
||||
private static validate(props: {
|
||||
id: string;
|
||||
driverId: string;
|
||||
gameId: string;
|
||||
carId: string;
|
||||
uploadedImageUrl: string;
|
||||
}): void {
|
||||
if (!props.id || props.id.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('DriverLivery ID is required');
|
||||
}
|
||||
@@ -173,8 +188,8 @@ export class DriverLivery implements IEntity<string> {
|
||||
o => o.leagueId === leagueId && o.seasonId === seasonId && o.decalId === decalId
|
||||
);
|
||||
|
||||
const override: DecalOverride = { leagueId, seasonId, decalId, newX, newY };
|
||||
|
||||
const override = DecalOverride.create({ leagueId, seasonId, decalId, newX, newY });
|
||||
|
||||
let updatedOverrides: DecalOverride[];
|
||||
if (existingIndex >= 0) {
|
||||
updatedOverrides = [...this.leagueOverrides];
|
||||
|
||||
Reference in New Issue
Block a user