Skip to content
Snippets Groups Projects
Commit 427feab9 authored by Samuli Virtapohja's avatar Samuli Virtapohja
Browse files

Add comments the code on mapmarker

parent be97636e
No related branches found
No related tags found
3 merge requests!59Development to master,!9Development,!6GeoJSON data to database
import { IsString, IsJSON } from 'class-validator'; import { IsString, IsJSON } from 'class-validator';
/*
DTO: MapMarker
- represents servers data handling.
*/
export class MapMarkerDTO { export class MapMarkerDTO {
@IsString() @IsString()
......
...@@ -2,6 +2,11 @@ import { Entity, Column, PrimaryGeneratedColumn, Timestamp, ManyToOne } from 'ty ...@@ -2,6 +2,11 @@ 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;
......
...@@ -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