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 } from 'typeorm-routing-controllers-extensions'; import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganisation'; 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('/organisation') //@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); runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }); return new ResponseRunnerOrganisation(runnerOrganisation); } @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(RunnerOrganisationHasRunnersError, { 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'] }); if (!runnerOrganisation) { throw new RunnerOrganisationNotFoundError(); } let runners: Runner[] = await runnerOrganisation.getRunners() if (!force) { if (runners.length != 0) { throw new RunnerOrganisationHasRunnersError(); } } const runnerController = new RunnerController() runners.forEach(runner => { runnerController.remove(runner.id, true) }); let teams: RunnerTeam[] = await runnerOrganisation.getTeams() if (!force) { if (teams.length != 0) { throw new RunnerOrganisationHasTeamsError(); } } const teamController = new RunnerTeamController() teams.forEach(team => { teamController.remove(team.id, true) }); const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation); await this.runnerOrganisationRepository.delete({ id: runnerOrganisation.id }); return responseOrganisation; } }