|
|
|
|
@@ -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<Track>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 });
|
|
|
|
|
|
|
|
|
|
|