From a437ada3b3cee64c69548cf0cd1e93d4fa2495b3 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 4 Dec 2020 21:38:28 +0100 Subject: [PATCH] Runnerteams now with resolving relations and response types :O ref #13 --- src/controllers/RunnerTeamController.ts | 36 +++++++++----- src/models/responses/ResponseRunnerGroup.ts | 55 +++++++++++++++++++++ src/models/responses/ResponseRunnerTeam.ts | 26 ++++++++++ 3 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 src/models/responses/ResponseRunnerGroup.ts create mode 100644 src/models/responses/ResponseRunnerTeam.ts diff --git a/src/controllers/RunnerTeamController.ts b/src/controllers/RunnerTeamController.ts index 37b4c9d..e07931a 100644 --- a/src/controllers/RunnerTeamController.ts +++ b/src/controllers/RunnerTeamController.ts @@ -6,6 +6,7 @@ import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNot import { CreateRunnerTeam } from '../models/creation/CreateRunnerTeam'; import { Runner } from '../models/entities/Runner'; import { RunnerTeam } from '../models/entities/RunnerTeam'; +import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam'; import { RunnerController } from './RunnerController'; @@ -22,23 +23,29 @@ export class RunnerTeamController { } @Get() - @ResponseSchema(RunnerTeam, { isArray: true }) + @ResponseSchema(ResponseRunnerTeam, { isArray: true }) @OpenAPI({ description: 'Lists all runnerTeams.' }) - getAll() { - return this.runnerTeamRepository.find({ relations: ['parentGroup', 'contact'] }); + async getAll() { + let responseTeams: ResponseRunnerTeam[] = new Array(); + const runners = await this.runnerTeamRepository.find({ relations: ['parentGroup', 'contact'] }); + console.log(runners); + runners.forEach(runner => { + responseTeams.push(new ResponseRunnerTeam(runner)); + }); + return responseTeams; } @Get('/:id') - @ResponseSchema(RunnerTeam) + @ResponseSchema(ResponseRunnerTeam) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @OnUndefined(RunnerTeamNotFoundError) @OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' }) - getOne(@Param('id') id: number) { - return this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }); + async getOne(@Param('id') id: number) { + return new ResponseRunnerTeam(await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] })); } @Post() - @ResponseSchema(RunnerTeam) + @ResponseSchema(ResponseRunnerTeam) @OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' }) async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) { let runnerTeam; @@ -48,11 +55,14 @@ export class RunnerTeamController { return error; } - return this.runnerTeamRepository.save(runnerTeam); + runnerTeam = await this.runnerTeamRepository.save(runnerTeam); + runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] }); + + return new ResponseRunnerTeam(runnerTeam); } @Put('/:id') - @ResponseSchema(RunnerTeam) + @ResponseSchema(ResponseRunnerTeam) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 }) @OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." }) @@ -68,11 +78,13 @@ export class RunnerTeamController { } await this.runnerTeamRepository.update(oldRunnerTeam, runnerTeam); - return runnerTeam; + + runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] }); + return new ResponseRunnerTeam(runnerTeam); } @Delete('/:id') - @ResponseSchema(RunnerTeam) + @ResponseSchema(ResponseRunnerTeam) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 }) @OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' }) @@ -95,6 +107,6 @@ export class RunnerTeamController { }); await this.runnerTeamRepository.delete(runnerTeam); - return runnerTeam; + return new ResponseRunnerTeam(runnerTeam); } } diff --git a/src/models/responses/ResponseRunnerGroup.ts b/src/models/responses/ResponseRunnerGroup.ts new file mode 100644 index 0000000..922f141 --- /dev/null +++ b/src/models/responses/ResponseRunnerGroup.ts @@ -0,0 +1,55 @@ +import { + IsInt, + + + + IsNotEmpty, + + + + IsObject, + + + + IsOptional, + + + + IsString +} from "class-validator"; +import { GroupContact } from '../entities/GroupContact'; +import { RunnerGroup } from '../entities/RunnerGroup'; + +/** + * Defines a track of given length. +*/ +export abstract class ResponseRunnerGroup { + /** + * Autogenerated unique id (primary key). + */ + @IsInt() + @IsNotEmpty() + id: number;; + + /** + * The groups's name. + */ + @IsString() + @IsNotEmpty() + name: string; + + + /** + * The group's contact. + * Optional. + */ + @IsObject() + @IsOptional() + contact?: GroupContact; + + public constructor(group: RunnerGroup) { + this.id = group.id; + this.name = group.name; + this.contact = group.contact; + } +} diff --git a/src/models/responses/ResponseRunnerTeam.ts b/src/models/responses/ResponseRunnerTeam.ts new file mode 100644 index 0000000..b26e2c2 --- /dev/null +++ b/src/models/responses/ResponseRunnerTeam.ts @@ -0,0 +1,26 @@ +import { + IsNotEmpty, + IsObject +} from "class-validator"; +import { RunnerOrganisation } from '../entities/RunnerOrganisation'; +import { RunnerTeam } from '../entities/RunnerTeam'; +import { ResponseRunnerGroup } from './ResponseRunnerGroup'; + +/** + * Defines RunnerTeam's response class. +*/ +export class ResponseRunnerTeam extends ResponseRunnerGroup { + + /** + * The team's parent group (organisation). + * Optional. + */ + @IsObject() + @IsNotEmpty() + parentGroup: RunnerOrganisation; + + public constructor(team: RunnerTeam) { + super(team); + this.parentGroup = team.parentGroup; + } +}