Added the enabled flag for scanstations

ref #67
This commit is contained in:
Nicolai Ort 2021-01-07 19:37:15 +01:00
parent 09b37f0ff2
commit 4f01baaa23
6 changed files with 86 additions and 22 deletions

View File

@ -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. <br> 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();

View File

@ -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. <br> 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. <br> 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")

View File

@ -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();

View File

@ -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<ScanStation> {
station.description = this.description;
station.enabled = this.enabled;
return station;
}
}

View File

@ -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.
*/

View File

@ -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;
}
}