@@ -4,7 +4,7 @@ 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 {TrackIdChangeNotAllowedError, TrackNotFoundError} from "../errors/TrackErrors";
 | 
					import {TrackIdsNotMatchingError, TrackNotFoundError} from "../errors/TrackErrors";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CreateTrack {
 | 
					class CreateTrack {
 | 
				
			||||||
	@IsString()
 | 
						@IsString()
 | 
				
			||||||
@@ -57,7 +57,7 @@ export class TrackController {
 | 
				
			|||||||
	@Put('/:id')
 | 
						@Put('/:id')
 | 
				
			||||||
	@ResponseSchema(Track)
 | 
						@ResponseSchema(Track)
 | 
				
			||||||
	@ResponseSchema(TrackNotFoundError, {statusCode: 404})
 | 
						@ResponseSchema(TrackNotFoundError, {statusCode: 404})
 | 
				
			||||||
	@ResponseSchema(TrackIdChangeNotAllowedError, {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 });
 | 
				
			||||||
@@ -67,7 +67,7 @@ export class TrackController {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(oldTrack.id != track.id){
 | 
							if(oldTrack.id != track.id){
 | 
				
			||||||
			throw new TrackIdChangeNotAllowedError();
 | 
								throw new TrackIdsNotMatchingError();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		await this.trackRepository.update(oldTrack, track);
 | 
							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 { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers';
 | 
				
			||||||
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
 | 
					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 {
 | 
					export class TrackNotFoundError extends NotFoundError {
 | 
				
			||||||
	@IsString()
 | 
						@IsString()
 | 
				
			||||||
	name = "TrackNotFoundError"
 | 
						name = "TrackNotFoundError"
 | 
				
			||||||
@@ -9,9 +13,14 @@ export class TrackNotFoundError extends NotFoundError {
 | 
				
			|||||||
	message = "Track not found!"
 | 
						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()
 | 
						@IsString()
 | 
				
			||||||
	name = "TrackIdChangeNotAllowed"
 | 
						name = "TrackIdsNotMatchingError"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@IsString()
 | 
						@IsString()
 | 
				
			||||||
	message = "The id's don't match!! \n And if you wanted to change a track's id: This isn't allowed"
 | 
						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";
 | 
					} from "class-validator";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @classdesc Defines a track of given length.
 | 
					 * 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
 | 
					 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
@Entity()
 | 
					@Entity()
 | 
				
			||||||
export class Track {
 | 
					export class Track {
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Autogenerated unique id (primary key).
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
  @PrimaryGeneratedColumn()
 | 
					  @PrimaryGeneratedColumn()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  id: number;
 | 
					  id: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The track's name.
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
  @Column()
 | 
					  @Column()
 | 
				
			||||||
  @IsString()
 | 
					  @IsString()
 | 
				
			||||||
  @IsNotEmpty()
 | 
					  @IsNotEmpty()
 | 
				
			||||||
  name: string;
 | 
					  name: string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The track's length in meters.
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
  @Column()
 | 
					  @Column()
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @IsPositive()
 | 
					  @IsPositive()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user