Moded track controller related models to a new file

This commit is contained in:
Nicolai Ort 2020-12-03 17:43:24 +01:00
parent a86465c554
commit da4597fa62
2 changed files with 32 additions and 20 deletions

View File

@ -4,20 +4,11 @@ import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { Track } from '../models/Track';
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
import {TrackIdsNotMatchingError, TrackNotFoundError} from "../errors/TrackErrors";
class CreateTrack {
@IsString()
@IsNotEmpty()
name: string;
@IsInt()
@IsPositive()
length: number;
}
import { TrackIdsNotMatchingError, TrackNotFoundError } from "../errors/TrackErrors";
import { CreateTrack } from '../models/CreateTrack';
@JsonController('/tracks')
@Authorized("TRACKS:read")
//@Authorized("TRACKS:read")
export class TrackController {
private trackRepository: Repository<Track>;
@ -37,7 +28,7 @@ export class TrackController {
@Get('/:id')
@ResponseSchema(Track)
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
@ResponseSchema(TrackNotFoundError, { statusCode: 404 })
@OnUndefined(TrackNotFoundError)
@OpenAPI({ description: "Returns a track of a specified id (if it exists)" })
getOne(@Param('id') id: number) {
@ -51,14 +42,14 @@ export class TrackController {
@Body({ validate: true })
track: CreateTrack
) {
return this.trackRepository.save(track);
return this.trackRepository.save(track.getTrack());
}
@Put('/:id')
@ResponseSchema(Track)
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
@ResponseSchema(TrackIdsNotMatchingError, {statusCode: 406})
@OpenAPI({description: "Update a track object (id can't be changed)."})
@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 });
@ -66,7 +57,7 @@ export class TrackController {
throw new TrackNotFoundError();
}
if(oldTrack.id != track.id){
if (oldTrack.id != track.id) {
throw new TrackIdsNotMatchingError();
}
@ -76,8 +67,8 @@ export class TrackController {
@Delete('/:id')
@ResponseSchema(Track)
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
@OpenAPI({description: "Delete a specified track (if it exists)."})
@ResponseSchema(TrackNotFoundError, { statusCode: 404 })
@OpenAPI({ description: "Delete a specified track (if it exists)." })
async remove(@Param('id') id: number) {
let track = await this.trackRepository.findOne({ id: id });

21
src/models/CreateTrack.ts Normal file
View File

@ -0,0 +1,21 @@
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
import { Track } from './Track';
export class CreateTrack {
@IsString()
@IsNotEmpty()
name: string;
@IsInt()
@IsPositive()
distance: number;
public getTrack(): Track {
let newTrack: Track = new Track();
newTrack.name = this.name;
newTrack.distance = this.distance;
return newTrack;
}
}