From 7b084895334431445ae2c038bd086c8833a8e530 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 4 Dec 2020 21:55:09 +0100 Subject: [PATCH] Now with working runner orga controller including responses ref #13 --- .../RunnerOrganisationController.ts | 63 ++++++++++++------- src/controllers/RunnerTeamController.ts | 5 +- .../responses/ResponseRunnerOrganisation.ts | 37 +++++++++++ 3 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 src/models/responses/ResponseRunnerOrganisation.ts diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts index bee281f..bed6eec 100644 --- a/src/controllers/RunnerOrganisationController.ts +++ b/src/controllers/RunnerOrganisationController.ts @@ -7,11 +7,12 @@ import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganis import { Runner } from '../models/entities/Runner'; import { RunnerOrganisation } from '../models/entities/RunnerOrganisation'; import { RunnerTeam } from '../models/entities/RunnerTeam'; +import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation'; import { RunnerController } from './RunnerController'; import { RunnerTeamController } from './RunnerTeamController'; -@JsonController('/organisations') +@JsonController('/organisation') //@Authorized('RUNNERS:read') export class RunnerOrganisationController { private runnerOrganisationRepository: Repository; @@ -24,23 +25,29 @@ export class RunnerOrganisationController { } @Get() - @ResponseSchema(RunnerOrganisation, { isArray: true }) + @ResponseSchema(ResponseRunnerOrganisation, { isArray: true }) @OpenAPI({ description: 'Lists all runnerOrganisations.' }) - getAll() { - return this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] }); + async getAll() { + let responseTeams: ResponseRunnerOrganisation[] = new Array(); + const runners = await this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] }); + console.log(runners); + runners.forEach(runner => { + responseTeams.push(new ResponseRunnerOrganisation(runner)); + }); + return responseTeams; } @Get('/:id') - @ResponseSchema(RunnerOrganisation) + @ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @OnUndefined(RunnerOrganisationNotFoundError) @OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' }) - getOne(@Param('id') id: number) { - return this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }); + async getOne(@Param('id') id: number) { + return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] })); } @Post() - @ResponseSchema(RunnerOrganisation) + @ResponseSchema(ResponseRunnerOrganisation) @OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' }) async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) { let runnerOrganisation; @@ -50,11 +57,14 @@ export class RunnerOrganisationController { return error; } - return this.runnerOrganisationRepository.save(runnerOrganisation); + runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation); + runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }); + + return new ResponseRunnerOrganisation(runnerOrganisation); } @Put('/:id') - @ResponseSchema(RunnerOrganisation) + @ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 }) @OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." }) @@ -70,14 +80,15 @@ export class RunnerOrganisationController { } await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation); - return runnerOrganisation; + + runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }); + return new ResponseRunnerOrganisation(runnerOrganisation); } @Delete('/:id') - @ResponseSchema(RunnerOrganisation) + @ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 }) - @ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 }) @OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' }) async remove(@Param('id') id: number, @QueryParam("force") force: boolean) { let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }); @@ -87,25 +98,29 @@ export class RunnerOrganisationController { } let runners: Runner[] = await runnerOrganisation.getRunners() - - if (!force && runners.length != 0) { - throw new RunnerOrganisationHasRunnersError(); + if (!force) { + if (runners.length != 0) { + throw new RunnerOrganisationHasRunnersError(); + } } const runnerController = new RunnerController() - await runners.forEach(async runner => { - await runnerController.remove(runner.id, true); + runners.forEach(runner => { + runnerController.remove(runner.id, true) }); let teams: RunnerTeam[] = await runnerOrganisation.getTeams() - if (!force && teams.length != 0) { - throw new RunnerOrganisationHasTeamsError(); + if (!force) { + if (teams.length != 0) { + throw new RunnerOrganisationHasTeamsError(); + } } const teamController = new RunnerTeamController() - await teams.forEach(async team => { - await teamController.remove(team.id, true); + teams.forEach(team => { + teamController.remove(team.id, true) }); - await this.runnerOrganisationRepository.delete(runnerOrganisation); - return runnerOrganisation; + const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation); + await this.runnerOrganisationRepository.delete({ id: runnerOrganisation.id }); + return responseOrganisation; } } diff --git a/src/controllers/RunnerTeamController.ts b/src/controllers/RunnerTeamController.ts index e07931a..6103083 100644 --- a/src/controllers/RunnerTeamController.ts +++ b/src/controllers/RunnerTeamController.ts @@ -106,7 +106,8 @@ export class RunnerTeamController { runnerController.remove(runner.id, true) }); - await this.runnerTeamRepository.delete(runnerTeam); - return new ResponseRunnerTeam(runnerTeam); + const responseTeam = new ResponseRunnerTeam(runnerTeam); + await this.runnerTeamRepository.delete({ id: runnerTeam.id }); + return responseTeam; } } diff --git a/src/models/responses/ResponseRunnerOrganisation.ts b/src/models/responses/ResponseRunnerOrganisation.ts new file mode 100644 index 0000000..2aecb75 --- /dev/null +++ b/src/models/responses/ResponseRunnerOrganisation.ts @@ -0,0 +1,37 @@ +import { + IsArray, + IsNotEmpty, + IsObject +} from "class-validator"; +import { Address } from '../entities/Address'; +import { RunnerOrganisation } from '../entities/RunnerOrganisation'; +import { RunnerTeam } from '../entities/RunnerTeam'; +import { ResponseRunnerGroup } from './ResponseRunnerGroup'; + +/** + * Defines RunnerOrgs's response class. +*/ +export class ResponseRunnerOrganisation extends ResponseRunnerGroup { + + /** + * The orgs's address. + * Optional. + */ + @IsObject() + @IsNotEmpty() + address?: Address; + + /** + * The orgs associated teams. + */ + @IsObject() + @IsArray() + teams: RunnerTeam[]; + + + public constructor(org: RunnerOrganisation) { + super(org); + this.address = org.address; + this.teams = org.teams; + } +}