Skip to content

Commit

Permalink
Create and get all in Game
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobrunodev committed Sep 20, 2019
1 parent 243e287 commit f3fa36f
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 34 deletions.
123 changes: 119 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"dependencies": {
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"@nestjs/mongoose": "^6.1.2",
"@nestjs/platform-express": "^6.0.0",
"mongoose": "^5.7.1",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"rxjs": "^6.3.3"
Expand Down
9 changes: 8 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { GameModule } from './game/game.module';

@Module({
imports: [GameModule],
imports: [
GameModule,
MongooseModule.forRoot(
'mongodb://root:root@localhost/collabstore?authSource=admin',
{ useNewUrlParser: true, useUnifiedTopology: true },
),
],
controllers: [AppController],
providers: [AppService],
})
Expand Down
40 changes: 12 additions & 28 deletions src/game/game.controller.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Body, Post } from '@nestjs/common';
import { Game } from './game.interface';
import { GameService } from './game.service';
import { CreateGameDto } from './game.dto';

@Controller('game')
export class GameController {
constructor(private readonly service: GameService) {}

@Get('overview')
getAllOverview(): Game[] {
return [
{
id: 123,
src: '/img/game.png',
alt: 'Imagem do Game',
title: 'Moonlighter',
price: 599.9,
stars: 3,
},
{
id: 123,
src: '/img/game.png',
alt: 'Imagem do Game',
title: 'Moonlighter',
price: 599.9,
stars: 3,
},
{
id: 123,
src: '/img/game.png',
alt: 'Imagem do Game',
title: 'Moonlighter',
price: 599.9,
stars: 3,
},
];
async getAllOverview(): Promise<Game[]> {
return await this.service.findAll();
}

@Post()
async create(@Body() createGameDto: CreateGameDto) {
return await this.service.create(createGameDto);
}
}
7 changes: 7 additions & 0 deletions src/game/game.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class CreateGameDto {
readonly src: string;
readonly alt: string;
readonly title: string;
readonly price: number;
readonly stars: number;
}
7 changes: 6 additions & 1 deletion src/game/game.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { GameController } from './game.controller';
import { GameService } from './game.service';
import GameSchema from './game.schema';

@Module({
controllers: [GameController]
imports: [MongooseModule.forFeature([{ name: 'Game', schema: GameSchema }])],
controllers: [GameController],
providers: [GameService],
})
export class GameModule {}
12 changes: 12 additions & 0 deletions src/game/game.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Schema } from 'mongoose';

const GameSchema = new Schema({
id: Number,
src: String,
alt: String,
title: String,
price: Number,
stars: Number,
});

export default GameSchema;
18 changes: 18 additions & 0 deletions src/game/game.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
19 changes: 19 additions & 0 deletions src/game/game.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Game } from './game.interface';
import { CreateGameDto } from './game.dto';

@Injectable()
export class GameService {
constructor(@InjectModel('Game') private readonly gameModel: Model<Game>) {}

async create(createGameDto: CreateGameDto): Promise<Game> {
const createGame = new this.gameModel(createGameDto);
return await createGame.save();
}

async findAll(): Promise<Game[]> {
return await this.gameModel.find().exec();
}
}

0 comments on commit f3fa36f

Please sign in to comment.