diff --git a/docker-compose.yml b/docker-compose.yml index 76cc44e952657f0262e2f90c7264effb581e57cd..7ac4913cbab54c870edd0ab853747d763eb56edd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,7 @@ services: ports: - 5000:5000 postgres: - image: postgres + image: mdillon/postgis volumes: - /home/postgres:/var/lib/postgresql/data ports: diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts deleted file mode 100644 index d22f3890a380cea30641cfecc329b5c794ef5fb2..0000000000000000000000000000000000000000 --- a/src/app.controller.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; - -describe('AppController', () => { - let appController: AppController; - - beforeEach(async () => { - const app: TestingModule = await Test.createTestingModule({ - controllers: [AppController], - providers: [AppService], - }).compile(); - - appController = app.get<AppController>(AppController); - }); - - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); -}); diff --git a/src/app.controller.ts b/src/app.controller.ts deleted file mode 100644 index cce879ee622146012901c9adb47ef40c0fd3a555..0000000000000000000000000000000000000000 --- a/src/app.controller.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Controller, Get } from '@nestjs/common'; -import { AppService } from './app.service'; - -@Controller() -export class AppController { - constructor(private readonly appService: AppService) {} - - @Get() - getHello(): string { - return this.appService.getHello(); - } -} diff --git a/src/app.module.ts b/src/app.module.ts index 02a7b1b23665dbd586cb72eed6b44cfec859127d..13f94c762487b48e7e763a23bf9ad20ad006abc2 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,19 +1,19 @@ import { Module } from '@nestjs/common'; import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core'; import { TypeOrmModule } from "@nestjs/typeorm"; -import { AppController } from './app.controller'; import { AppService } from './app.service'; 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 { GameModule } from './game/game.module'; @Module({ - imports: [TypeOrmModule.forRoot(), UserModule, MapMarkerModule], - controllers: [AppController], + imports: [TypeOrmModule.forRoot(), UserModule, MapMarkerModule, GameModule], + controllers: [], providers: [ AppService, { provide: APP_FILTER, diff --git a/src/app.service.ts b/src/app.service.ts index 927d7cca0badb13577152bf8753ce3552358f53b..7263d33a2a66e48c0b40af50ada82d18b0d376ae 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -1,8 +1,4 @@ import { Injectable } from '@nestjs/common'; @Injectable() -export class AppService { - getHello(): string { - return 'Hello World!'; - } -} +export class AppService {} diff --git a/src/game/game.controller.spec.ts b/src/game/game.controller.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..23d9a2f9c849e6b63b31ac707abca1cef9874012 --- /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 new file mode 100644 index 0000000000000000000000000000000000000000..3294a0b76414226d5d95438fb7a3befd3049b770 --- /dev/null +++ b/src/game/game.controller.ts @@ -0,0 +1,24 @@ +import { Controller, Post, UseGuards, Body, Get } from '@nestjs/common'; +import { GameService } from './game.service'; +import { AuthGuard } from 'dist/shared/auth.guard'; +import { User } from 'src/user/user.decorator'; +import { GameDTO, FactionDTO } from './game.dto'; + +@Controller('game') +export class GameController { + constructor(private gameservice:GameService) {} + + @Post('new') + @UseGuards(new AuthGuard()) + async newGame(@User('id') person, @Body() body) { + //@Body() game: GameDTO, @Body() factions: FactionDTO[] + //return body; + return this.gameservice.createNewGame(person, body, body.factions); + //game: GameDTO, @Body() factions: FactionDTO[] + } + + @Get('listgames') + async listGames() { + return this.gameservice.listGames(); + } +} diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..3f7bed0c56711222809ffcd41b00fc31895e583f --- /dev/null +++ b/src/game/game.dto.ts @@ -0,0 +1,35 @@ +import { + IsString, + IsDateString, + IsJSON, + IsNotEmpty, + Length, +} from 'class-validator'; + +export class GameDTO { + @IsString() + @IsNotEmpty() + @Length(2, 31) + name: string; + @IsString() + @IsNotEmpty() + @Length(1, 255) + desc: string; + @IsJSON() + map: JSON; + @IsDateString() + @IsNotEmpty() + startdate: string; + @IsDateString() + @IsNotEmpty() + enddate: string; +} + +export class FactionDTO { + @IsString() + @IsNotEmpty() + @Length(2, 15) + name: string; + id: string; + game: GameDTO; +} diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts new file mode 100644 index 0000000000000000000000000000000000000000..a79b61b8b437a5ce402d010cbd736578c27aa4dc --- /dev/null +++ b/src/game/game.entity.ts @@ -0,0 +1,31 @@ +import { + Entity, + Column, + PrimaryGeneratedColumn, + ManyToOne, + OneToMany, + Timestamp, +} from 'typeorm'; + + +// table that stores all created games +@Entity('Game') +export class GameEntity { + @PrimaryGeneratedColumn('uuid') id: string; + @Column('text') name: string; + @Column('text') desc: string; + @Column('json') map: JSON; + @Column('timestamp') startdate: Timestamp; + @Column('timestamp') enddate: Timestamp; + @OneToMany(type => FactionEntity, faction => faction.game) + factions: FactionEntity[]; +} + +// table that stores all factions created for games +@Entity('Faction') +export class FactionEntity { + @PrimaryGeneratedColumn('uuid') id: string; + @Column('text') name: string; + @ManyToOne(type => GameEntity, game => game.factions) + game: GameEntity; +} diff --git a/src/game/game.module.ts b/src/game/game.module.ts new file mode 100644 index 0000000000000000000000000000000000000000..b24a8293cd4b13e7820adc8b280c6740cf2f6c1b --- /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, FactionEntity } from './game.entity'; + +@Module({ + imports: [TypeOrmModule.forFeature([GameEntity, FactionEntity])], + controllers: [GameController], + providers: [GameService] +}) +export class GameModule {} diff --git a/src/game/game.service.spec.ts b/src/game/game.service.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..f4a1db7e70bf2a0e38c6d430c95e54feb3934fdf --- /dev/null +++ b/src/game/game.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { GameService } from './game.service'; + +describe('GameService', () => { + let service: GameService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [GameService], + }).compile(); + + service = module.get<GameService>(GameService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/game/game.service.ts b/src/game/game.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..9c8ff8856dcd68c5102d13d6443e1c2f8804a907 --- /dev/null +++ b/src/game/game.service.ts @@ -0,0 +1,49 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { GameEntity, FactionEntity } from './game.entity'; +import { FactionDTO, GameDTO } from './game.dto'; + +@Injectable() +export class GameService { + constructor( + @InjectRepository(GameEntity) + private gameRepository: Repository<GameEntity>, + @InjectRepository(FactionEntity) + private factionRepository: Repository<FactionEntity>, + ) {} + + // create a new game + async createNewGame( + personId: string, + gameData: GameDTO, + factions: FactionDTO[], + ) { + //Logger.log(gameData); + //Logger.log(factions); + const game = await this.gameRepository.create({ + ...gameData, + factions: factions, + }); + await this.gameRepository.insert(game); + + const gameid = await this.gameRepository.findOne({ where: { name: gameData.name } }) + + factions.map(async faction => { + let name = await this.factionRepository.create({ + ...faction, + game: gameid + }); + await this.factionRepository.insert(name); + }); + return 'success'; +} + + async listGames() { + //return await this.gameRepository.find(); + const games = await this.gameRepository.find({ relations: ['factions'] }); + return games.map(game => { + return { ...game, factions: game.factions }; + }); + } +} diff --git a/src/main.ts b/src/main.ts index 3c7cc4607bac085ff529775a441ef81e4e089a6a..3f8503099e79675f3fbf5a0c8bdadd265807806f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,6 +17,6 @@ async function bootstrap() { app.enableCors(); // apply limiter to all routes app.use(limiter); - await app.listen(5000); + await app.listen(5001); } bootstrap();