refactor
This commit is contained in:
@@ -6,55 +6,82 @@
|
||||
*/
|
||||
|
||||
import type { IEntity } from '@core/shared/domain';
|
||||
|
||||
import { RacingDomainValidationError, RacingDomainInvariantError } from '../errors/RacingDomainError';
|
||||
import type { LiveryDecal } from '../value-objects/LiveryDecal';
|
||||
import { LiveryTemplateId } from './LiveryTemplateId';
|
||||
import { LeagueId } from './LeagueId';
|
||||
import { SeasonId } from './SeasonId';
|
||||
import { CarId } from './CarId';
|
||||
import { ImageUrl } from './ImageUrl';
|
||||
import { LiveryTemplateCreatedAt } from './LiveryTemplateCreatedAt';
|
||||
import { LiveryTemplateUpdatedAt } from './LiveryTemplateUpdatedAt';
|
||||
|
||||
export interface LiveryTemplateProps {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
seasonId: string;
|
||||
carId: string;
|
||||
baseImageUrl: string;
|
||||
adminDecals: LiveryDecal[];
|
||||
createdAt: Date;
|
||||
updatedAt: Date | undefined;
|
||||
}
|
||||
|
||||
export class LiveryTemplate implements IEntity<string> {
|
||||
readonly id: string;
|
||||
readonly leagueId: string;
|
||||
readonly seasonId: string;
|
||||
readonly carId: string;
|
||||
readonly baseImageUrl: string;
|
||||
export class LiveryTemplate implements IEntity<LiveryTemplateId> {
|
||||
readonly id: LiveryTemplateId;
|
||||
readonly leagueId: LeagueId;
|
||||
readonly seasonId: SeasonId;
|
||||
readonly carId: CarId;
|
||||
readonly baseImageUrl: ImageUrl;
|
||||
readonly adminDecals: LiveryDecal[];
|
||||
readonly createdAt: Date;
|
||||
readonly updatedAt: Date | undefined;
|
||||
readonly createdAt: LiveryTemplateCreatedAt;
|
||||
readonly updatedAt: LiveryTemplateUpdatedAt | undefined;
|
||||
|
||||
private constructor(props: LiveryTemplateProps) {
|
||||
private constructor(props: {
|
||||
id: LiveryTemplateId;
|
||||
leagueId: LeagueId;
|
||||
seasonId: SeasonId;
|
||||
carId: CarId;
|
||||
baseImageUrl: ImageUrl;
|
||||
adminDecals: LiveryDecal[];
|
||||
createdAt: LiveryTemplateCreatedAt;
|
||||
updatedAt?: LiveryTemplateUpdatedAt;
|
||||
}) {
|
||||
this.id = props.id;
|
||||
this.leagueId = props.leagueId;
|
||||
this.seasonId = props.seasonId;
|
||||
this.carId = props.carId;
|
||||
this.baseImageUrl = props.baseImageUrl;
|
||||
this.adminDecals = props.adminDecals;
|
||||
this.createdAt = props.createdAt ?? new Date();
|
||||
this.createdAt = props.createdAt;
|
||||
this.updatedAt = props.updatedAt;
|
||||
}
|
||||
|
||||
static create(props: Omit<LiveryTemplateProps, 'createdAt' | 'adminDecals'> & {
|
||||
static create(props: {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
seasonId: string;
|
||||
carId: string;
|
||||
baseImageUrl: string;
|
||||
createdAt?: Date;
|
||||
adminDecals?: LiveryDecal[];
|
||||
}): LiveryTemplate {
|
||||
this.validate(props);
|
||||
|
||||
const id = LiveryTemplateId.create(props.id);
|
||||
const leagueId = LeagueId.create(props.leagueId);
|
||||
const seasonId = SeasonId.create(props.seasonId);
|
||||
const carId = CarId.create(props.carId);
|
||||
const baseImageUrl = ImageUrl.create(props.baseImageUrl);
|
||||
const createdAt = LiveryTemplateCreatedAt.create(props.createdAt ?? new Date());
|
||||
|
||||
return new LiveryTemplate({
|
||||
...props,
|
||||
createdAt: props.createdAt ?? new Date(),
|
||||
id,
|
||||
leagueId,
|
||||
seasonId,
|
||||
carId,
|
||||
baseImageUrl,
|
||||
adminDecals: props.adminDecals ?? [],
|
||||
createdAt,
|
||||
});
|
||||
}
|
||||
|
||||
private static validate(props: Omit<LiveryTemplateProps, 'createdAt' | 'adminDecals'>): void {
|
||||
private static validate(props: {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
seasonId: string;
|
||||
carId: string;
|
||||
baseImageUrl: string;
|
||||
}): void {
|
||||
if (!props.id || props.id.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('LiveryTemplate ID is required');
|
||||
}
|
||||
@@ -87,7 +114,7 @@ export class LiveryTemplate implements IEntity<string> {
|
||||
return new LiveryTemplate({
|
||||
...this,
|
||||
adminDecals: [...this.adminDecals, decal],
|
||||
updatedAt: new Date(),
|
||||
updatedAt: LiveryTemplateUpdatedAt.create(new Date()),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -104,7 +131,7 @@ export class LiveryTemplate implements IEntity<string> {
|
||||
return new LiveryTemplate({
|
||||
...this,
|
||||
adminDecals: updatedDecals,
|
||||
updatedAt: new Date(),
|
||||
updatedAt: LiveryTemplateUpdatedAt.create(new Date()),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -113,7 +140,7 @@ export class LiveryTemplate implements IEntity<string> {
|
||||
*/
|
||||
updateDecal(decalId: string, updatedDecal: LiveryDecal): LiveryTemplate {
|
||||
const index = this.adminDecals.findIndex(d => d.id === decalId);
|
||||
|
||||
|
||||
if (index === -1) {
|
||||
throw new RacingDomainValidationError('Decal not found in template');
|
||||
}
|
||||
@@ -124,7 +151,7 @@ export class LiveryTemplate implements IEntity<string> {
|
||||
return new LiveryTemplate({
|
||||
...this,
|
||||
adminDecals: updatedDecals,
|
||||
updatedAt: new Date(),
|
||||
updatedAt: LiveryTemplateUpdatedAt.create(new Date()),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user