parent
15552eff54
commit
f0a7cbbcae
|
@ -4,7 +4,7 @@ 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 {TrackIdChangeNotAllowedError, TrackNotFoundError} from "../errors/TrackErrors";
|
||||
import {TrackIdsNotMatchingError, TrackNotFoundError} from "../errors/TrackErrors";
|
||||
|
||||
class CreateTrack {
|
||||
@IsString()
|
||||
|
@ -57,7 +57,7 @@ export class TrackController {
|
|||
@Put('/:id')
|
||||
@ResponseSchema(Track)
|
||||
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
|
||||
@ResponseSchema(TrackIdChangeNotAllowedError, {statusCode: 406})
|
||||
@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 });
|
||||
|
@ -67,7 +67,7 @@ export class TrackController {
|
|||
}
|
||||
|
||||
if(oldTrack.id != track.id){
|
||||
throw new TrackIdChangeNotAllowedError();
|
||||
throw new TrackIdsNotMatchingError();
|
||||
}
|
||||
|
||||
await this.trackRepository.update(oldTrack, track);
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers';
|
||||
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
||||
|
||||
/**
|
||||
* Error to throw when a track couldn't be found.
|
||||
* Implemented this ways to work with the json-schema conversion for openapi.
|
||||
*/
|
||||
export class TrackNotFoundError extends NotFoundError {
|
||||
@IsString()
|
||||
name = "TrackNotFoundError"
|
||||
|
@ -9,9 +13,14 @@ export class TrackNotFoundError extends NotFoundError {
|
|||
message = "Track not found!"
|
||||
}
|
||||
|
||||
export class TrackIdChangeNotAllowedError extends NotAcceptableError {
|
||||
/**
|
||||
* Error to throw when two tracks' ids don't match.
|
||||
* Usually occurs when a user tries to change a track's id.
|
||||
* Implemented this ways to work with the json-schema conversion for openapi.
|
||||
*/
|
||||
export class TrackIdsNotMatchingError extends NotAcceptableError {
|
||||
@IsString()
|
||||
name = "TrackIdChangeNotAllowed"
|
||||
name = "TrackIdsNotMatchingError"
|
||||
|
||||
@IsString()
|
||||
message = "The id's don't match!! \n And if you wanted to change a track's id: This isn't allowed"
|
||||
|
|
|
@ -8,23 +8,29 @@ import {
|
|||
} from "class-validator";
|
||||
|
||||
/**
|
||||
* @classdesc Defines a track of given length.
|
||||
* @property {number} id - Autogenerated unique id
|
||||
* @property {string} name - The track's name
|
||||
* @property {number} lenth - The track's length in meters
|
||||
* Defines a track of given length.
|
||||
*/
|
||||
@Entity()
|
||||
export class Track {
|
||||
/**
|
||||
* Autogenerated unique id (primary key).
|
||||
*/
|
||||
@PrimaryGeneratedColumn()
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The track's name.
|
||||
*/
|
||||
@Column()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The track's length in meters.
|
||||
*/
|
||||
@Column()
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
|
|
Loading…
Reference in New Issue