diff --git a/src/mapmarkers/mapmarker.dto.ts b/src/mapmarkers/mapmarker.dto.ts index eab9dc6c55d4a1aeada99bf82b37f1ce2cb10ca1..f9821f4cbc73b62582263ac63e29c07a75a46b44 100644 --- a/src/mapmarkers/mapmarker.dto.ts +++ b/src/mapmarkers/mapmarker.dto.ts @@ -1,5 +1,9 @@ -import { IsString, IsDateString } from 'class-validator'; -import { Timestamp } from 'typeorm'; +import { IsString, IsJSON } from 'class-validator'; +/* +DTO: MapMarker +- represents servers data handling. +*/ + export class MapMarkerDTO { @IsString() @@ -10,4 +14,6 @@ export class MapMarkerDTO { longitude: string; @IsString() timestamp: string; + @IsJSON() + features: JSON; } \ No newline at end of file diff --git a/src/mapmarkers/mapmarker.entity.ts b/src/mapmarkers/mapmarker.entity.ts index 39ff2b2663473acba2ca17d9b13ed8be3a845c41..41a955d3998702c15ca0429b9e92bad936ced49c 100644 --- a/src/mapmarkers/mapmarker.entity.ts +++ b/src/mapmarkers/mapmarker.entity.ts @@ -2,12 +2,18 @@ import { Entity, Column, PrimaryGeneratedColumn, Timestamp, ManyToOne } from 'ty import { PersonEntity } from 'src/user/user.entity' +/* +Entity: MapMarker +- represents data that database contains on mapmarker +*/ + @Entity('MapMarker') export class MapMarkerEntity { @PrimaryGeneratedColumn('uuid') id: string; @Column({type: 'text'}) latitude: string; @Column({type: 'text'}) longitude: string; @Column({type: 'timestamp'}) timestamp: Timestamp; + @Column({type: 'json', nullable: true}) features: JSON; @ManyToOne(type => PersonEntity, player => player.markers) player: PersonEntity; } \ No newline at end of file diff --git a/src/mapmarkers/mapmarker.service.ts b/src/mapmarkers/mapmarker.service.ts index 5235e4b4abbe0e5573be32145cdb131dc995ffda..c47ddf0f9ce2773addb34c05e8a77d296b8cf713 100644 --- a/src/mapmarkers/mapmarker.service.ts +++ b/src/mapmarkers/mapmarker.service.ts @@ -10,6 +10,7 @@ import { userInfo } from 'os'; @Injectable() export class MapMarkerService { constructor( + //create references to tables as repositories @InjectRepository(MapMarkerEntity) private mapmarkerRepository: Repository<MapMarkerEntity>, @InjectRepository(PersonEntity) private personRepository: Repository<PersonEntity> ) { } @@ -17,10 +18,15 @@ export class MapMarkerService { // insert markers async insertLocation(personId: string, data: MapMarkerDTO) { try { - data.timestamp = new Date(Date.now()).toLocaleString(); //get functions runtime as timestamp + //get functions runtime as timestamp + data.timestamp = new Date(Date.now()).toLocaleString(); + //check from database for the user who uploads the data const user = await this.personRepository.findOne({ where: { id: personId } }) + //create© entity properties const location = await this.mapmarkerRepository.create({ ...data, player: user }); + // insert created entity NOTE: insert method doesn't check for duplicates. await this.mapmarkerRepository.insert(location); + // return data and player id&name return { ...data, player: location.player.nameObject() }; } catch (error) { return error; @@ -30,7 +36,9 @@ export class MapMarkerService { // get all markers async getAllMarkers() { try { + // find all markers with specified player const markers = await this.mapmarkerRepository.find({ relations: ['player'] }); + // return markers from database with said playerdata return markers.map(marker => { return { ...marker, player: marker.player.nameObject() } }); } catch (error) { return error.message; diff --git a/src/mapmarkers/mapmarkers.controller.ts b/src/mapmarkers/mapmarkers.controller.ts index f6c2e0ba8b3242be35f0f99bbdeb020a08e68f55..6aa89c9f3949e1fa39e64779bbc073df46ac0c6b 100644 --- a/src/mapmarkers/mapmarkers.controller.ts +++ b/src/mapmarkers/mapmarkers.controller.ts @@ -20,7 +20,7 @@ export class MapMarkersController { } } - // return all markers + // return all markers through service @Get('getall') async getAll(){ return this.mapmarkerservice.getAllMarkers();