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 { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core';
import { TypeOrmModule } from "@nestjs/typeorm";
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Connection } from "typeorm";
import { Connection } from 'typeorm';
import { UserModule } from './user/user.module';
import { HttpErrorFilter } from './shared/http-error.filter';
import { LoggingInterceptor } from './shared/logging.interceptor';
import { MapMarkerModule } from './mapmarkers/mapmarkers.module';
import { NotificationModule } from './notifications/notifications.module';
@Module({
imports: [TypeOrmModule.forRoot(), UserModule, MapMarkerModule],
imports: [
TypeOrmModule.forRoot(),
UserModule,
MapMarkerModule,
NotificationModule,
],
controllers: [AppController],
providers: [
AppService, {
AppService,
{
provide: APP_FILTER,
useClass: HttpErrorFilter
useClass: HttpErrorFilter,
},
{
provide: APP_INTERCEPTOR,
useClass: LoggingInterceptor,
}
},
],
})
export class AppModule {
......
import { IsString, IsDateString } from 'class-validator';
import { Timestamp } from 'typeorm';
import { IsString, IsJSON } from 'class-validator';
/*
DTO: MapMarker
- represents servers data handling.
*/
export class MapMarkerDTO {
@IsString()
......@@ -10,4 +14,6 @@ export class MapMarkerDTO {
longitude: string;
@IsString()
timestamp: string;
@IsJSON()
features: JSON;
}
\ No newline at end of file
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')
export class MapMarkerEntity {
......@@ -8,6 +13,7 @@ export class MapMarkerEntity {
@Column({type: 'text'}) latitude: string;
@Column({type: 'text'}) longitude: string;
@Column({type: 'timestamp'}) timestamp: Timestamp;
@Column({type: 'json', nullable: true}) features: JSON;
@ManyToOne(type => PersonEntity, player => player.markers)
player: PersonEntity;
}
\ No newline at end of file
......@@ -4,12 +4,13 @@ import { InjectRepository } from "@nestjs/typeorm";
import { MapMarkerEntity } from './mapmarker.entity';
import { MapMarkerDTO } from './mapmarker.dto';
import { PersonEntity } from 'dist/user/user.entity';
import { PersonEntity } from '../user/user.entity';
import { userInfo } from 'os';
@Injectable()
export class MapMarkerService {
constructor(
//create references to tables as repositories
@InjectRepository(MapMarkerEntity) private mapmarkerRepository: Repository<MapMarkerEntity>,
@InjectRepository(PersonEntity) private personRepository: Repository<PersonEntity>
) { }
......@@ -17,10 +18,16 @@ export class MapMarkerService {
// insert markers
async insertLocation(personId: string, data: MapMarkerDTO) {
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 } })
//create&copy entity properties
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);
// return data and player id&name
return { ...data, player: location.player.nameObject() };
} catch (error) {
return error;
......@@ -30,7 +37,9 @@ export class MapMarkerService {
// get all markers
async getAllMarkers() {
try {
// find all markers with specified 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() } });
} catch (error) {
return error.message;
......
......@@ -2,13 +2,13 @@ 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';
import { AuthGuard } from '../shared/auth.guard';
import { User } from '../user/user.decorator';
@Controller('mapmarkers')
export class MapMarkersController {
constructor(private mapmarkerservice: MapMarkerService){}
// Insert figure location, needs "authorization" header with valid Bearer token and content-type json
@Put('insertLocation')
@UseGuards(new AuthGuard())
......@@ -20,7 +20,7 @@ export class MapMarkersController {
}
}
// return all markers
// return all markers through service
@Get('getall')
async getAll(){
return this.mapmarkerservice.getAllMarkers();
......
......@@ -4,7 +4,7 @@ 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';
import { PersonEntity } from '../user/user.entity';
@Module({
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
import { UserService } from './user.service';
import { UserDTO } from './user.dto';
import { AuthGuard } from 'src/shared/auth.guard';
import { ValidationPipe } from 'src/shared/validation.pipe';
import { AuthGuard } from '../shared/auth.guard';
import { ValidationPipe } from '../shared/validation.pipe';
@Controller('user')
export class UserController {
......
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';
import { MapMarkerEntity } from '../mapmarkers/mapmarker.entity';
@Entity('Person')
export class PersonEntity {
......