Added endpoint descriptions

ref #4
This commit is contained in:
Nicolai Ort 2020-11-27 19:58:00 +01:00
parent d2278fd248
commit b267d87ba6

View File

@ -1,7 +1,7 @@
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined } from 'routing-controllers'; import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { Track } from '../models/Track'; import { Track } from '../models/Track';
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator'; import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
@ -25,12 +25,16 @@ export class TrackNotFoundError extends NotFoundError {
export class TrackController { export class TrackController {
private trackRepository: Repository<Track>; private trackRepository: Repository<Track>;
/**
* Gets the repository of this controller's model/entity.
*/
constructor() { constructor() {
this.trackRepository = getConnectionManager().get().getRepository(Track); this.trackRepository = getConnectionManager().get().getRepository(Track);
} }
@Get() @Get()
@ResponseSchema(Track, { isArray: true }) @ResponseSchema(Track, { isArray: true })
@OpenAPI({description: "Lists all tracks."})
getAll() { getAll() {
return this.trackRepository.find(); return this.trackRepository.find();
} }
@ -38,12 +42,14 @@ export class TrackController {
@Get('/:id') @Get('/:id')
@ResponseSchema(Track) @ResponseSchema(Track)
@OnUndefined(TrackNotFoundError) @OnUndefined(TrackNotFoundError)
@OpenAPI({description: "Returns a track of a specified id (if it exists)"})
getOne(@Param('id') id: number) { getOne(@Param('id') id: number) {
return this.trackRepository.findOne({ id: id }); return this.trackRepository.findOne({ id: id });
} }
@Post() @Post()
@ResponseSchema(Track) @ResponseSchema(Track)
@OpenAPI({description: "Create a new track object (id will be generated automagicly)."})
post( post(
@Body({ validate: true }) @Body({ validate: true })
track: CreateTrack track: CreateTrack
@ -53,6 +59,7 @@ export class TrackController {
@Put('/:id') @Put('/:id')
@ResponseSchema(Track) @ResponseSchema(Track)
@OpenAPI({description: "Update a track object (id can't be changed)."})
async put(@Param('id') id: number, @EntityFromBody() track: Track) { async put(@Param('id') id: number, @EntityFromBody() track: Track) {
let oldTrack = await this.trackRepository.findOne({ id: id }); let oldTrack = await this.trackRepository.findOne({ id: id });
@ -66,7 +73,7 @@ export class TrackController {
@Delete('/:id') @Delete('/:id')
@ResponseSchema(Track) @ResponseSchema(Track)
@OnUndefined(TrackNotFoundError) @OpenAPI({description: "Delete a specified track (if it exists)."})
async remove(@Param('id') id: number) { async remove(@Param('id') id: number) {
let track = await this.trackRepository.findOne({ id: id }); let track = await this.trackRepository.findOne({ id: id });