diff --git a/src/controllers/ScanController.ts b/src/controllers/ScanController.ts index 6674f36..c18a0eb 100644 --- a/src/controllers/ScanController.ts +++ b/src/controllers/ScanController.ts @@ -4,6 +4,7 @@ import { getConnectionManager, Repository } from 'typeorm'; import { RunnerNotFoundError } from '../errors/RunnerErrors'; import { ScanIdsNotMatchingError, ScanNotFoundError } from '../errors/ScanErrors'; import { CreateScan } from '../models/actions/CreateScan'; +import { CreateTrackScan } from '../models/actions/CreateTrackScan'; import { UpdateScan } from '../models/actions/UpdateScan'; import { Scan } from '../models/entities/Scan'; import { ResponseEmpty } from '../models/responses/ResponseEmpty'; @@ -53,7 +54,7 @@ export class ScanController { @Authorized("SCAN:CREATE") @ResponseSchema(ResponseScan) @OpenAPI({ description: 'Create a new runner.
Please remeber to provide the runner\'s group\'s id.' }) - async post(@Body({ validate: true }) createScan: CreateScan) { + async post(@Body({ validate: true }) createScan: CreateScan | CreateTrackScan) { let scan = await createScan.toScan(); scan = await this.scanRepository.save(scan); return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner'] })).toResponse(); diff --git a/src/controllers/ScanStationController.ts b/src/controllers/ScanStationController.ts index d6edcd3..e85f31e 100644 --- a/src/controllers/ScanStationController.ts +++ b/src/controllers/ScanStationController.ts @@ -1,9 +1,10 @@ -import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, QueryParam } from 'routing-controllers'; +import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; -import { ScanStationHasScansError, ScanStationNotFoundError } from '../errors/ScanStationErrors'; +import { ScanStationHasScansError, ScanStationIdsNotMatchingError, ScanStationNotFoundError } from '../errors/ScanStationErrors'; import { TrackNotFoundError } from '../errors/TrackErrors'; import { CreateScanStation } from '../models/actions/CreateScanStation'; +import { UpdateScanStation } from '../models/actions/UpdateScanStation'; import { ScanStation } from '../models/entities/ScanStation'; import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseScanStation } from '../models/responses/ResponseScanStation'; @@ -59,27 +60,26 @@ export class ScanStationController { return responseStation; } - // @Put('/:id') - // @Authorized("SCAN:UPDATE") - // @ResponseSchema(ResponseScan) - // @ResponseSchema(ScanNotFoundError, { statusCode: 404 }) - // @ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) - // @ResponseSchema(ScanIdsNotMatchingError, { statusCode: 406 }) - // @OpenAPI({ description: "Update the runner whose id you provided.
Please remember that ids can't be changed." }) - // async put(@Param('id') id: number, @Body({ validate: true }) scan: UpdateScan) { - // let oldScan = await this.scanRepository.findOne({ id: id }); + @Put('/:id') + @Authorized("STATION:UPDATE") + @ResponseSchema(ResponseScanStation) + @ResponseSchema(ScanStationNotFoundError, { statusCode: 404 }) + @ResponseSchema(ScanStationIdsNotMatchingError, { statusCode: 406 }) + @OpenAPI({ description: "Update the station whose id you provided.
Please remember that only the description and enabled state can be changed." }) + async put(@Param('id') id: number, @Body({ validate: true }) station: UpdateScanStation) { + let oldStation = await this.stationRepository.findOne({ id: id }); - // if (!oldScan) { - // throw new ScanNotFoundError(); - // } + if (!oldStation) { + throw new ScanStationNotFoundError(); + } - // if (oldScan.id != scan.id) { - // throw new ScanIdsNotMatchingError(); - // } + if (oldStation.id != station.id) { + throw new ScanStationNotFoundError(); + } - // await this.scanRepository.save(await scan.updateScan(oldScan)); - // return (await this.scanRepository.findOne({ id: id }, { relations: ['runner'] })).toResponse(); - // } + await this.stationRepository.save(await station.updateStation(oldStation)); + return (await this.stationRepository.findOne({ id: id }, { relations: ['track'] })).toResponse(); + } @Delete('/:id') @Authorized("STATION:DELETE") diff --git a/src/models/actions/CreateScanStation.ts b/src/models/actions/CreateScanStation.ts index edd19cc..76d414a 100644 --- a/src/models/actions/CreateScanStation.ts +++ b/src/models/actions/CreateScanStation.ts @@ -1,5 +1,5 @@ import * as argon2 from "argon2"; -import { IsInt, IsOptional, IsPositive, IsString } from 'class-validator'; +import { IsBoolean, IsInt, IsOptional, IsPositive, IsString } from 'class-validator'; import crypto from 'crypto'; import { getConnection } from 'typeorm'; import * as uuid from 'uuid'; @@ -26,6 +26,13 @@ export class CreateScanStation { @IsPositive() track: number; + /** + * Is this station enabled? + */ + @IsBoolean() + @IsOptional() + enabled?: boolean = true; + /** * Converts this to a ScanStation entity. */ @@ -33,6 +40,7 @@ export class CreateScanStation { let newStation: ScanStation = new ScanStation(); newStation.description = this.description; + newStation.enabled = this.enabled; newStation.track = await this.getTrack(); let newUUID = uuid.v4().toUpperCase(); diff --git a/src/models/actions/UpdateScanStation.ts b/src/models/actions/UpdateScanStation.ts new file mode 100644 index 0000000..24039a0 --- /dev/null +++ b/src/models/actions/UpdateScanStation.ts @@ -0,0 +1,39 @@ +import { IsBoolean, IsInt, IsOptional, IsString } from 'class-validator'; +import { ScanStation } from '../entities/ScanStation'; + +/** + * This classed is used to create a new StatsClient entity from a json body (post request). + */ +export class UpdateScanStation { + /** + * The updated station'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; + + /** + * The updated station's description. + */ + @IsString() + @IsOptional() + description?: string; + + /** + * Is this station enabled? + */ + @IsBoolean() + @IsOptional() + enabled?: boolean = true; + + /** + * Converts this to a ScanStation entity. + * TODO: + */ + public async updateStation(station: ScanStation): Promise { + station.description = this.description; + station.enabled = this.enabled; + + return station; + } +} \ No newline at end of file diff --git a/src/models/entities/ScanStation.ts b/src/models/entities/ScanStation.ts index 93fde64..38f7803 100644 --- a/src/models/entities/ScanStation.ts +++ b/src/models/entities/ScanStation.ts @@ -1,4 +1,5 @@ import { + IsBoolean, IsInt, IsNotEmpty, IsOptional, @@ -70,6 +71,13 @@ export class ScanStation { @OneToMany(() => TrackScan, scan => scan.track, { nullable: true }) scans: TrackScan[]; + /** + * Is this station enabled? + */ + @Column({ nullable: true }) + @IsBoolean() + enabled?: boolean = true; + /** * Turns this entity into it's response class. */ diff --git a/src/models/responses/ResponseScanStation.ts b/src/models/responses/ResponseScanStation.ts index 6b147c9..7d7dc48 100644 --- a/src/models/responses/ResponseScanStation.ts +++ b/src/models/responses/ResponseScanStation.ts @@ -1,5 +1,6 @@ import { + IsBoolean, IsInt, IsNotEmpty, @@ -48,6 +49,12 @@ export class ResponseScanStation { @IsNotEmpty() track: ResponseTrack; + /** + * Is this station enabled? + */ + @IsBoolean() + enabled?: boolean = true; + /** * Creates a ResponseStatsClient object from a statsClient. * @param client The statsClient the response shall be build for. @@ -58,5 +65,6 @@ export class ResponseScanStation { this.prefix = station.prefix; this.key = "Only visible on creation."; this.track = station.track; + this.enabled = station.enabled; } }