import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; import { CreateRunnerOrganisation } from '../models/actions/CreateRunnerOrganisation'; import { RunnerOrganisation } from '../models/entities/RunnerOrganisation'; import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation'; import { RunnerController } from './RunnerController'; import { RunnerTeamController } from './RunnerTeamController'; @JsonController('/organisations') //@Authorized('RUNNERS:read') export class RunnerOrganisationController { private runnerOrganisationRepository: Repository; /** * Gets the repository of this controller's model/entity. */ constructor() { this.runnerOrganisationRepository = getConnectionManager().get().getRepository(RunnerOrganisation); } @Get() @ResponseSchema(ResponseRunnerOrganisation, { isArray: true }) @OpenAPI({ description: 'Lists all runnerOrganisations.' }) 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(ResponseRunnerOrganisation) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @OnUndefined(RunnerOrganisationNotFoundError) @OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' }) async getOne(@Param('id') id: number) { return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] })); } @Post() @ResponseSchema(ResponseRunnerOrganisation) @OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' }) async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) { let runnerOrganisation; try { runnerOrganisation = await createRunnerOrganisation.toRunnerOrganisation(); } catch (error) { return error; } runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation); return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] })); } @Put('/:id') @ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 }) @OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." }) async put(@Param('id') id: number, @EntityFromBody() runnerOrganisation: RunnerOrganisation) { let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }); if (!oldRunnerOrganisation) { throw new RunnerOrganisationNotFoundError(); } if (oldRunnerOrganisation.id != runnerOrganisation.id) { throw new RunnerOrganisationIdsNotMatchingError(); } await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation); runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }); return new ResponseRunnerOrganisation(runnerOrganisation); } @Delete('/:id') @ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 }) @ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 }) @OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' }) async remove(@EntityFromParam('id') organisation: RunnerOrganisation, @QueryParam("force") force: boolean) { if (!organisation) { throw new RunnerOrganisationNotFoundError() } let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: ['address', 'contact', 'runners', 'teams'] }); if (!force) { if (runnerOrganisation.teams.length != 0) { throw new RunnerOrganisationHasTeamsError(); } } const teamController = new RunnerTeamController() for (let team of runnerOrganisation.teams) { await teamController.remove(team, true); } if (!force) { if (runnerOrganisation.runners.length != 0) { throw new RunnerOrganisationHasRunnersError(); } } const runnerController = new RunnerController() for (let runner of runnerOrganisation.runners) { await runnerController.remove(runner, true); } const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation); await this.runnerOrganisationRepository.delete(organisation); return responseOrganisation; } }