From d1be4eb1fd227e1c06fc2c686f8850b3e6a7bc3d Mon Sep 17 00:00:00 2001 From: L4168 <L4168@student.jamk.fi> Date: Wed, 5 Jun 2019 13:23:43 +0300 Subject: [PATCH] Added relationship between tables --- src/mapmarkers/mapmarker.entity.ts | 6 ++++- src/mapmarkers/mapmarker.service.ts | 30 ++++++++++++++++--------- src/mapmarkers/mapmarkers.controller.ts | 14 +++++++----- src/mapmarkers/mapmarkers.module.ts | 3 ++- src/user/user.decorator.ts | 5 +++++ src/user/user.entity.ts | 11 ++++++++- 6 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 src/user/user.decorator.ts diff --git a/src/mapmarkers/mapmarker.entity.ts b/src/mapmarkers/mapmarker.entity.ts index 0134f91..39ff2b2 100644 --- a/src/mapmarkers/mapmarker.entity.ts +++ b/src/mapmarkers/mapmarker.entity.ts @@ -1,4 +1,6 @@ -import { Entity, Column, PrimaryGeneratedColumn, Timestamp } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, Timestamp, ManyToOne } from 'typeorm'; + +import { PersonEntity } from 'src/user/user.entity' @Entity('MapMarker') export class MapMarkerEntity { @@ -6,4 +8,6 @@ export class MapMarkerEntity { @Column({type: 'text'}) latitude: string; @Column({type: 'text'}) longitude: string; @Column({type: 'timestamp'}) timestamp: Timestamp; + @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 9c45c08..5235e4b 100644 --- a/src/mapmarkers/mapmarker.service.ts +++ b/src/mapmarkers/mapmarker.service.ts @@ -1,30 +1,38 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; -import { Repository } from "typeorm"; +import { Repository, In } from "typeorm"; import { InjectRepository } from "@nestjs/typeorm"; import { MapMarkerEntity } from './mapmarker.entity'; import { MapMarkerDTO } from './mapmarker.dto'; +import { PersonEntity } from 'dist/user/user.entity'; +import { userInfo } from 'os'; @Injectable() export class MapMarkerService { - constructor(@InjectRepository(MapMarkerEntity) private mapmarkerRepository: Repository<MapMarkerEntity>){} + constructor( + @InjectRepository(MapMarkerEntity) private mapmarkerRepository: Repository<MapMarkerEntity>, + @InjectRepository(PersonEntity) private personRepository: Repository<PersonEntity> + ) { } // insert markers - async insertLocation(data: MapMarkerDTO){ - try{ + async insertLocation(personId: string, data: MapMarkerDTO) { + 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){ + const user = await this.personRepository.findOne({ where: { id: personId } }) + const location = await this.mapmarkerRepository.create({ ...data, player: user }); + await this.mapmarkerRepository.insert(location); + return { ...data, player: location.player.nameObject() }; + } catch (error) { return error; } } // get all markers - async getAllMarkers(): Promise<MapMarkerEntity[]>{ - try{ - return this.mapmarkerRepository.find(); - }catch(error){ + async getAllMarkers() { + try { + const markers = await this.mapmarkerRepository.find({ relations: ['player'] }); + 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 f07570f..f6c2e0b 100644 --- a/src/mapmarkers/mapmarkers.controller.ts +++ b/src/mapmarkers/mapmarkers.controller.ts @@ -1,26 +1,28 @@ -import { Controller, Body, Get, Put } from '@nestjs/common'; +import { Controller, Body, Get, Put, UseGuards } from '@nestjs/common'; import { MapMarkerService } from './mapmarker.service'; import { MapMarkerDTO } from './mapmarker.dto'; +import { AuthGuard } from 'dist/shared/auth.guard'; +import { User } from 'src/user/user.decorator'; @Controller('mapmarkers') export class MapMarkersController { constructor(private mapmarkerservice: MapMarkerService){} - // Insert figure location + // Insert figure location, needs "authorization" header with valid Bearer token and content-type json @Put('insertLocation') - async insertLocation(@Body() data: MapMarkerDTO): Promise<string>{ + @UseGuards(new AuthGuard()) + async insertLocation(@User('id') person, @Body() data: MapMarkerDTO): Promise<string>{ try { - return this.mapmarkerservice.insertLocation(data); + return this.mapmarkerservice.insertLocation(person, data); } catch (error) { return error; } } + // return all markers @Get('getall') async getAll(){ - // tarkistaa oikeudet - // hakee kaikki return this.mapmarkerservice.getAllMarkers(); } } diff --git a/src/mapmarkers/mapmarkers.module.ts b/src/mapmarkers/mapmarkers.module.ts index 95ce9d0..06c45c2 100644 --- a/src/mapmarkers/mapmarkers.module.ts +++ b/src/mapmarkers/mapmarkers.module.ts @@ -4,9 +4,10 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { MapMarkersController } from './mapmarkers.controller'; import { MapMarkerService } from './mapmarker.service'; import { MapMarkerEntity } from './mapmarker.entity'; +import { PersonEntity } from 'src/user/user.entity'; @Module({ - imports: [TypeOrmModule.forFeature([MapMarkerEntity])], + imports: [TypeOrmModule.forFeature([MapMarkerEntity, PersonEntity])], controllers: [MapMarkersController], providers: [MapMarkerService] }) diff --git a/src/user/user.decorator.ts b/src/user/user.decorator.ts new file mode 100644 index 0000000..29f93b7 --- /dev/null +++ b/src/user/user.decorator.ts @@ -0,0 +1,5 @@ +import { createParamDecorator } from "@nestjs/common"; + +export const User = createParamDecorator((data, req) => { + return data ? req.user[data] : req.user; +}) \ No newline at end of file diff --git a/src/user/user.entity.ts b/src/user/user.entity.ts index f9f1381..e718ce4 100644 --- a/src/user/user.entity.ts +++ b/src/user/user.entity.ts @@ -1,12 +1,15 @@ -import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, OneToMany } from 'typeorm'; import * as bcrypt from 'bcryptjs'; import * as jwt from 'jsonwebtoken'; +import { MapMarkerEntity } from 'src/mapmarkers/mapmarker.entity'; @Entity('Person') export class PersonEntity { @PrimaryGeneratedColumn('uuid') id: string; @Column({type: 'text', unique: true}) name: string; @Column('text') password: string; + @OneToMany(type => MapMarkerEntity, marker => marker.player) + markers: MapMarkerEntity[]; @BeforeInsert() async hashPassword() { @@ -18,6 +21,12 @@ export class PersonEntity { return {name, token}; } + // returns username and the id + nameObject() { + const {id, name} = this; + return {id, name}; + } + async comparePassword(attempt: string) { return await bcrypt.compareSync(attempt, this.password); } -- GitLab