diff --git a/src/controllers/TrackController.ts b/src/controllers/TrackController.ts index 1f74193..d2d7e74 100644 --- a/src/controllers/TrackController.ts +++ b/src/controllers/TrackController.ts @@ -2,7 +2,7 @@ import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; -import { TrackIdsNotMatchingError, TrackNotFoundError } from "../errors/TrackErrors"; +import { TrackIdsNotMatchingError, TrackLapTimeCantBeNegativeError, TrackNotFoundError } from "../errors/TrackErrors"; import { CreateTrack } from '../models/actions/CreateTrack'; import { Track } from '../models/entities/Track'; import { ResponseEmpty } from '../models/responses/ResponseEmpty'; @@ -48,6 +48,7 @@ export class TrackController { @Post() @Authorized("TRACK:CREATE") @ResponseSchema(ResponseTrack) + @ResponseSchema(TrackLapTimeCantBeNegativeError, { statusCode: 406 }) @OpenAPI({ description: "Create a new track.
Please remember that the track\'s distance must be greater than 0." }) async post( @Body({ validate: true }) @@ -61,6 +62,7 @@ export class TrackController { @ResponseSchema(ResponseTrack) @ResponseSchema(TrackNotFoundError, { statusCode: 404 }) @ResponseSchema(TrackIdsNotMatchingError, { statusCode: 406 }) + @ResponseSchema(TrackLapTimeCantBeNegativeError, { statusCode: 406 }) @OpenAPI({ description: "Update the track whose id you provided.
Please remember that ids can't be changed." }) async put(@Param('id') id: number, @EntityFromBody() track: Track) { let oldTrack = await this.trackRepository.findOne({ id: id }); diff --git a/src/errors/TrackErrors.ts b/src/errors/TrackErrors.ts index 7d4cfa9..e3d1902 100644 --- a/src/errors/TrackErrors.ts +++ b/src/errors/TrackErrors.ts @@ -22,4 +22,15 @@ export class TrackIdsNotMatchingError extends NotAcceptableError { @IsString() message = "The ids don't match! \n And if you wanted to change a track's id: This isn't allowed" +} + +/** + * Error to throw when a track's lap time is set to a negative value. + */ +export class TrackLapTimeCantBeNegativeError extends NotAcceptableError { + @IsString() + name = "TrackLapTimeCantBeNegativeError" + + @IsString() + message = "The minimum lap time you provided is negative - That isn't possible. \n If you wanted to disable it: Just set it to 0/null." } \ No newline at end of file diff --git a/src/models/actions/CreateTrack.ts b/src/models/actions/CreateTrack.ts index c4dde39..0130ad8 100644 --- a/src/models/actions/CreateTrack.ts +++ b/src/models/actions/CreateTrack.ts @@ -1,4 +1,5 @@ import { IsInt, IsNotEmpty, IsOptional, IsPositive, IsString } from 'class-validator'; +import { TrackLapTimeCantBeNegativeError } from '../../errors/TrackErrors'; import { Track } from '../entities/Track'; /** @@ -25,8 +26,7 @@ export class CreateTrack { */ @IsInt() @IsOptional() - @IsPositive() - minimumLapTime?: number; + minimumLapTime: number; /** * Creates a new Track entity from this. @@ -37,6 +37,9 @@ export class CreateTrack { newTrack.name = this.name; newTrack.distance = this.distance; newTrack.minimumLapTime = this.minimumLapTime; + if (this.minimumLapTime < 0) { + throw new TrackLapTimeCantBeNegativeError(); + } return newTrack; } diff --git a/src/models/entities/Track.ts b/src/models/entities/Track.ts index 19a08fa..3f05262 100644 --- a/src/models/entities/Track.ts +++ b/src/models/entities/Track.ts @@ -43,10 +43,9 @@ export class Track { * The minimum time a runner should take to run a lap on this track. * Will be used for fraud detection. */ - @Column() + @Column({ nullable: true }) @IsInt() @IsOptional() - @IsPositive() minimumLapTime?: number; /** diff --git a/src/models/responses/ResponseTrack.ts b/src/models/responses/ResponseTrack.ts index 17c128c..2ae34ff 100644 --- a/src/models/responses/ResponseTrack.ts +++ b/src/models/responses/ResponseTrack.ts @@ -1,4 +1,5 @@ import { IsInt, IsOptional, IsString } from "class-validator"; +import { TrackLapTimeCantBeNegativeError } from '../../errors/TrackErrors'; import { Track } from '../entities/Track'; /** @@ -40,5 +41,8 @@ export class ResponseTrack { this.name = track.name; this.distance = track.distance; this.minimumLapTime = track.minimumLapTime; + if (this.minimumLapTime < 0) { + throw new TrackLapTimeCantBeNegativeError(); + } } }