83 lines
2.9 KiB
TypeScript
83 lines
2.9 KiB
TypeScript
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
|
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
|
import { getConnectionManager, Repository } from 'typeorm';
|
|
import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions';
|
|
import { TrackIdsNotMatchingError, TrackNotFoundError } from "../errors/TrackErrors";
|
|
import { CreateTrack } from '../models/creation/CreateTrack';
|
|
import { Track } from '../models/entities/Track';
|
|
import { ResponseTrack } from '../models/responses/ResponseTrack';
|
|
|
|
@JsonController('/tracks')
|
|
//@Authorized("TRACKS:read")
|
|
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(ResponseTrack, { isArray: true })
|
|
@OpenAPI({ description: "Lists all tracks." })
|
|
async getAll() {
|
|
let responseTracks: ResponseTrack[] = new Array<ResponseTrack>();
|
|
const tracks = await this.trackRepository.find();
|
|
tracks.forEach(track => {
|
|
responseTracks.push(new ResponseTrack(track));
|
|
});
|
|
return responseTracks;
|
|
}
|
|
|
|
@Get('/:id')
|
|
@ResponseSchema(ResponseTrack)
|
|
@ResponseSchema(TrackNotFoundError, { statusCode: 404 })
|
|
@OnUndefined(TrackNotFoundError)
|
|
@OpenAPI({ description: "Returns a track of a specified id (if it exists)" })
|
|
async getOne(@Param('id') id: number) {
|
|
return new ResponseTrack(await this.trackRepository.findOne({ id: id }));
|
|
}
|
|
|
|
@Post()
|
|
@ResponseSchema(ResponseTrack)
|
|
@OpenAPI({ description: "Create a new track object (id will be generated automagicly)." })
|
|
async post(
|
|
@Body({ validate: true })
|
|
track: CreateTrack
|
|
) {
|
|
return new ResponseTrack(await this.trackRepository.save(track.toTrack()));
|
|
}
|
|
|
|
@Put('/:id')
|
|
@ResponseSchema(ResponseTrack)
|
|
@ResponseSchema(TrackNotFoundError, { statusCode: 404 })
|
|
@ResponseSchema(TrackIdsNotMatchingError, { statusCode: 406 })
|
|
@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 });
|
|
|
|
if (!oldTrack) {
|
|
throw new TrackNotFoundError();
|
|
}
|
|
|
|
if (oldTrack.id != track.id) {
|
|
throw new TrackIdsNotMatchingError();
|
|
}
|
|
|
|
await this.trackRepository.update(oldTrack, track);
|
|
return new ResponseTrack(track);
|
|
}
|
|
|
|
@Delete('/:id')
|
|
@ResponseSchema(ResponseTrack)
|
|
@ResponseSchema(TrackNotFoundError, { statusCode: 404 })
|
|
@OpenAPI({ description: "Delete a specified track (if it exists)." })
|
|
async remove(@EntityFromParam('id') track: Track) {
|
|
if (!track) { throw new TrackNotFoundError(); }
|
|
|
|
await this.trackRepository.delete(track);
|
|
return new ResponseTrack(track);
|
|
}
|
|
} |