refactor
This commit is contained in:
71
core/racing/domain/value-objects/DecalOverride.ts
Normal file
71
core/racing/domain/value-objects/DecalOverride.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
||||
import type { IValueObject } from '@core/shared/domain';
|
||||
|
||||
export interface DecalOverrideProps {
|
||||
leagueId: string;
|
||||
seasonId: string;
|
||||
decalId: string;
|
||||
newX: number;
|
||||
newY: number;
|
||||
}
|
||||
|
||||
export class DecalOverride implements IValueObject<DecalOverrideProps> {
|
||||
readonly leagueId: string;
|
||||
readonly seasonId: string;
|
||||
readonly decalId: string;
|
||||
readonly newX: number;
|
||||
readonly newY: number;
|
||||
|
||||
private constructor(props: DecalOverrideProps) {
|
||||
this.leagueId = props.leagueId;
|
||||
this.seasonId = props.seasonId;
|
||||
this.decalId = props.decalId;
|
||||
this.newX = props.newX;
|
||||
this.newY = props.newY;
|
||||
}
|
||||
|
||||
static create(props: DecalOverrideProps): DecalOverride {
|
||||
this.validate(props);
|
||||
return new DecalOverride(props);
|
||||
}
|
||||
|
||||
private static validate(props: DecalOverrideProps): void {
|
||||
if (!props.leagueId || props.leagueId.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('DecalOverride leagueId is required');
|
||||
}
|
||||
if (!props.seasonId || props.seasonId.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('DecalOverride seasonId is required');
|
||||
}
|
||||
if (!props.decalId || props.decalId.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('DecalOverride decalId is required');
|
||||
}
|
||||
if (props.newX < 0 || props.newX > 1) {
|
||||
throw new RacingDomainValidationError('DecalOverride newX must be between 0 and 1');
|
||||
}
|
||||
if (props.newY < 0 || props.newY > 1) {
|
||||
throw new RacingDomainValidationError('DecalOverride newY must be between 0 and 1');
|
||||
}
|
||||
}
|
||||
|
||||
equals(other: IValueObject<DecalOverrideProps>): boolean {
|
||||
const a = this.props;
|
||||
const b = other.props;
|
||||
return (
|
||||
a.leagueId === b.leagueId &&
|
||||
a.seasonId === b.seasonId &&
|
||||
a.decalId === b.decalId &&
|
||||
a.newX === b.newX &&
|
||||
a.newY === b.newY
|
||||
);
|
||||
}
|
||||
|
||||
get props(): DecalOverrideProps {
|
||||
return {
|
||||
leagueId: this.leagueId,
|
||||
seasonId: this.seasonId,
|
||||
decalId: this.decalId,
|
||||
newX: this.newX,
|
||||
newY: this.newY,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user