diff --git a/src/mapmarkers/mapmarker.entity.ts b/src/mapmarkers/mapmarker.entity.ts index 0134f91ef18aff40ab657f1720fef48fba4829e9..39ff2b2663473acba2ca17d9b13ed8be3a845c41 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 9c45c0861646eca5de067d45afd64c760215743d..5235e4b4abbe0e5573be32145cdb131dc995ffda 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 f07570fa909300fff101bdf5213d158c14fba181..f6c2e0ba8b3242be35f0f99bbdeb020a08e68f55 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 95ce9d08ae34839ee311d9937f01cb1b004a84e6..06c45c209fa8bd7f876359fa447e69c084ed3915 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 0000000000000000000000000000000000000000..29f93b7b3e58ab16f25e25414d025b567aea3efb --- /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 f9f138167d858cdaafab049dae22c8205309517c..e718ce4834b859a3b4bd316a3b78efb313437035 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); }