rating
This commit is contained in:
44
core/identity/domain/value-objects/RatingValue.ts
Normal file
44
core/identity/domain/value-objects/RatingValue.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { IValueObject } from '@core/shared/domain';
|
||||
import { IdentityDomainValidationError } from '../errors/IdentityDomainError';
|
||||
|
||||
export interface RatingValueProps {
|
||||
value: number;
|
||||
}
|
||||
|
||||
export class RatingValue implements IValueObject<RatingValueProps> {
|
||||
readonly value: number;
|
||||
|
||||
private constructor(value: number) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
static create(value: number): RatingValue {
|
||||
if (typeof value !== 'number' || isNaN(value)) {
|
||||
throw new IdentityDomainValidationError('Rating value must be a valid number');
|
||||
}
|
||||
|
||||
if (value < 0 || value > 100) {
|
||||
throw new IdentityDomainValidationError(
|
||||
`Rating value must be between 0 and 100, got: ${value}`
|
||||
);
|
||||
}
|
||||
|
||||
return new RatingValue(value);
|
||||
}
|
||||
|
||||
get props(): RatingValueProps {
|
||||
return { value: this.value };
|
||||
}
|
||||
|
||||
equals(other: IValueObject<RatingValueProps>): boolean {
|
||||
return this.value === other.props.value;
|
||||
}
|
||||
|
||||
toNumber(): number {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user