init nest (wip)

This commit is contained in:
Anthony Mineo 2020-08-22 10:19:50 -04:00
parent 1a211e261e
commit 3b5f41f933
26 changed files with 8251 additions and 0 deletions

View file

@ -0,0 +1,22 @@
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!');
});
});
});

View file

@ -0,0 +1,12 @@
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();
}
}

33
app/api/src/app.module.ts Normal file
View file

@ -0,0 +1,33 @@
import * as Joi from 'joi';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { GamesModule } from './games/games.module';
@Module({
imports: [
ConfigModule.forRoot({
validationSchema: Joi.object({
DATABASE_HOST: Joi.required(),
DATABASE_PORT: Joi.number().default(5432)
})
}),
TypeOrmModule.forRoot({
type: 'postgres', // type of our database
host: process.env.DATABASE_HOST,
port: +process.env.DATABASE_PORT,
username: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
autoLoadEntities: true // models will be loaded automatically (you don't have to explicitly specify the entities: [] array)
}),
GamesModule
],
controllers: [ AppController ],
providers: [ AppService ]
})
export class AppModule {}

View file

@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

View file

@ -0,0 +1,11 @@
import { IsOptional, IsPositive } from 'class-validator';
export class PaginationQueryDto {
@IsOptional()
@IsPositive()
limit: number;
@IsOptional()
@IsPositive()
offset: number;
}

View file

@ -0,0 +1,62 @@
import { Column, Entity, Index, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { GameDetail } from '../games/entities/GameDetail';
@Index('players_pk', [ 'playerGuid' ], { unique: true })
@Index('players_player_name_key', [ 'playerName' ], { unique: true })
@Index('players_uuid_key', [ 'uuid' ], { unique: true })
@Entity('players', { schema: 'public' })
export class Players {
@PrimaryGeneratedColumn({ type: 'integer', name: 'id' })
id: number;
@Column('numeric', { primary: true, name: 'player_guid' })
playerGuid: string;
@Column('text', { name: 'player_name', unique: true })
playerName: string;
@Column('numeric', { name: 'total_games_ctfgame', default: () => '0' })
totalGamesCtfgame: string;
@Column('numeric', { name: 'total_games_dmgame', default: () => '0' })
totalGamesDmgame: string;
@Column('numeric', { name: 'total_games_lakrabbitgame', default: () => '0' })
totalGamesLakrabbitgame: string;
@Column('numeric', { name: 'total_games_sctfgame', default: () => '0' })
totalGamesSctfgame: string;
@Column('numeric', { name: 'stat_overwrite_ctfgame', default: () => '0' })
statOverwriteCtfgame: string;
@Column('numeric', { name: 'stat_overwrite_dmgame', default: () => '0' })
statOverwriteDmgame: string;
@Column('numeric', {
name: 'stat_overwrite_lakrabbitgame',
default: () => '0'
})
statOverwriteLakrabbitgame: string;
@Column('numeric', { name: 'stat_overwrite_sctfgame', default: () => '0' })
statOverwriteSctfgame: string;
@Column('text', { name: 'uuid', unique: true })
uuid: string;
@Column('timestamp with time zone', {
name: 'created_at',
default: () => 'now()'
})
createdAt: Date;
@Column('timestamp with time zone', {
name: 'updated_at',
default: () => 'now()'
})
updatedAt: Date;
@OneToMany(() => GameDetail, (gameDetail) => gameDetail.playerGuid)
gameDetails: GameDetail[];
}

View file

@ -0,0 +1,46 @@
import { Column, Entity, Index, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Games } from './Games';
import { Players } from '../../entities/Players';
@Index('games_pk', [ 'id' ], { unique: true })
@Index('game_detail_uuid_key', [ 'uuid' ], { unique: true })
@Entity('game_detail', { schema: 'public' })
export class GameDetail {
@PrimaryGeneratedColumn({ type: 'integer', name: 'id' })
id: number;
@Column('text', { name: 'player_name' })
playerName: string;
@Column('numeric', { name: 'stat_overwrite' })
statOverwrite: string;
@Column('text', { name: 'map' })
map: string;
@Column('jsonb', { name: 'stats' })
stats: any;
@Column('timestamp without time zone', { name: 'datestamp' })
datestamp: Date;
@Column('text', { name: 'uuid', unique: true })
uuid: string;
@Column('text', { name: 'gametype' })
gametype: string;
@Column('timestamp with time zone', {
name: 'created_at',
default: () => 'now()'
})
createdAt: Date;
// @ManyToOne(() => Games, (games) => games.gameDetails)
// @JoinColumn([ { name: 'game_id', referencedColumnName: 'gameId' } ])
// game: Games;
@ManyToOne(() => Players, (players) => players.gameDetails)
@JoinColumn([ { name: 'player_guid', referencedColumnName: 'playerGuid' } ])
playerGuid: Players;
}

View file

@ -0,0 +1,24 @@
import { Column, Entity, Index, OneToMany } from 'typeorm';
import { GameDetail } from './GameDetail';
@Index('game_pk', [ 'gameId' ], { unique: true })
@Entity('games', { schema: 'public' })
export class Games {
@Column('numeric', { primary: true, name: 'game_id' })
gameId: string;
@Column('text', { name: 'map' })
map: string;
@Column('timestamp without time zone', { name: 'datestamp' })
datestamp: Date;
@Column('text', { name: 'gametype' })
gametype: string;
@Column('timestamp with time zone', {
name: 'created_at',
default: () => 'now()'
})
createdAt: Date;
}

View file

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { GamesController } from './games.controller';
describe('Games Controller', () => {
let controller: GamesController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [GamesController],
}).compile();
controller = module.get<GamesController>(GamesController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View file

@ -0,0 +1,22 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { GamesService } from './games.service';
import { PaginationQueryDto } from '../common/dto/pagination-query.dto';
@Controller('games')
export class GamesController {
constructor(private readonly gameService: GamesService) {}
// /games
@Get()
findAll(@Query() paginationQuery: PaginationQueryDto) {
//const { limit, offset } = paginationQuery;
return this.gameService.findAll({ limit: 100, offset: 0 });
}
// /game/:gameId
@Get(':gameId')
findOne(@Param('gameId') gameId: string) {
return '';
}
}

View file

@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GamesController } from './games.controller';
import { GamesService } from './games.service';
import { Games } from './entities/Games';
@Module({
imports: [ TypeOrmModule.forFeature([ Games ]), ConfigModule ],
controllers: [ GamesController ],
providers: [ GamesService ]
})
export class GamesModule {}

View file

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { GamesService } from './games.service';
describe('GamesService', () => {
let service: GamesService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [GamesService],
}).compile();
service = module.get<GamesService>(GamesService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View file

@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Connection, Repository } from 'typeorm';
import { Games } from './entities/Games';
import { InjectRepository } from '@nestjs/typeorm';
import { PaginationQueryDto } from '../common/dto/pagination-query.dto';
@Injectable()
export class GamesService {
constructor(
private readonly connection: Connection,
private readonly configService: ConfigService,
@InjectRepository(Games) private readonly gamesRepository: Repository<Games>
) {}
async findAll(paginationQuery: PaginationQueryDto) {
const { limit, offset } = paginationQuery;
const games = await this.gamesRepository.find({
skip: offset,
take: limit
});
return games;
}
}

21
app/api/src/main.ts Normal file
View file

@ -0,0 +1,21 @@
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
transformOptions: {
enableImplicitConversion: true
}
})
);
await app.listen(3000);
}
bootstrap();