Added basic creation class
This commit is contained in:
parent
980ac64688
commit
7bbf769bdd
|
@ -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<Runner>;
|
||||
|
||||
|
@ -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 });
|
||||
|
||||
|
|
Loading…
Reference in New Issue