diff --git a/backend/repositories/GameRepository.test.ts b/backend/repositories/GameRepository.test.ts index baf8080c..98ca5d7d 100644 --- a/backend/repositories/GameRepository.test.ts +++ b/backend/repositories/GameRepository.test.ts @@ -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], diff --git a/backend/repositories/GameRepository.ts b/backend/repositories/GameRepository.ts index 998cd6de..0fbd2ce1 100644 --- a/backend/repositories/GameRepository.ts +++ b/backend/repositories/GameRepository.ts @@ -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 => { - if (!isValidGame(gameData)) { +export const addGame = async (data: unknown): Promise => { + 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) } diff --git a/backend/storage/db/db-transformations.ts b/backend/storage/db/db-transformations.ts index 3a5e70f8..1f6a0928 100644 --- a/backend/storage/db/db-transformations.ts +++ b/backend/storage/db/db-transformations.ts @@ -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, +}) diff --git a/backend/types/Game.ts b/backend/types/Game.ts index dfa9479a..9b2027bf 100644 --- a/backend/types/Game.ts +++ b/backend/types/Game.ts @@ -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 + return test.name !== undefined && test.name !== null && + test.description !== null && test.description !== null +} + +export interface Game extends GameData { + readonly id: number; }