From 75dd763b3c96d17f7962178e13ee75ae21afdeb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Syd=C3=A4nmaa?= <L4072@student.jamk.fi> Date: Tue, 11 Jun 2019 15:26:17 +0300 Subject: [PATCH] Database edited. TypeORM --- src/app.module.ts | 3 ++- src/game/coordinate.entity.ts | 3 ++- src/game/game.controller.spec.ts | 18 ++++++++++++++++++ src/game/game.controller.ts | 12 ++++-------- src/game/game.dto.ts | 7 ++----- src/game/game.entity.ts | 12 ++++++++---- src/game/game.module.ts | 13 +++++++++++++ src/game/game.service.ts | 14 +++++++------- src/user/user.module.ts | 2 +- 9 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 src/game/game.controller.spec.ts create mode 100644 src/game/game.module.ts diff --git a/src/app.module.ts b/src/app.module.ts index 02a7b1b..30eed27 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -8,12 +8,13 @@ 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 { GameController } from './game/game.controller'; @Module({ imports: [TypeOrmModule.forRoot(), UserModule, MapMarkerModule], - controllers: [AppController], + controllers: [AppController, GameController], providers: [ AppService, { provide: APP_FILTER, diff --git a/src/game/coordinate.entity.ts b/src/game/coordinate.entity.ts index 02f2c7d..9a996e1 100644 --- a/src/game/coordinate.entity.ts +++ b/src/game/coordinate.entity.ts @@ -1,4 +1,4 @@ -import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, ManyToMany, OneToMany, ManyToOne, JoinTable } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, ManyToMany, OneToMany, ManyToOne, JoinTable, Timestamp } from 'typeorm'; import {Game_PersonEntity, ObjectivePointEntity, GameEntity} from './game.entity' import { FactionEntity } from './faction.entity' //import { MapEntity, MapDrawingEntity } from '../map/map.entity' @@ -8,6 +8,7 @@ export class CoordinateEntity { @PrimaryGeneratedColumn('uuid') coordinateId: string; @Column({type: 'geometry'}) longitude: string; @Column({type: 'geometry'}) latitude: string; + @Column() coordinateTimestamp: Timestamp; @ManyToMany(type => MapDrawingEntity, mapDrawings => mapDrawings.mapDrawings_coordinates) mapDrawings: MapDrawingEntity[]; diff --git a/src/game/game.controller.spec.ts b/src/game/game.controller.spec.ts new file mode 100644 index 0000000..23d9a2f --- /dev/null +++ b/src/game/game.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { GameController } from './game.controller'; + +describe('Game Controller', () => { + let controller: GameController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [GameController], + }).compile(); + + controller = module.get<GameController>(GameController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index 5e1a521..13ccb86 100644 --- a/src/game/game.controller.ts +++ b/src/game/game.controller.ts @@ -1,10 +1,6 @@ -import { Controller, Post, Body } from '@nestjs/common'; -import {CreateGameDTO} from './game.dto' +import { Controller } from '@nestjs/common'; @Controller('game') -export class GameController{ - @Post() - create(@Body() data: CreateGameDTO) { - return this//.gameService.register(data); - } -} \ No newline at end of file +export class GameController { + +} diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts index 64e062b..058479d 100644 --- a/src/game/game.dto.ts +++ b/src/game/game.dto.ts @@ -22,9 +22,6 @@ export class CreateGameDTO { @IsArray() @IsNotEmpty() factions?: FactionEntity[]; - @IsArray() @IsNotEmpty() - powerUps?: PowerUpDTO[]; - @IsArray() @IsNotEmpty() objectivePoint?: ObjectivePointDTO[]; @@ -44,7 +41,7 @@ export class FactionDTO { } -export class PowerUpDTO { +/*export class PowerUpDTO { @IsString() @IsNotEmpty() powerUpName: string; @@ -56,7 +53,7 @@ export class PowerUpDTO { @IsNotEmpty() cooldown: string; -} +}*/ export class ObjectivePointDTO { @IsString() @IsNotEmpty() diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts index 6f04c34..4a823eb 100644 --- a/src/game/game.entity.ts +++ b/src/game/game.entity.ts @@ -1,8 +1,8 @@ import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, ManyToMany, Timestamp, JoinTable } from 'typeorm'; +import * as bcrypt from 'bcryptjs'; import {PersonEntity, PersonRoleEntity} from '../user/user.entity' import {FactionEntity} from './faction.entity' -import {CoordinateEntity} from './coordinate.entity' -import { MapEntity } from './coordinate.entity' +import {CoordinateEntity, MapEntity} from './coordinate.entity' @Entity('Game') export class GameEntity { @@ -23,8 +23,12 @@ export class GameEntity { objective_points: ObjectivePointEntity[]; gameObject() { - const {gameId, gameName, gameDescription, startDate, endDate, GM_Password} = this; - return this; + const {gameId, gameName, gameDescription, startDate, endDate, factions, objective_points, map, GM_Password} = this; + return {gameId, gameName, gameDescription, startDate, endDate, factions, objective_points, map, GM_Password}; + } + + async comparePassword(attempt: string) { + return await bcrypt.compareSync(attempt, this.GM_Password); } } diff --git a/src/game/game.module.ts b/src/game/game.module.ts new file mode 100644 index 0000000..737305f --- /dev/null +++ b/src/game/game.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; + +import { GameController } from './game.controller'; +import { GameService } from './game.service'; +import { GameEntity } from './game.entity'; + +@Module({ + imports: [TypeOrmModule.forFeature([GameEntity])], + controllers: [GameController], + providers: [GameService] +}) +export class GameModule {} \ No newline at end of file diff --git a/src/game/game.service.ts b/src/game/game.service.ts index 9ea147d..3d91055 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -6,18 +6,18 @@ import { GameEntity } from './game.entity'; import { CreateGameDTO } from './game.dto'; @Injectable() -export class UserService { +export class GameService { constructor(@InjectRepository(GameEntity) private userRepository: Repository<GameEntity>){} async register(data: CreateGameDTO) { const { gameName } = data; - let user = await this.userRepository.findOne({where: {name}}); - if (user) { - throw new HttpException('User already exists', HttpStatus.BAD_REQUEST); + let game = await this.userRepository.findOne({where: {gameName}}); + if (game) { + throw new HttpException('Game with this name already exists', HttpStatus.BAD_REQUEST); } - user = await this.userRepository.create(data); - await this.userRepository.save(user); - return user.gameObject(); + game = await this.userRepository.create(data); + await this.userRepository.save(game); + return game.gameObject(); } /*async login(data: UserDTO) { diff --git a/src/user/user.module.ts b/src/user/user.module.ts index 4776b01..cd43a16 100644 --- a/src/user/user.module.ts +++ b/src/user/user.module.ts @@ -3,7 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { UserController } from './user.controller'; import { UserService } from './user.service'; -import { PersonEntity} from './user.entity'; +import { PersonEntity } from './user.entity'; @Module({ imports: [TypeOrmModule.forFeature([PersonEntity])], -- GitLab