Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • wimma-lab-2019/overflow/ehasa-backend
1 result
Show changes
Commits on Source (11)
This diff is collapsed.
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core'; import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core';
import { TypeOrmModule } from "@nestjs/typeorm"; import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { Connection } from "typeorm"; import { Connection } from 'typeorm';
import { UserModule } from './user/user.module'; import { UserModule } from './user/user.module';
import { HttpErrorFilter } from './shared/http-error.filter'; import { HttpErrorFilter } from './shared/http-error.filter';
import { LoggingInterceptor } from './shared/logging.interceptor'; import { LoggingInterceptor } from './shared/logging.interceptor';
import { MapMarkerModule } from './mapmarkers/mapmarkers.module'; import { MapMarkerModule } from './mapmarkers/mapmarkers.module';
import { NotificationModule } from './notifications/notifications.module';
@Module({ @Module({
imports: [TypeOrmModule.forRoot(), UserModule, MapMarkerModule], imports: [
TypeOrmModule.forRoot(),
UserModule,
MapMarkerModule,
NotificationModule,
],
controllers: [AppController], controllers: [AppController],
providers: [ providers: [
AppService, { AppService,
{
provide: APP_FILTER, provide: APP_FILTER,
useClass: HttpErrorFilter useClass: HttpErrorFilter,
}, },
{ {
provide: APP_INTERCEPTOR, provide: APP_INTERCEPTOR,
useClass: LoggingInterceptor, useClass: LoggingInterceptor,
} },
], ],
}) })
export class AppModule { export class AppModule {
......
import { IsString, IsDateString } from 'class-validator'; import { IsString, IsJSON } from 'class-validator';
import { Timestamp } from 'typeorm'; /*
DTO: MapMarker
- represents servers data handling.
*/
export class MapMarkerDTO { export class MapMarkerDTO {
@IsString() @IsString()
...@@ -10,4 +14,6 @@ export class MapMarkerDTO { ...@@ -10,4 +14,6 @@ export class MapMarkerDTO {
longitude: string; longitude: string;
@IsString() @IsString()
timestamp: string; timestamp: string;
@IsJSON()
features: JSON;
} }
\ No newline at end of file
import { Entity, Column, PrimaryGeneratedColumn, Timestamp, ManyToOne } from 'typeorm'; import { Entity, Column, PrimaryGeneratedColumn, Timestamp, ManyToOne } from 'typeorm';
import { PersonEntity } from 'src/user/user.entity' import { PersonEntity } from '../user/user.entity'
/*
Entity: MapMarker
- represents data that database contains on mapmarker
*/
@Entity('MapMarker') @Entity('MapMarker')
export class MapMarkerEntity { export class MapMarkerEntity {
...@@ -8,6 +13,7 @@ export class MapMarkerEntity { ...@@ -8,6 +13,7 @@ export class MapMarkerEntity {
@Column({type: 'text'}) latitude: string; @Column({type: 'text'}) latitude: string;
@Column({type: 'text'}) longitude: string; @Column({type: 'text'}) longitude: string;
@Column({type: 'timestamp'}) timestamp: Timestamp; @Column({type: 'timestamp'}) timestamp: Timestamp;
@Column({type: 'json', nullable: true}) features: JSON;
@ManyToOne(type => PersonEntity, player => player.markers) @ManyToOne(type => PersonEntity, player => player.markers)
player: PersonEntity; player: PersonEntity;
} }
\ No newline at end of file
...@@ -4,12 +4,13 @@ import { InjectRepository } from "@nestjs/typeorm"; ...@@ -4,12 +4,13 @@ import { InjectRepository } from "@nestjs/typeorm";
import { MapMarkerEntity } from './mapmarker.entity'; import { MapMarkerEntity } from './mapmarker.entity';
import { MapMarkerDTO } from './mapmarker.dto'; import { MapMarkerDTO } from './mapmarker.dto';
import { PersonEntity } from 'dist/user/user.entity'; import { PersonEntity } from '../user/user.entity';
import { userInfo } from 'os'; import { userInfo } from 'os';
@Injectable() @Injectable()
export class MapMarkerService { export class MapMarkerService {
constructor( constructor(
//create references to tables as repositories
@InjectRepository(MapMarkerEntity) private mapmarkerRepository: Repository<MapMarkerEntity>, @InjectRepository(MapMarkerEntity) private mapmarkerRepository: Repository<MapMarkerEntity>,
@InjectRepository(PersonEntity) private personRepository: Repository<PersonEntity> @InjectRepository(PersonEntity) private personRepository: Repository<PersonEntity>
) { } ) { }
...@@ -17,10 +18,16 @@ export class MapMarkerService { ...@@ -17,10 +18,16 @@ export class MapMarkerService {
// insert markers // insert markers
async insertLocation(personId: string, data: MapMarkerDTO) { async insertLocation(personId: string, data: MapMarkerDTO) {
try { try {
data.timestamp = new Date(Date.now()).toLocaleString(); //get functions runtime as timestamp
//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 } }) const user = await this.personRepository.findOne({ where: { id: personId } })
//create&copy entity properties
const location = await this.mapmarkerRepository.create({ ...data, player: user }); 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); await this.mapmarkerRepository.insert(location);
// return data and player id&name
return { ...data, player: location.player.nameObject() }; return { ...data, player: location.player.nameObject() };
} catch (error) { } catch (error) {
return error; return error;
...@@ -30,7 +37,9 @@ export class MapMarkerService { ...@@ -30,7 +37,9 @@ export class MapMarkerService {
// get all markers // get all markers
async getAllMarkers() { async getAllMarkers() {
try { try {
// find all markers with specified player
const markers = await this.mapmarkerRepository.find({ relations: ['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() } }); return markers.map(marker => { return { ...marker, player: marker.player.nameObject() } });
} catch (error) { } catch (error) {
return error.message; return error.message;
......
...@@ -2,13 +2,13 @@ import { Controller, Body, Get, Put, UseGuards } from '@nestjs/common'; ...@@ -2,13 +2,13 @@ import { Controller, Body, Get, Put, UseGuards } from '@nestjs/common';
import { MapMarkerService } from './mapmarker.service'; import { MapMarkerService } from './mapmarker.service';
import { MapMarkerDTO } from './mapmarker.dto'; import { MapMarkerDTO } from './mapmarker.dto';
import { AuthGuard } from 'dist/shared/auth.guard'; import { AuthGuard } from '../shared/auth.guard';
import { User } from 'src/user/user.decorator'; import { User } from '../user/user.decorator';
@Controller('mapmarkers') @Controller('mapmarkers')
export class MapMarkersController { export class MapMarkersController {
constructor(private mapmarkerservice: MapMarkerService){} constructor(private mapmarkerservice: MapMarkerService){}
// Insert figure location, needs "authorization" header with valid Bearer token and content-type json // Insert figure location, needs "authorization" header with valid Bearer token and content-type json
@Put('insertLocation') @Put('insertLocation')
@UseGuards(new AuthGuard()) @UseGuards(new AuthGuard())
...@@ -20,7 +20,7 @@ export class MapMarkersController { ...@@ -20,7 +20,7 @@ export class MapMarkersController {
} }
} }
// return all markers // return all markers through service
@Get('getall') @Get('getall')
async getAll(){ async getAll(){
return this.mapmarkerservice.getAllMarkers(); return this.mapmarkerservice.getAllMarkers();
......
...@@ -4,7 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; ...@@ -4,7 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { MapMarkersController } from './mapmarkers.controller'; import { MapMarkersController } from './mapmarkers.controller';
import { MapMarkerService } from './mapmarker.service'; import { MapMarkerService } from './mapmarker.service';
import { MapMarkerEntity } from './mapmarker.entity'; import { MapMarkerEntity } from './mapmarker.entity';
import { PersonEntity } from 'src/user/user.entity'; import { PersonEntity } from '../user/user.entity';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([MapMarkerEntity, PersonEntity])], imports: [TypeOrmModule.forFeature([MapMarkerEntity, PersonEntity])],
......
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from "typeorm";
// temporary table for warning notifications
@Entity('Notifications')
export class NotificationEntity {
@PrimaryGeneratedColumn('uuid') id: string;
@Column({type: 'text'}) message: string;
@CreateDateColumn() issued: Date;
// TODO:
// when game creation has been implemented, add logic so that the notifications are tied to games
}
\ No newline at end of file
import {
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
OnGatewayInit,
OnGatewayConnection,
OnGatewayDisconnect,
} from '@nestjs/websockets';
import { Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Server, Socket } from 'socket.io';
import { Repository } from 'typeorm';
import { NotificationEntity } from './notification.entity';
@WebSocketGateway()
export class NotificationGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
constructor(
//create references to tables as repositories
@InjectRepository(NotificationEntity)
private notificationRepository: Repository<NotificationEntity>,
) {}
@WebSocketServer()
server: Server;
// for debugging: starting server, accepting connection, terminating connection
private logger: Logger = new Logger('NotificationGateway');
afterInit(server: Server) {
this.logger.log('Server has started');
}
handleConnection(client: Socket, ...args: any[]) {
this.logger.log(`Client ${client.id} connected`);
}
handleDisconnect(client: Socket) {
this.logger.log(`Client ${client.id} disconnected`);
}
// notifications for when game needs to be paused / stopped
@SubscribeMessage('shutItDown')
async handleMessage(client: Socket, text: string) {
this.logger.log(text);
// send the message to all clients listening to warningToPlayers branch
this.server.emit('warningToPlayers', text);
// create entity properties
const message = await this.notificationRepository.create({ message: text });
// insert created entity NOTE: insert method doesn't check for duplicates.
await this.notificationRepository.insert(message);
}
}
import { Module } from '@nestjs/common';
import { NotificationGateway } from './notifications.gateway';
import { NotificationEntity } from './notification.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [TypeOrmModule.forFeature([NotificationEntity])],
providers: [NotificationGateway],
})
export class NotificationModule {}
...@@ -2,8 +2,8 @@ import { Controller, Post, Body, UsePipes, Get, UseGuards } from '@nestjs/common ...@@ -2,8 +2,8 @@ import { Controller, Post, Body, UsePipes, Get, UseGuards } from '@nestjs/common
import { UserService } from './user.service'; import { UserService } from './user.service';
import { UserDTO } from './user.dto'; import { UserDTO } from './user.dto';
import { AuthGuard } from 'src/shared/auth.guard'; import { AuthGuard } from '../shared/auth.guard';
import { ValidationPipe } from 'src/shared/validation.pipe'; import { ValidationPipe } from '../shared/validation.pipe';
@Controller('user') @Controller('user')
export class UserController { export class UserController {
......
import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, OneToMany } from 'typeorm'; import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, OneToMany } from 'typeorm';
import * as bcrypt from 'bcryptjs'; import * as bcrypt from 'bcryptjs';
import * as jwt from 'jsonwebtoken'; import * as jwt from 'jsonwebtoken';
import { MapMarkerEntity } from 'src/mapmarkers/mapmarker.entity'; import { MapMarkerEntity } from '../mapmarkers/mapmarker.entity';
@Entity('Person') @Entity('Person')
export class PersonEntity { export class PersonEntity {
......