define a generic folder
This commit is contained in:
		
							
								
								
									
										11
									
								
								src/generic/converters/coordinate.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/generic/converters/coordinate.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| import { CoordinateDto } from '../dto/coordinate.dto'; | ||||
| import { Coordinate } from '../models/coordinate.model'; | ||||
|  | ||||
| export class CoordinateConverter { | ||||
|   static convert<T>(coordinate: Coordinate | CoordinateDto): T { | ||||
|     return { | ||||
|       latitude: coordinate.latitude, | ||||
|       longitude: coordinate.longitude, | ||||
|     } as T; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										4
									
								
								src/generic/converters/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/generic/converters/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| import { CoordinateConverter } from './coordinate'; | ||||
| import { WaypointConverter } from './waypoint'; | ||||
|  | ||||
| export { CoordinateConverter, WaypointConverter }; | ||||
							
								
								
									
										20
									
								
								src/generic/converters/waypoint.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/generic/converters/waypoint.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| import { WaypointDto } from '../dto/waypoint.dto'; | ||||
| import { Waypoint } from '../models/waypoint.model'; | ||||
| import { CoordinateConverter } from './coordinate'; | ||||
|  | ||||
| export class WaypointConverter { | ||||
|   static convert<T, C>(waypoint: Waypoint | WaypointDto): T { | ||||
|     return { | ||||
|       identifier: waypoint.identifier, | ||||
|       coordinate: CoordinateConverter.convert<C>(waypoint.coordinate), | ||||
|     } as T; | ||||
|   } | ||||
|  | ||||
|   static convertList<T, E>(waypoints: Waypoint[] | WaypointDto[]): T[] { | ||||
|     const retval: T[] = []; | ||||
|     waypoints.forEach((waypoint) => | ||||
|       retval.push(WaypointConverter.convert<T, E>(waypoint)), | ||||
|     ); | ||||
|     return retval; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										20
									
								
								src/generic/dto/coordinate.dto.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/generic/dto/coordinate.dto.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| import { IsNotEmpty, IsLatitude, IsLongitude } from 'class-validator'; | ||||
| import { ApiProperty } from '@nestjs/swagger'; | ||||
|  | ||||
| export class CoordinateDto { | ||||
|   @IsNotEmpty() | ||||
|   @ApiProperty({ | ||||
|     description: 'The latitudinal component', | ||||
|     example: 42.8402, | ||||
|   }) | ||||
|   @IsLatitude() | ||||
|   latitude: number; | ||||
|  | ||||
|   @IsNotEmpty() | ||||
|   @ApiProperty({ | ||||
|     description: 'The longitudinal component', | ||||
|     example: 18.8402, | ||||
|   }) | ||||
|   @IsLongitude() | ||||
|   longitude: number; | ||||
| } | ||||
							
								
								
									
										19
									
								
								src/generic/dto/waypoint.dto.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/generic/dto/waypoint.dto.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| import { IsNotEmpty } from 'class-validator'; | ||||
| import { ApiProperty } from '@nestjs/swagger'; | ||||
| import { CoordinateDto } from './coordinate.dto'; | ||||
|  | ||||
| export class WaypointDto { | ||||
|   @IsNotEmpty() | ||||
|   @ApiProperty({ | ||||
|     description: 'The unique waypoint code', | ||||
|     example: 'KETAP', | ||||
|   }) | ||||
|   identifier: string; | ||||
|  | ||||
|   @IsNotEmpty() | ||||
|   @ApiProperty({ | ||||
|     description: 'The latitudinal component', | ||||
|     example: 42.8402, | ||||
|   }) | ||||
|   coordinate: CoordinateDto; | ||||
| } | ||||
							
								
								
									
										21
									
								
								src/generic/models/coordinate.model.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/generic/models/coordinate.model.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||||
| import { Document } from 'mongoose'; | ||||
|  | ||||
| export type CoordinateDocument = Coordinate & Document; | ||||
|  | ||||
| @Schema() | ||||
| export class Coordinate { | ||||
|   @Prop({ | ||||
|     required: true, | ||||
|     type: Number, | ||||
|   }) | ||||
|   latitude: number; | ||||
|  | ||||
|   @Prop({ | ||||
|     required: true, | ||||
|     type: Number, | ||||
|   }) | ||||
|   longitude: number; | ||||
| } | ||||
|  | ||||
| export const CoordinateSchema = SchemaFactory.createForClass(Coordinate); | ||||
							
								
								
									
										22
									
								
								src/generic/models/waypoint.model.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/generic/models/waypoint.model.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||||
| import { Document } from 'mongoose'; | ||||
| import { Coordinate, CoordinateSchema } from './coordinate.model'; | ||||
|  | ||||
| export type WaypointDocument = Waypoint & Document; | ||||
|  | ||||
| @Schema() | ||||
| export class Waypoint { | ||||
|   @Prop({ | ||||
|     required: true, | ||||
|     type: String, | ||||
|   }) | ||||
|   identifier: string; | ||||
|  | ||||
|   @Prop({ | ||||
|     required: true, | ||||
|     type: CoordinateSchema, | ||||
|   }) | ||||
|   coordinate: Coordinate; | ||||
| } | ||||
|  | ||||
| export const WaypointSchema = SchemaFactory.createForClass(Waypoint); | ||||
		Reference in New Issue
	
	Block a user