Skip to content

Commit

Permalink
gh-112: Turn Game, GameData to interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
littlewhywhat committed Mar 20, 2020
1 parent d8bb46a commit f657e38
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
2 changes: 2 additions & 0 deletions backend/repositories/GameRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('GameRepository', () => {
})
describe('addGame', () => {
it.each([
['has not a game data', undefined, undefined],
['has nulls', null, null],
['has empty data', '', ''],
['has no description', FOOSBALL_DATA.name, ''],
['has no name', '', FOOSBALL_DATA.description],
Expand Down
17 changes: 10 additions & 7 deletions backend/repositories/GameRepository.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { GameData } from '../types/Game'
import { GameData, isGameData } from '../types/Game'
import { InputError } from '../errors/InputError'
import * as storage from '../storage/Storage'

const isValidGame = (gameData: GameData): boolean => {
return gameData.name &&
gameData.description &&
const isValidGameData = (gameData: GameData): boolean => {
return gameData.name !== '' &&
gameData.description !== '' &&
gameData.name.trim() == gameData.name &&
gameData.name.toLowerCase() == gameData.name &&
!gameData.name.includes(' ') &&
gameData.description.trim() == gameData.description
}

export const addGame = async (gameData: GameData): Promise<void> => {
if (!isValidGame(gameData)) {
export const addGame = async (data: unknown): Promise<void> => {
if (!isGameData(data)) {
throw new InputError('Data is not valid!')
}
if (!isValidGameData(data)) {
throw new InputError('Game data is not valid!')
}
storage.insertGame(gameData)
storage.insertGame(data)
}
10 changes: 5 additions & 5 deletions backend/storage/db/db-transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export const createMatchFromDbRow = (matchRow): MatchWithId => {
}
}

export const createGameFromDbRow = (gameRow): Game => new Game(
Number(gameRow.Id),
gameRow.Name,
gameRow.Description
)
export const createGameFromDbRow = (gameRow): Game => ({
id: Number(gameRow.Id),
name: gameRow.Name,
description: gameRow.Description,
})
22 changes: 11 additions & 11 deletions backend/types/Game.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export class Game {
constructor(
readonly id: number,
readonly name: string,
readonly description: string
) {}
export interface GameData {
readonly name: string;
readonly description: string;
}

export class GameData {
constructor(
readonly name: string,
readonly description: string
) {}
export const isGameData = (data: unknown): data is GameData => {
const test = data as GameData

This comment has been minimized.

Copy link
@novellizator

novellizator Mar 20, 2020

Contributor

this is inherently unsafe, since it gives you the illusion you can work with test as if it were GameData.
example: if game data is shaped like this {a: {b:{c: "somestring"}} and you test it via typeof a.b.c == "string"
then you might end up with a run time error (calling c of undefined)

This comment has been minimized.

Copy link
@novellizator

novellizator Mar 20, 2020

Contributor

discussed on slack, resovled

return test.name !== undefined && test.name !== null &&
test.description !== null && test.description !== null
}

export interface Game extends GameData {
readonly id: number;
}

0 comments on commit f657e38

Please sign in to comment.