From a35e6d0a9f19dc7dbde3c867b840276416eac9f6 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 3 Dec 2020 19:20:53 +0100 Subject: [PATCH] Added basics for runnerorg controller ref #13 --- .../RunnerOrganisationController.ts | 87 +++++++++++++++++++ src/errors/RunnerOrganisationErrors.ts | 27 ++++++ src/models/CreateRunnerOrganisation.ts | 15 ++++ 3 files changed, 129 insertions(+) create mode 100644 src/controllers/RunnerOrganisationController.ts create mode 100644 src/errors/RunnerOrganisationErrors.ts create mode 100644 src/models/CreateRunnerOrganisation.ts diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts new file mode 100644 index 0000000..6297073 --- /dev/null +++ b/src/controllers/RunnerOrganisationController.ts @@ -0,0 +1,87 @@ +import { JsonController, Param, Body, Get, Post, Put, Delete, OnUndefined } from 'routing-controllers'; +import { getConnectionManager, Repository } from 'typeorm'; +import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; +import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; +import { RunnerOrganisation } from '../models/RunnerOrganisation'; +import { RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; +import { CreateRunnerOrganisation } from '../models/CreateRunnerOrganisation'; +import { RunnerGroup } from '../models/RunnerGroup'; + + +@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(RunnerGroup); + } + + @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) { + let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }); + + if (!runnerOrganisation) { + throw new RunnerOrganisationNotFoundError(); + } + + await this.runnerOrganisationRepository.delete(runnerOrganisation); + return runnerOrganisation; + } +} diff --git a/src/errors/RunnerOrganisationErrors.ts b/src/errors/RunnerOrganisationErrors.ts new file mode 100644 index 0000000..f846ffa --- /dev/null +++ b/src/errors/RunnerOrganisationErrors.ts @@ -0,0 +1,27 @@ +import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers'; +import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator'; + +/** + * Error to throw when a runner couldn't be found. + * Implemented this ways to work with the json-schema conversion for openapi. + */ +export class RunnerOrganisationNotFoundError extends NotFoundError { + @IsString() + name = "RunnerOrganisationNotFoundError" + + @IsString() + message = "RunnerOrganisation not found!" +} + +/** + * Error to throw when two runners' ids don't match. + * Usually occurs when a user tries to change a runner's id. + * Implemented this ways to work with the json-schema conversion for openapi. + */ +export class RunnerOrganisationIdsNotMatchingError extends NotAcceptableError { + @IsString() + name = "RunnerOrganisationIdsNotMatchingError" + + @IsString() + message = "The id's don't match!! \n And if you wanted to change a runner's id: This isn't allowed" +} \ No newline at end of file diff --git a/src/models/CreateRunnerOrganisation.ts b/src/models/CreateRunnerOrganisation.ts new file mode 100644 index 0000000..b7e5c71 --- /dev/null +++ b/src/models/CreateRunnerOrganisation.ts @@ -0,0 +1,15 @@ +import { IsString } from 'class-validator'; +import { RunnerOrganisation } from './RunnerOrganisation'; + +export class CreateRunnerOrganisation { + @IsString() + name: string; + + public async toRunnerOrganisation(): Promise { + let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation(); + + newRunnerOrganisation.name = this.name; + + return newRunnerOrganisation; + } +} \ No newline at end of file