import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { Repository, In } from "typeorm"; import { InjectRepository } from "@nestjs/typeorm"; import { MapMarkerEntity } from './mapmarker.entity'; import { MapMarkerDTO } from './mapmarker.dto'; import { PersonEntity } from '../user/user.entity'; 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> ) { } // insert markers async insertLocation(personId: string, data: MapMarkerDTO) { try { //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; } } // 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; } } }