parent
28c1b6d31d
commit
59cb72a11d
@ -1,9 +1,9 @@
|
|||||||
import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
|
import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
|
||||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||||
import { getConnectionManager, Repository } from 'typeorm';
|
import { getConnectionManager, Repository } from 'typeorm';
|
||||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
|
||||||
import { TrackIdsNotMatchingError, TrackLapTimeCantBeNegativeError, TrackNotFoundError } from "../errors/TrackErrors";
|
import { TrackIdsNotMatchingError, TrackLapTimeCantBeNegativeError, TrackNotFoundError } from "../errors/TrackErrors";
|
||||||
import { CreateTrack } from '../models/actions/CreateTrack';
|
import { CreateTrack } from '../models/actions/CreateTrack';
|
||||||
|
import { UpdateTrack } from '../models/actions/UpdateTrack';
|
||||||
import { Track } from '../models/entities/Track';
|
import { Track } from '../models/entities/Track';
|
||||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||||
import { ResponseTrack } from '../models/responses/ResponseTrack';
|
import { ResponseTrack } from '../models/responses/ResponseTrack';
|
||||||
@ -64,19 +64,19 @@ export class TrackController {
|
|||||||
@ResponseSchema(TrackIdsNotMatchingError, { statusCode: 406 })
|
@ResponseSchema(TrackIdsNotMatchingError, { statusCode: 406 })
|
||||||
@ResponseSchema(TrackLapTimeCantBeNegativeError, { statusCode: 406 })
|
@ResponseSchema(TrackLapTimeCantBeNegativeError, { statusCode: 406 })
|
||||||
@OpenAPI({ description: "Update the track whose id you provided. <br> Please remember that ids can't be changed." })
|
@OpenAPI({ description: "Update the track whose id you provided. <br> Please remember that ids can't be changed." })
|
||||||
async put(@Param('id') id: number, @EntityFromBody() track: Track) {
|
async put(@Param('id') id: number, @Body({ validate: true }) updateTrack: UpdateTrack) {
|
||||||
let oldTrack = await this.trackRepository.findOne({ id: id });
|
let oldTrack = await this.trackRepository.findOne({ id: id });
|
||||||
|
|
||||||
if (!oldTrack) {
|
if (!oldTrack) {
|
||||||
throw new TrackNotFoundError();
|
throw new TrackNotFoundError();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldTrack.id != track.id) {
|
if (oldTrack.id != updateTrack.id) {
|
||||||
throw new TrackIdsNotMatchingError();
|
throw new TrackIdsNotMatchingError();
|
||||||
}
|
}
|
||||||
|
await this.trackRepository.save(await updateTrack.updateTrack(oldTrack));
|
||||||
|
|
||||||
await this.trackRepository.save(track);
|
return new ResponseTrack(await this.trackRepository.findOne({ id: id }));
|
||||||
return new ResponseTrack(track);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete('/:id')
|
@Delete('/:id')
|
||||||
|
50
src/models/actions/UpdateTrack.ts
Normal file
50
src/models/actions/UpdateTrack.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import { IsInt, IsNotEmpty, IsOptional, IsPositive, IsString } from 'class-validator';
|
||||||
|
import { TrackLapTimeCantBeNegativeError } from '../../errors/TrackErrors';
|
||||||
|
import { Track } from '../entities/Track';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is used to update a Track entity (via put request).
|
||||||
|
*/
|
||||||
|
export class UpdateTrack {
|
||||||
|
/**
|
||||||
|
* The updated track's id.
|
||||||
|
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The updated track's distance in meters (must be greater than 0).
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
@IsPositive()
|
||||||
|
distance: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum time a runner should take to run a lap on this track.
|
||||||
|
* Will be used for fraud detection.
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
@IsOptional()
|
||||||
|
minimumLapTime: number;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a Track entity based on this.
|
||||||
|
* @param track The track that shall be updated.
|
||||||
|
*/
|
||||||
|
public updateTrack(track: Track): Track {
|
||||||
|
track.name = this.name;
|
||||||
|
track.distance = this.distance;
|
||||||
|
track.minimumLapTime = this.minimumLapTime;
|
||||||
|
if (this.minimumLapTime < 0) {
|
||||||
|
throw new TrackLapTimeCantBeNegativeError();
|
||||||
|
}
|
||||||
|
|
||||||
|
return track;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user