From cdddb9a073ecc42938136427a4c378945b7742db Mon Sep 17 00:00:00 2001 From: legonzaur Date: Mon, 30 Dec 2024 00:39:06 +0100 Subject: [PATCH] fix : unify dto conversion --- dto/conceptinturnresponse.dto.ts | 4 +++- dto/conceptresponse.dto.ts | 3 ++- dto/gameresponse.dto.ts | 27 ++++++++++++++++++--------- dto/playerresponse.dto.ts | 2 +- dto/userresponse.dto.ts | 4 +++- game.events.ts | 8 ++++++-- lobby.events.ts | 19 +++++++++++++++---- word.events.ts | 8 ++++++-- 8 files changed, 54 insertions(+), 21 deletions(-) diff --git a/dto/conceptinturnresponse.dto.ts b/dto/conceptinturnresponse.dto.ts index bfef872..bdd464a 100644 --- a/dto/conceptinturnresponse.dto.ts +++ b/dto/conceptinturnresponse.dto.ts @@ -2,7 +2,9 @@ import { ConceptResponseDTO } from './conceptresponse.dto'; export class ConceptInTurnResponseDTO { constructor(partial: Partial) { - Object.assign(this, partial); + this.order = partial.order; + this.subconcept = partial.subconcept; + this.markers = partial.markers; this.concept = new ConceptResponseDTO(partial.concept); } diff --git a/dto/conceptresponse.dto.ts b/dto/conceptresponse.dto.ts index b3cd7d6..d200479 100644 --- a/dto/conceptresponse.dto.ts +++ b/dto/conceptresponse.dto.ts @@ -1,6 +1,7 @@ export class ConceptResponseDTO { constructor(partial: Partial) { - Object.assign(this, partial); + this.id = partial.id; + this.value = partial.value; } id: number; diff --git a/dto/gameresponse.dto.ts b/dto/gameresponse.dto.ts index 19a44ed..ff92aee 100644 --- a/dto/gameresponse.dto.ts +++ b/dto/gameresponse.dto.ts @@ -4,24 +4,33 @@ import { ConceptInTurnResponseDTO } from './conceptinturnresponse.dto'; export class GameResponseDTO { constructor(partial: Partial) { - Object.assign(this, partial); + this.id = partial.id; + this.started = partial.started; this.owner = new UserResponseDTO(partial.owner); - this.players = partial.players.map((p) => new PlayerResponseDTO(p)); - this.currentPlayer = new PlayerResponseDTO(partial.currentPlayer); - this.currentConcepts = partial.currentConcepts.map( - (c) => new ConceptInTurnResponseDTO(c), - ); + if (partial.players) { + this.players = partial.players.map((p) => new PlayerResponseDTO(p)); + } + + if (partial.currentConcepts) { + this.currentConcepts = partial.currentConcepts.map( + (c) => new ConceptInTurnResponseDTO(c), + ); + } + + if (partial.currentPlayer) { + this.currentPlayer = new PlayerResponseDTO(partial.currentPlayer); + } } id: number; owner: UserResponseDTO; - players: PlayerResponseDTO[]; + players: PlayerResponseDTO[] = []; - currentPlayer: PlayerResponseDTO; + currentPlayer?: PlayerResponseDTO; started: boolean; - currentConcepts: ConceptInTurnResponseDTO[]; + currentConcepts: ConceptInTurnResponseDTO[] = []; } diff --git a/dto/playerresponse.dto.ts b/dto/playerresponse.dto.ts index a67cf44..fdebc6b 100644 --- a/dto/playerresponse.dto.ts +++ b/dto/playerresponse.dto.ts @@ -2,7 +2,7 @@ import { UserResponseDTO } from './userresponse.dto'; export class PlayerResponseDTO { constructor(partial: Partial) { - Object.assign(this, partial); + this.score = partial.score; this.user = new UserResponseDTO(partial.user); } diff --git a/dto/userresponse.dto.ts b/dto/userresponse.dto.ts index a294a7b..fed8c7b 100644 --- a/dto/userresponse.dto.ts +++ b/dto/userresponse.dto.ts @@ -1,6 +1,8 @@ export class UserResponseDTO { constructor(partial: Partial) { - Object.assign(this, partial); + this.id = partial.id; + this.username = partial.username; + this.avatar = partial.avatar; } id: string; diff --git a/game.events.ts b/game.events.ts index 28d8c57..fd2561d 100644 --- a/game.events.ts +++ b/game.events.ts @@ -10,11 +10,15 @@ export class GameDeleteEvent { export class GameJoinEvent { type = 'joingame'; - constructor(public player: PlayerResponseDTO) {} + constructor(public player: PlayerResponseDTO) { + this.player = new PlayerResponseDTO(player); + } } export class GameLeaveEvent { type = 'leavegame'; gameId?: number; - constructor(public player: PlayerResponseDTO) {} + constructor(public player: PlayerResponseDTO) { + this.player = new PlayerResponseDTO(player); + } } diff --git a/lobby.events.ts b/lobby.events.ts index 30af269..ee81cea 100644 --- a/lobby.events.ts +++ b/lobby.events.ts @@ -3,12 +3,17 @@ import { PlayerResponseDTO } from './dto/playerresponse.dto'; export class LobbyCreateEvent { type = 'create'; - constructor(public game: GameResponseDTO) {} + game: GameResponseDTO; + constructor(game: GameResponseDTO) { + this.game = new GameResponseDTO(game); + } } export class LobbyStartEvent { type = 'start'; - constructor(public game: GameResponseDTO) {} + constructor(public game: GameResponseDTO) { + this.game = new GameResponseDTO(game); + } } export class LobbyDeleteEvent { @@ -21,7 +26,10 @@ export class LobbyJoinEvent { constructor( public player: PlayerResponseDTO, public game: GameResponseDTO, - ) {} + ) { + this.player = new PlayerResponseDTO(player); + this.game = new GameResponseDTO(game); + } } export class LobbyLeaveEvent { @@ -30,5 +38,8 @@ export class LobbyLeaveEvent { constructor( public player: PlayerResponseDTO, public game: GameResponseDTO, - ) {} + ) { + this.player = new PlayerResponseDTO(player); + this.game = new GameResponseDTO(game); + } } diff --git a/word.events.ts b/word.events.ts index 2c09cd7..fb9a588 100644 --- a/word.events.ts +++ b/word.events.ts @@ -2,12 +2,16 @@ import { WordResponseDTO } from './dto/wordresponse.dto'; export class WordCreateEvent { type = 'create'; - constructor(public word: WordResponseDTO) {} + constructor(public word: WordResponseDTO) { + this.word = new WordResponseDTO(word); + } } export class WordEditEvent { type = 'edit'; - constructor(public word: WordResponseDTO) {} + constructor(public word: WordResponseDTO) { + this.word = new WordResponseDTO(word); + } } export class WordDeleteEvent {