Skip to content

Commit

Permalink
Merge pull request #65 from ClaudioMartinH/60-gameservice-gamecontrol…
Browse files Browse the repository at this point in the history
…ler-testing

testing done with some issues
  • Loading branch information
ClaudioMartinH authored Sep 8, 2024
2 parents a49f12b + 758b3ab commit 05e6395
Show file tree
Hide file tree
Showing 15 changed files with 1,026 additions and 231 deletions.
11 changes: 2 additions & 9 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
"@types/express": "^4.17.21",
"@types/node": "^20.14.11",
"nodemon": "^3.1.4",
"prisma": "^5.19.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.3",
"vitest": "^2.0.5"
},
"dependencies": {
"@prisma/client": "^5.19.1",
"express": "^4.19.2"
"express": "^4.19.2",
"prisma": "^5.19.1"
}
}
}
Binary file modified prisma/dev.db
Binary file not shown.
28 changes: 0 additions & 28 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,3 @@ model Ranking {
winPercentage Float @default(0.0)
player Player @relation(fields: [playerId], references: [id])
}

// generator client {
// provider = "prisma-client-js"
// }

// datasource db {
// provider = "mysql"
// url = env("DATABASE_URL")
// }

// model Player {
// id Int @id @default(autoincrement())
// name String @unique
// register_date DateTime @default(now())
// games Game[]
// password String

// }

// model Game {
// id Int @id @default(autoincrement())
// playerId Int
// player Player @relation(fields: [playerId], references: [id])
// dice1_result Int
// dice2_result Int
// overall_result String
// played_at DateTime @default(now())
// }
44 changes: 38 additions & 6 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,64 @@
const { PrismaClient } = require("@prisma/client");
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

async function main() {
const player = await prisma.player.create({
await prisma.ranking.deleteMany({});
await prisma.game.deleteMany({});
await prisma.player.deleteMany({});

const player1 = await prisma.player.create({
data: {
name: "Andrea",
password: "securepassword",
},
});
const player2 = await prisma.player.create({
data: {
name: "Claudio",
},
});
const game = await prisma.game.create({
data: {
playerId: player.id,
playerId: player1.id,
dice1Result: 4,
dice2Result: 3,
overallResult: "win",
},
});
const game2 = await prisma.game.create({
data: {
playerId: player2.id,
dice1Result: 6,
dice2Result: 3,
overallResult: "loss",
},
});
const ranking = await prisma.ranking.create({
data: {
playerId: player.id,
playerId: player1.id,
totalGames: 1,
totalWins: 1,
totalLost: 0,
winPercentage: 100.0,
},
});
const ranking2 = await prisma.ranking.create({
data: {
playerId: player2.id,
totalGames: 1,
totalWins: 0,
totalLost: 1,
winPercentage: 0.0,
},
});

console.log("Datos insertados correctamente:", { player, game, ranking });
console.log("Datos insertados correctamente:", {
player1,
player2,
game,
game2,
ranking,
ranking2,
});
}

main()
Expand Down
51 changes: 22 additions & 29 deletions src/domain/entities/Game.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
import { Player } from "./Player.js";

export class Game {
id: Number;
playerId: Number;
gameId: Number;
dice1Result: Number;
dice2Result: Number;
overallResult: String;
player?: Player;
game?: Game;

constructor(
id: Number,
playerId: Number,
gameId: Number,
dice1Result: Number,
dice2Result: Number,
overallResult: String,
player?: Player,
game?: Game
) {
this.id = id;
this.playerId = playerId;
this.gameId = gameId;
this.dice1Result = dice1Result;
this.dice2Result = dice2Result;
this.overallResult = overallResult;
this.player = player;
this.game = game;
}
id: Number;
playerId: Number;
dice1Result: Number;
dice2Result: Number;
overallResult: String;
player?: Player;

constructor(
id: Number,
playerId: Number,
dice1Result: Number,
dice2Result: Number,
overallResult: String,
player?: Player
) {
this.id = id;
this.playerId = playerId;
this.dice1Result = dice1Result;
this.dice2Result = dice2Result;
this.overallResult = overallResult;
this.player = player;
}
}
85 changes: 45 additions & 40 deletions src/infrastructure/controllers/gameController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,53 @@ import { GameService } from "../services/gameService.js";
const gameService = new GameService();

export class GameController {
async getGamesForPlayer(req: Request, res: Response) {
const { id } = req.params;
const playerID = parseInt(id);
try {
const games = await gameService.getGamesForPlayer(playerID);
if (!games) {
return res.status(404).json({ message: "Games for player not found" });
}
return res.status(200).json(games);
} catch (error: any) {
return res.status(500).json({ error: error.message });
}
async getGamesForPlayer(req: Request, res: Response) {
const { id } = req.params;
const playerID = parseInt(id);
try {
const games = await gameService.getGamesForPlayer(playerID);
if (!games || games.length === 0) {
//si devuelve array vacio se considera truthy y pasa el test
return res.status(404).json({ message: "Games for player not found" });
}
return res.status(200).json(games);
} catch (error: any) {
return res.status(500).json({ error: error.message });
}
}

async createGame(req: Request, res: Response) {
const { playerId, dice1Result, dice2Result, overallResult } = req.body;
if (!playerId || !dice1Result || !dice2Result || !overallResult) {
return res
.status(400)
.json({ message: "Missing required fields: playerId, dice1Result, dice2Result, overallResult" });
}
try {
const newGame = await gameService.createGame({
playerId, dice1Result, dice2Result, overallResult
});
return res.status(201).json(newGame);
} catch (error: any) {
return res.status(500).json({ error: error.message });
}
async createGame(req: Request, res: Response) {
const { playerId, dice1Result, dice2Result, overallResult } = req.body;
if (!playerId || !dice1Result || !dice2Result || !overallResult) {
return res.status(400).json({
message:
"Missing required fields: playerId, dice1Result, dice2Result, overallResult",
});
}
try {
const newGame = await gameService.createGame({
playerId,
dice1Result,
dice2Result,
overallResult,
});
return res.status(201).json(newGame);
} catch (error: any) {
return res.status(500).json({ error: error.message });
}
}

async deleteGames(req: Request, res: Response) {
const { id } = req.params;
const playerID = parseInt(id);
try {
const games = await gameService.deleteGames(playerID);
if (!games) {
return res.status(404).json({ message: "Games for player not found" });
}
return res.status(200).json(games);
} catch (error: any) {
return res.status(500).json({ error: error.message });
}
async deleteGames(req: Request, res: Response) {
const { id } = req.params;
const playerID = parseInt(id);
try {
const games = await gameService.deleteGames(playerID);
if (!games) {
return res.status(404).json({ message: "Games for player not found" });
}
return res.status(204).json(games);
} catch (error: any) {
return res.status(500).json({ error: error.message });
}
}
}
}
Loading

0 comments on commit 05e6395

Please sign in to comment.