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

Merge branch 'relationship' into 'master'

Added relationship between tables

See merge request !3
parents 47ee5986 d1be4eb1
No related branches found
No related tags found
1 merge request!3Added relationship between tables
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
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;
}
......
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();
}
}
......@@ -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]
})
......
import { createParamDecorator } from "@nestjs/common";
export const User = createParamDecorator((data, req) => {
return data ? req.user[data] : req.user;
})
\ No newline at end of file
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);
}
......
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