From 2628f6965165de7abbc100ed729b8d1f9c8c422d Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 7 Jan 2021 18:39:38 +0100 Subject: [PATCH] Implemented scan station creation ref #67 --- src/controllers/ScanStationController.ts | 25 ++++++++++++++---------- src/models/actions/CreateScanStation.ts | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/controllers/ScanStationController.ts b/src/controllers/ScanStationController.ts index fc13028..536c265 100644 --- a/src/controllers/ScanStationController.ts +++ b/src/controllers/ScanStationController.ts @@ -1,7 +1,9 @@ -import { Authorized, Get, JsonController, OnUndefined, Param } from 'routing-controllers'; +import { Authorized, Body, Get, JsonController, OnUndefined, Param, Post } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { ScanStationNotFoundError } from '../errors/ScanStationErrors'; +import { TrackNotFoundError } from '../errors/TrackErrors'; +import { CreateScanStation } from '../models/actions/CreateScanStation'; import { ScanStation } from '../models/entities/ScanStation'; import { ResponseScanStation } from '../models/responses/ResponseScanStation'; @@ -42,15 +44,18 @@ export class ScanStationController { return scan.toResponse(); } - // @Post() - // @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) { - // let scan = await createScan.toScan(); - // scan = await this.scanRepository.save(scan); - // return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner'] })).toResponse(); - // } + @Post() + @Authorized("STATION:CREATE") + @ResponseSchema(ResponseScanStation) + @ResponseSchema(TrackNotFoundError, { statusCode: 404 }) + @OpenAPI({ description: 'Create a new runner.
Please remeber to provide the runner\'s group\'s id.' }) + async post(@Body({ validate: true }) createStation: CreateScanStation) { + let newStation = await createStation.toEntity(); + const station = await this.stationRepository.save(newStation); + let responseStation = (await this.stationRepository.findOne({ id: station.id }, { relations: ['track'] })).toResponse(); + responseStation.key = newStation.cleartextkey; + return responseStation; + } // @Put('/:id') // @Authorized("SCAN:UPDATE") diff --git a/src/models/actions/CreateScanStation.ts b/src/models/actions/CreateScanStation.ts index fd1d8ad..edd19cc 100644 --- a/src/models/actions/CreateScanStation.ts +++ b/src/models/actions/CreateScanStation.ts @@ -10,7 +10,7 @@ import { Track } from '../entities/Track'; /** * This classed is used to create a new StatsClient entity from a json body (post request). */ -export class CreateStatsClient { +export class CreateScanStation { /** * The new client's description. */