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