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 { RunnerOrganisation } from '../models/entities/RunnerOrganisation'; @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(RunnerOrganisation, { isArray: true }) @OpenAPI({ description: 'Lists all runnerOrganisations.' }) getAll() { return this.runnerOrganisationRepository.find(); } @Get('/:id') @ResponseSchema(RunnerOrganisation) @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 }); } @Post() @ResponseSchema(RunnerOrganisation) @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; } return this.runnerOrganisationRepository.save(runnerOrganisation); } @Put('/:id') @ResponseSchema(RunnerOrganisation) @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 }); if (!oldRunnerOrganisation) { throw new RunnerOrganisationNotFoundError(); } if (oldRunnerOrganisation.id != runnerOrganisation.id) { throw new RunnerOrganisationIdsNotMatchingError(); } await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation); return runnerOrganisation; } @Delete('/:id') @ResponseSchema(RunnerOrganisation) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' }) async remove(@Param('id') id: number, @QueryParam("force") force) { let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }); if (!runnerOrganisation) { throw new RunnerOrganisationNotFoundError(); } if (!force) { if (runnerOrganisation.runners.length != 0) { throw new RunnerOrganisationHasRunnersError(); } if (runnerOrganisation.teams.length != 0) { throw new RunnerOrganisationHasTeamsError(); } } //TODO: Delete runner and teams await this.runnerOrganisationRepository.delete(runnerOrganisation); return runnerOrganisation; } }