Skip to content
Snippets Groups Projects
Commit 692519bf authored by L4168's avatar L4168
Browse files

Merge branch 'GeoJSON2' into 'Development'

GeoJSON data to database

See merge request !6
parents 95452139 427feab9
No related branches found
No related tags found
3 merge requests!59Development to master,!9Development,!6GeoJSON data to database
import { IsString, IsDateString } from 'class-validator'; import { IsString, IsJSON } from 'class-validator';
import { Timestamp } from 'typeorm'; /*
DTO: MapMarker
- represents servers data handling.
*/
export class MapMarkerDTO { export class MapMarkerDTO {
@IsString() @IsString()
...@@ -10,4 +14,6 @@ export class MapMarkerDTO { ...@@ -10,4 +14,6 @@ export class MapMarkerDTO {
longitude: string; longitude: string;
@IsString() @IsString()
timestamp: string; timestamp: string;
@IsJSON()
features: JSON;
} }
\ No newline at end of file
...@@ -2,12 +2,18 @@ import { Entity, Column, PrimaryGeneratedColumn, Timestamp, ManyToOne } from 'ty ...@@ -2,12 +2,18 @@ import { Entity, Column, PrimaryGeneratedColumn, Timestamp, ManyToOne } from 'ty
import { PersonEntity } from 'src/user/user.entity' import { PersonEntity } from 'src/user/user.entity'
/*
Entity: MapMarker
- represents data that database contains on mapmarker
*/
@Entity('MapMarker') @Entity('MapMarker')
export class MapMarkerEntity { export class MapMarkerEntity {
@PrimaryGeneratedColumn('uuid') id: string; @PrimaryGeneratedColumn('uuid') id: string;
@Column({type: 'text'}) latitude: string; @Column({type: 'text'}) latitude: string;
@Column({type: 'text'}) longitude: string; @Column({type: 'text'}) longitude: string;
@Column({type: 'timestamp'}) timestamp: Timestamp; @Column({type: 'timestamp'}) timestamp: Timestamp;
@Column({type: 'json', nullable: true}) features: JSON;
@ManyToOne(type => PersonEntity, player => player.markers) @ManyToOne(type => PersonEntity, player => player.markers)
player: PersonEntity; player: PersonEntity;
} }
\ No newline at end of file
...@@ -10,6 +10,7 @@ import { userInfo } from 'os'; ...@@ -10,6 +10,7 @@ import { userInfo } from 'os';
@Injectable() @Injectable()
export class MapMarkerService { export class MapMarkerService {
constructor( constructor(
//create references to tables as repositories
@InjectRepository(MapMarkerEntity) private mapmarkerRepository: Repository<MapMarkerEntity>, @InjectRepository(MapMarkerEntity) private mapmarkerRepository: Repository<MapMarkerEntity>,
@InjectRepository(PersonEntity) private personRepository: Repository<PersonEntity> @InjectRepository(PersonEntity) private personRepository: Repository<PersonEntity>
) { } ) { }
...@@ -17,10 +18,15 @@ export class MapMarkerService { ...@@ -17,10 +18,15 @@ export class MapMarkerService {
// insert markers // insert markers
async insertLocation(personId: string, data: MapMarkerDTO) { async insertLocation(personId: string, data: MapMarkerDTO) {
try { 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 } }) const user = await this.personRepository.findOne({ where: { id: personId } })
//create&copy entity properties
const location = await this.mapmarkerRepository.create({ ...data, player: user }); 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); await this.mapmarkerRepository.insert(location);
// return data and player id&name
return { ...data, player: location.player.nameObject() }; return { ...data, player: location.player.nameObject() };
} catch (error) { } catch (error) {
return error; return error;
...@@ -30,7 +36,9 @@ export class MapMarkerService { ...@@ -30,7 +36,9 @@ export class MapMarkerService {
// get all markers // get all markers
async getAllMarkers() { async getAllMarkers() {
try { try {
// find all markers with specified player
const markers = await this.mapmarkerRepository.find({ relations: ['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() } }); return markers.map(marker => { return { ...marker, player: marker.player.nameObject() } });
} catch (error) { } catch (error) {
return error.message; return error.message;
......
...@@ -20,7 +20,7 @@ export class MapMarkersController { ...@@ -20,7 +20,7 @@ export class MapMarkersController {
} }
} }
// return all markers // return all markers through service
@Get('getall') @Get('getall')
async getAll(){ async getAll(){
return this.mapmarkerservice.getAllMarkers(); return this.mapmarkerservice.getAllMarkers();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment