Skip to content
Snippets Groups Projects
Commit 4ac463d1 authored by L4168's avatar L4168
Browse files

basic game creation, added postgis to d-compose

parent d1be4eb1
No related branches found
No related tags found
3 merge requests!59Development to master,!10Gamecreation,!5basic game creation, added postgis to d-compose
...@@ -9,7 +9,7 @@ services: ...@@ -9,7 +9,7 @@ services:
ports: ports:
- 5000:5000 - 5000:5000
postgres: postgres:
image: postgres image: mdillon/postgis
volumes: volumes:
- /home/postgres:/var/lib/postgresql/data - /home/postgres:/var/lib/postgresql/data
ports: ports:
......
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!');
});
});
});
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();
}
}
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 { 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 { GameModule } from './game/game.module';
@Module({ @Module({
imports: [TypeOrmModule.forRoot(), UserModule, MapMarkerModule], imports: [TypeOrmModule.forRoot(), UserModule, MapMarkerModule, GameModule],
controllers: [AppController], controllers: [],
providers: [ providers: [
AppService, { AppService, {
provide: APP_FILTER, provide: APP_FILTER,
......
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
@Injectable() @Injectable()
export class AppService { export class AppService {}
getHello(): string {
return 'Hello World!';
}
}
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();
});
});
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();
}
}
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;
}
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;
}
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 {}
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();
});
});
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 };
});
}
}
...@@ -17,6 +17,6 @@ async function bootstrap() { ...@@ -17,6 +17,6 @@ async function bootstrap() {
app.enableCors(); app.enableCors();
// apply limiter to all routes // apply limiter to all routes
app.use(limiter); app.use(limiter);
await app.listen(5000); await app.listen(5001);
} }
bootstrap(); bootstrap();
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