bette use of value objects

This commit is contained in:
sbriat
2023-09-13 15:28:07 +02:00
parent 4731020e8a
commit 74fb2c120e
22 changed files with 387 additions and 96 deletions

View File

@@ -1,6 +1,6 @@
import { QueryBase } from '@mobicoop/ddd-library';
import { Waypoint } from '@modules/geography/core/domain/route.types';
import { GeorouterSettings } from '../../types/georouter-settings.type';
import { Waypoint } from '@modules/geography/core/domain/route.types';
export class GetRouteQuery extends QueryBase {
readonly waypoints: Waypoint[];

View File

@@ -20,6 +20,7 @@ export interface CreateRouteProps {
georouterSettings: GeorouterSettings;
}
// Types used outside the domain
export type Route = {
distance: number;
duration: number;

View File

@@ -1,30 +1,17 @@
import {
ArgumentInvalidException,
ArgumentOutOfRangeException,
ValueObject,
} from '@mobicoop/ddd-library';
import { ArgumentInvalidException, ValueObject } from '@mobicoop/ddd-library';
import { PointProps } from './point.value-object';
/** Note:
* Value Objects with multiple properties can contain
* other Value Objects inside if needed.
* */
export interface StepProps {
lon: number;
lat: number;
export interface StepProps extends PointProps {
duration: number;
distance: number;
}
export class Step extends ValueObject<StepProps> {
get lon(): number {
return this.props.lon;
}
get lat(): number {
return this.props.lat;
}
get duration(): number {
return this.props.duration;
}
@@ -33,6 +20,14 @@ export class Step extends ValueObject<StepProps> {
return this.props.distance;
}
get lon(): number {
return this.props.lon;
}
get lat(): number {
return this.props.lat;
}
protected validate(props: StepProps): void {
if (props.duration < 0)
throw new ArgumentInvalidException(
@@ -42,9 +37,5 @@ export class Step extends ValueObject<StepProps> {
throw new ArgumentInvalidException(
'distance must be greater than or equal to 0',
);
if (props.lon > 180 || props.lon < -180)
throw new ArgumentOutOfRangeException('lon must be between -180 and 180');
if (props.lat > 90 || props.lat < -90)
throw new ArgumentOutOfRangeException('lat must be between -90 and 90');
}
}

View File

@@ -1,18 +1,13 @@
import {
ArgumentInvalidException,
ArgumentOutOfRangeException,
ValueObject,
} from '@mobicoop/ddd-library';
import { ArgumentInvalidException, ValueObject } from '@mobicoop/ddd-library';
import { PointProps } from './point.value-object';
/** Note:
* Value Objects with multiple properties can contain
* other Value Objects inside if needed.
* */
export interface WaypointProps {
export interface WaypointProps extends PointProps {
position: number;
lon: number;
lat: number;
}
export class Waypoint extends ValueObject<WaypointProps> {
@@ -33,9 +28,5 @@ export class Waypoint extends ValueObject<WaypointProps> {
throw new ArgumentInvalidException(
'position must be greater than or equal to 0',
);
if (props.lon > 180 || props.lon < -180)
throw new ArgumentOutOfRangeException('lon must be between -180 and 180');
if (props.lat > 90 || props.lat < -90)
throw new ArgumentOutOfRangeException('lat must be between -90 and 90');
}
}