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