From 7bbf769bddaa92ec263d06adb5ccd3199feb8bc4 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 2 Dec 2020 19:05:58 +0100 Subject: [PATCH] Added basic creation class --- src/controllers/RunnerController.ts | 80 +++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/src/controllers/RunnerController.ts b/src/controllers/RunnerController.ts index 89e5e8b..5b38bf6 100644 --- a/src/controllers/RunnerController.ts +++ b/src/controllers/RunnerController.ts @@ -1,23 +1,69 @@ -import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError, Authorized } from 'routing-controllers'; +import { + JsonController, + Param, + Body, + Get, + Post, + Put, + Delete, + NotFoundError, + OnUndefined, + NotAcceptableError, + Authorized +} from 'routing-controllers'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { Runner } from '../models/Runner'; -import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator'; -import {RunnerIdsNotMatchingError, RunnerNotFoundError} from "../errors/RunnerErrors"; +import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsPositive, IsString } from 'class-validator'; +import { RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors'; class CreateRunner { + /** + * The runners's first name. + */ + @IsNotEmpty() + @IsString() + firstname: string; + + /** + * The runners's middle name. + * Optional + */ + @IsOptional() + @IsString() + middlename?: string; + + /** + * The runers's last name. + */ @IsString() @IsNotEmpty() - name: string; + lastname: string; + + /** + * The runner's phone number. + * Optional + */ + @IsOptional() + @IsPhoneNumber('DE') + phone?: string; + + /** + * The runner's email address. + * Optional + */ + @IsOptional() + @IsEmail() + email?: string; @IsInt() - @IsPositive() - length: number; + @IsOptional() + groupId?: number; } @JsonController('/runners') -@Authorized("RUNNERS:read") +//@Authorized('RUNNERS:read') export class RunnerController { private runnerRepository: Repository; @@ -30,23 +76,23 @@ export class RunnerController { @Get() @ResponseSchema(Runner, { isArray: true }) - @OpenAPI({ description: "Lists all runners." }) + @OpenAPI({ description: 'Lists all runners.' }) getAll() { return this.runnerRepository.find(); } @Get('/:id') @ResponseSchema(Runner) - @ResponseSchema(RunnerNotFoundError, {statusCode: 404}) + @ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) @OnUndefined(RunnerNotFoundError) - @OpenAPI({ description: "Returns a runner of a specified id (if it exists)" }) + @OpenAPI({ description: 'Returns a runner of a specified id (if it exists)' }) getOne(@Param('id') id: number) { return this.runnerRepository.findOne({ id: id }); } @Post() @ResponseSchema(Runner) - @OpenAPI({ description: "Create a new runner object (id will be generated automagicly)." }) + @OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' }) post( @Body({ validate: true }) runner: CreateRunner @@ -56,9 +102,9 @@ export class RunnerController { @Put('/:id') @ResponseSchema(Runner) - @ResponseSchema(RunnerNotFoundError, {statusCode: 404}) - @ResponseSchema(RunnerIdsNotMatchingError, {statusCode: 406}) - @OpenAPI({description: "Update a runner object (id can't be changed)."}) + @ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) + @ResponseSchema(RunnerIdsNotMatchingError, { statusCode: 406 }) + @OpenAPI({ description: "Update a runner object (id can't be changed)." }) async put(@Param('id') id: number, @EntityFromBody() runner: Runner) { let oldRunner = await this.runnerRepository.findOne({ id: id }); @@ -66,7 +112,7 @@ export class RunnerController { throw new RunnerNotFoundError(); } - if(oldRunner.id != runner.id){ + if (oldRunner.id != runner.id) { throw new RunnerIdsNotMatchingError(); } @@ -76,8 +122,8 @@ export class RunnerController { @Delete('/:id') @ResponseSchema(Runner) - @ResponseSchema(RunnerNotFoundError, {statusCode: 404}) - @OpenAPI({description: "Delete a specified runner (if it exists)."}) + @ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) + @OpenAPI({ description: 'Delete a specified runner (if it exists).' }) async remove(@Param('id') id: number) { let runner = await this.runnerRepository.findOne({ id: id });