From b267d87ba66e19e61fc04a64dee5056e3cad91e5 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 27 Nov 2020 19:58:00 +0100 Subject: [PATCH] Added endpoint descriptions ref #4 --- src/controllers/TrackController.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/controllers/TrackController.ts b/src/controllers/TrackController.ts index 029ee7b..62fd70f 100644 --- a/src/controllers/TrackController.ts +++ b/src/controllers/TrackController.ts @@ -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 { 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 { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator'; @@ -25,12 +25,16 @@ export class TrackNotFoundError extends NotFoundError { export class TrackController { private trackRepository: Repository; + /** + * Gets the repository of this controller's model/entity. + */ constructor() { this.trackRepository = getConnectionManager().get().getRepository(Track); } @Get() @ResponseSchema(Track, { isArray: true }) + @OpenAPI({description: "Lists all tracks."}) getAll() { return this.trackRepository.find(); } @@ -38,12 +42,14 @@ export class TrackController { @Get('/:id') @ResponseSchema(Track) @OnUndefined(TrackNotFoundError) + @OpenAPI({description: "Returns a track of a specified id (if it exists)"}) getOne(@Param('id') id: number) { return this.trackRepository.findOne({ id: id }); } @Post() @ResponseSchema(Track) + @OpenAPI({description: "Create a new track object (id will be generated automagicly)."}) post( @Body({ validate: true }) track: CreateTrack @@ -53,6 +59,7 @@ export class TrackController { @Put('/:id') @ResponseSchema(Track) + @OpenAPI({description: "Update a track object (id can't be changed)."}) async put(@Param('id') id: number, @EntityFromBody() track: Track) { let oldTrack = await this.trackRepository.findOne({ id: id }); @@ -66,7 +73,7 @@ export class TrackController { @Delete('/:id') @ResponseSchema(Track) - @OnUndefined(TrackNotFoundError) + @OpenAPI({description: "Delete a specified track (if it exists)."}) async remove(@Param('id') id: number) { let track = await this.trackRepository.findOne({ id: id });