-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(dto): better structure and composition for dto (#51)
- Loading branch information
1 parent
0afb1ad
commit 3bc3b6e
Showing
66 changed files
with
11,283 additions
and
6,367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/modules/game/dto/base/decorators/composition-bounds.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { applyDecorators } from "@nestjs/common"; | ||
import { ArrayMaxSize, ArrayMinSize } from "class-validator"; | ||
import { gameFieldsSpecs } from "../../../constants/game.constant"; | ||
|
||
function CompositionBounds(): <TFunction extends () => void, Y>(target: (TFunction | object), propertyKey?: (string | symbol), descriptor?: TypedPropertyDescriptor<Y>) => void { | ||
return applyDecorators( | ||
ArrayMinSize(gameFieldsSpecs.players.minItems), | ||
ArrayMaxSize(gameFieldsSpecs.players.maxItems), | ||
); | ||
} | ||
|
||
export { CompositionBounds }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/modules/game/dto/base/decorators/composition-unique-names.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { applyDecorators } from "@nestjs/common"; | ||
import { ArrayUnique } from "class-validator"; | ||
import isObject from "isobject"; | ||
import { has } from "lodash"; | ||
|
||
function getPlayerName(value?: unknown): unknown { | ||
if (!isObject(value) || !has(value, "name")) { | ||
return value; | ||
} | ||
return (value as { name: unknown }).name; | ||
} | ||
|
||
function CompositionUniqueNames(): <TFunction extends () => void, Y>( | ||
target: (TFunction | object), | ||
propertyKey?: (string | symbol), | ||
descriptor?: TypedPropertyDescriptor<Y> | ||
) => void { | ||
return applyDecorators(ArrayUnique(getPlayerName, { message: "players.name must be unique" })); | ||
} | ||
|
||
export { getPlayerName, CompositionUniqueNames }; |
25 changes: 25 additions & 0 deletions
25
src/modules/game/dto/base/game-player/game-player-role/game-player-role.base.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
import { IsBoolean, IsEnum } from "class-validator"; | ||
import { ROLE_NAMES } from "../../../../../role/enums/role.enum"; | ||
import { playerApiProperties } from "../../../../schemas/player/constants/player.constant"; | ||
import { playerRoleApiProperties } from "../../../../schemas/player/schemas/player-role/constants/player-role.constant"; | ||
|
||
class GamePlayerRoleBaseDto { | ||
@ApiProperty(playerApiProperties.role) | ||
@IsEnum(ROLE_NAMES) | ||
public name: ROLE_NAMES; | ||
|
||
@ApiProperty(playerRoleApiProperties.original) | ||
@IsEnum(ROLE_NAMES) | ||
public original: ROLE_NAMES; | ||
|
||
@ApiProperty(playerRoleApiProperties.current) | ||
@IsEnum(ROLE_NAMES) | ||
public current: ROLE_NAMES; | ||
|
||
@ApiProperty(playerRoleApiProperties.isRevealed) | ||
@IsBoolean() | ||
public isRevealed: boolean; | ||
} | ||
|
||
export { GamePlayerRoleBaseDto }; |
16 changes: 16 additions & 0 deletions
16
src/modules/game/dto/base/game-player/game-player-side/game-player-side.base.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
import { IsEnum } from "class-validator"; | ||
import { ROLE_SIDES } from "../../../../../role/enums/role.enum"; | ||
import { playerSideApiProperties } from "../../../../schemas/player/schemas/player-side/constants/player-side.constant"; | ||
|
||
class GamePlayerSideBaseDto { | ||
@ApiProperty(playerSideApiProperties.original) | ||
@IsEnum(ROLE_SIDES) | ||
public original: ROLE_SIDES; | ||
|
||
@ApiProperty(playerSideApiProperties.current) | ||
@IsEnum(ROLE_SIDES) | ||
public current: ROLE_SIDES; | ||
} | ||
|
||
export { GamePlayerSideBaseDto }; |
35 changes: 35 additions & 0 deletions
35
src/modules/game/dto/base/game-player/game-player.base.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
import { Transform, Type } from "class-transformer"; | ||
import { IsInt, IsString, MaxLength, Min, MinLength, ValidateNested } from "class-validator"; | ||
import { playerApiProperties, playersFieldsSpecs } from "../../../schemas/player/constants/player.constant"; | ||
import { GamePlayerRoleBaseDto } from "./game-player-role/game-player-role.base.dto"; | ||
import { GamePlayerSideBaseDto } from "./game-player-side/game-player-side.base.dto"; | ||
import { playerRoleTransformer } from "./transformers/player-role.transformer"; | ||
import { playerSideTransformer } from "./transformers/player-side.transformer"; | ||
|
||
class GamePlayerBaseDto { | ||
@ApiProperty(playerApiProperties.name) | ||
@IsString() | ||
@MinLength(playersFieldsSpecs.name.minLength) | ||
@MaxLength(playersFieldsSpecs.name.maxLength) | ||
public name: string; | ||
|
||
@ApiProperty(playerApiProperties.role) | ||
@Transform(playerRoleTransformer) | ||
@Type(() => GamePlayerRoleBaseDto) | ||
@ValidateNested() | ||
public role: GamePlayerRoleBaseDto; | ||
|
||
@ApiProperty(playerApiProperties.role) | ||
@Transform(playerSideTransformer) | ||
@Type(() => GamePlayerRoleBaseDto) | ||
@ValidateNested() | ||
public side: GamePlayerSideBaseDto; | ||
|
||
@ApiProperty(playerApiProperties.position) | ||
@IsInt() | ||
@Min(playersFieldsSpecs.position.minimum) | ||
public position: number; | ||
} | ||
|
||
export { GamePlayerBaseDto }; |
26 changes: 26 additions & 0 deletions
26
src/modules/game/dto/base/game-player/transformers/player-role.transformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { TransformFnParams } from "class-transformer/types/interfaces"; | ||
import { has } from "lodash"; | ||
import { roles } from "../../../../../role/constants/role.constant"; | ||
import { ROLE_NAMES } from "../../../../../role/enums/role.enum"; | ||
|
||
function playerRoleTransformer(params: TransformFnParams): unknown { | ||
if (!has(params.value as object, "name")) { | ||
return params.value; | ||
} | ||
const value = params.value as { | ||
name: string; | ||
current: ROLE_NAMES; | ||
original: ROLE_NAMES; | ||
isRevealed: boolean; | ||
}; | ||
const role = roles.find(({ name }) => name === value.name); | ||
if (role === undefined) { | ||
return value; | ||
} | ||
value.current = role.name; | ||
value.original = role.name; | ||
value.isRevealed = role.name === ROLE_NAMES.VILLAGER_VILLAGER; | ||
return value; | ||
} | ||
|
||
export { playerRoleTransformer }; |
25 changes: 25 additions & 0 deletions
25
src/modules/game/dto/base/game-player/transformers/player-side.transformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { TransformFnParams } from "class-transformer/types/interfaces"; | ||
import isObject from "isobject"; | ||
import { has } from "lodash"; | ||
import { roles } from "../../../../../role/constants/role.constant"; | ||
import type { ROLE_SIDES, ROLE_NAMES } from "../../../../../role/enums/role.enum"; | ||
|
||
function playerSideTransformer(params: TransformFnParams): unknown { | ||
if (!isObject(params.value) || !isObject(params.obj) || !has(params.obj as object, ["role", "name"])) { | ||
return params.value; | ||
} | ||
const obj = params.obj as { role: { name: ROLE_NAMES } }; | ||
const value = params.value as { | ||
current: ROLE_SIDES; | ||
original: ROLE_SIDES; | ||
}; | ||
const role = roles.find(({ name }) => name === obj.role.name); | ||
if (role === undefined) { | ||
return value; | ||
} | ||
value.current = role.side; | ||
value.original = role.side; | ||
return params.value; | ||
} | ||
|
||
export { playerSideTransformer }; |
Oops, something went wrong.