From af3a9e5ce249950ebd9d54c9b5521c9bd0aaab8c Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 9 Jan 2021 11:15:29 +0100 Subject: [PATCH] Added basic response calss for runner cards ref #77 --- src/models/entities/RunnerCard.ts | 3 +- src/models/responses/ResponseRunnerCard.ts | 47 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/models/responses/ResponseRunnerCard.ts diff --git a/src/models/entities/RunnerCard.ts b/src/models/entities/RunnerCard.ts index 4ea40e2..25ce61c 100644 --- a/src/models/entities/RunnerCard.ts +++ b/src/models/entities/RunnerCard.ts @@ -7,6 +7,7 @@ import { IsString } from "class-validator"; import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; +import { ResponseRunnerCard } from '../responses/ResponseRunnerCard'; import { Runner } from "./Runner"; import { TrackScan } from "./TrackScan"; @@ -62,6 +63,6 @@ export class RunnerCard { * Turns this entity into it's response class. */ public toResponse() { - return new Error("NotImplemented"); + return new ResponseRunnerCard(this); } } diff --git a/src/models/responses/ResponseRunnerCard.ts b/src/models/responses/ResponseRunnerCard.ts new file mode 100644 index 0000000..94c7148 --- /dev/null +++ b/src/models/responses/ResponseRunnerCard.ts @@ -0,0 +1,47 @@ +import { IsBoolean, IsEAN, IsInt, IsNotEmpty, IsObject, IsString } from "class-validator"; +import { RunnerCard } from '../entities/RunnerCard'; +import { ResponseRunner } from './ResponseRunner'; + +/** + * Defines the runner card response. +*/ +export class ResponseRunnerCard { + /** + * The card's id. + */ + @IsInt() + id: number;; + + /** + * The card's associated runner. + * This is important to link scans to runners. + */ + @IsObject() + runner: ResponseRunner; + + /** + * The card's code. + */ + @IsEAN() + @IsString() + @IsNotEmpty() + code: string; + + /** + * Is the enabled valid (for fraud reasons). + * The determination of validity will work differently for every child class. + */ + @IsBoolean() + enabled: boolean = true; + + /** + * Creates a ResponseRunnerCard object from a runner card. + * @param card The card the response shall be build for. + */ + public constructor(card: RunnerCard) { + this.id = card.id; + this.runner = card.runner.toResponse() || null; + this.code = card.code; + this.enabled = card.enabled; + } +}