From aa8be4433d90790b69f889d385d251f57747890d Mon Sep 17 00:00:00 2001 From: Samuli Virtapohja <l4721@student.jamk.fi> Date: Tue, 4 Jun 2019 14:16:05 +0300 Subject: [PATCH] timestamps added --- src/mapmarkers/mapmarker.dto.ts | 6 ++++-- src/mapmarkers/mapmarker.entity.ts | 8 ++------ src/mapmarkers/mapmarker.service.ts | 21 ++++++++++++--------- src/mapmarkers/mapmarkers.controller.ts | 20 ++++++++------------ 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/mapmarkers/mapmarker.dto.ts b/src/mapmarkers/mapmarker.dto.ts index 2ce73f2..eab9dc6 100644 --- a/src/mapmarkers/mapmarker.dto.ts +++ b/src/mapmarkers/mapmarker.dto.ts @@ -1,4 +1,5 @@ -import { IsString } from 'class-validator'; +import { IsString, IsDateString } from 'class-validator'; +import { Timestamp } from 'typeorm'; export class MapMarkerDTO { @IsString() @@ -7,5 +8,6 @@ export class MapMarkerDTO { latitude: string; @IsString() longitude: string; - + @IsString() + timestamp: string; } \ No newline at end of file diff --git a/src/mapmarkers/mapmarker.entity.ts b/src/mapmarkers/mapmarker.entity.ts index 1314e95..0134f91 100644 --- a/src/mapmarkers/mapmarker.entity.ts +++ b/src/mapmarkers/mapmarker.entity.ts @@ -1,13 +1,9 @@ -import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, PrimaryColumn } from 'typeorm'; - -import * as jwt from 'jsonwebtoken'; -import { identifier } from '@babel/types'; +import { Entity, Column, PrimaryGeneratedColumn, Timestamp } from 'typeorm'; @Entity('MapMarker') export class MapMarkerEntity { @PrimaryGeneratedColumn('uuid') id: string; - @Column({type: 'text'}) latitude: string; @Column({type: 'text'}) longitude: string; - + @Column({type: 'timestamp'}) timestamp: Timestamp; } \ No newline at end of file diff --git a/src/mapmarkers/mapmarker.service.ts b/src/mapmarkers/mapmarker.service.ts index c95748f..9c45c08 100644 --- a/src/mapmarkers/mapmarker.service.ts +++ b/src/mapmarkers/mapmarker.service.ts @@ -11,19 +11,22 @@ export class MapMarkerService { // insert markers async insertLocation(data: MapMarkerDTO){ - - // check for token return if not set return null or something - const { latitude, longitude } = data; - let location = await this.mapmarkerRepository.create(data); - await this.mapmarkerRepository.save(location); + try{ + data.timestamp = new Date(Date.now()).toLocaleString(); //get functions runtime as timestamp + let location = await this.mapmarkerRepository.create(data); + await this.mapmarkerRepository.insert(location); + }catch(error){ + return error; + } } // get all markers async getAllMarkers(): Promise<MapMarkerEntity[]>{ + try{ + return this.mapmarkerRepository.find(); + }catch(error){ + return error.message; + } - // const users= this.mapmarkerRepository.find(); - // users.map - // user. - return this.mapmarkerRepository.find(); } } \ No newline at end of file diff --git a/src/mapmarkers/mapmarkers.controller.ts b/src/mapmarkers/mapmarkers.controller.ts index 793f3cb..f07570f 100644 --- a/src/mapmarkers/mapmarkers.controller.ts +++ b/src/mapmarkers/mapmarkers.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Post, Body, UsePipes, ValidationPipe, Get, Put, UseGuards, Req } from '@nestjs/common'; +import { Controller, Body, Get, Put } from '@nestjs/common'; import { MapMarkerService } from './mapmarker.service'; import { MapMarkerDTO } from './mapmarker.dto'; @@ -6,25 +6,21 @@ import { MapMarkerDTO } from './mapmarker.dto'; @Controller('mapmarkers') export class MapMarkersController { constructor(private mapmarkerservice: MapMarkerService){} - - // update user location - @Post('location') - async getLoctaion(@Body() data: MapMarkerDTO){ - // get user location && id - // update location to database - return this.mapmarkerservice.insertLocation(data); - } // Insert figure location @Put('insertLocation') - async insertLocation(@Body() data: MapMarkerDTO){ - this.mapmarkerservice.insertLocation(data); + async insertLocation(@Body() data: MapMarkerDTO): Promise<string>{ + try { + return this.mapmarkerservice.insertLocation(data); + } catch (error) { + return error; + } } @Get('getall') async getAll(){ + // tarkistaa oikeudet // hakee kaikki - // palauttaa vain halutut return this.mapmarkerservice.getAllMarkers(); } } -- GitLab