From 500b94b44afc27df2bbbaab50390fdf7e7fb7d14 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 29 Dec 2020 20:01:40 +0100 Subject: [PATCH] Added a controller for stats clients (todo: put) ref #56 --- src/controllers/StatsClientController.ts | 97 ++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/controllers/StatsClientController.ts diff --git a/src/controllers/StatsClientController.ts b/src/controllers/StatsClientController.ts new file mode 100644 index 0000000..7ba6360 --- /dev/null +++ b/src/controllers/StatsClientController.ts @@ -0,0 +1,97 @@ +import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post } from 'routing-controllers'; +import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; +import { getConnectionManager, Repository } from 'typeorm'; +import { StatsClientNotFoundError } from '../errors/StatsClientErrors'; +import { TrackNotFoundError } from "../errors/TrackErrors"; +import { CreateStatsClient } from '../models/actions/CreateStatsClient'; +import { StatsClient } from '../models/entities/StatsClient'; +import { ResponseEmpty } from '../models/responses/ResponseEmpty'; +import { ResponseStatsClient } from '../models/responses/ResponseStatsClient'; + +@JsonController('/statsclients') +@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) +export class TrackController { + private clientRepository: Repository; + + /** + * Gets the repository of this controller's model/entity. + */ + constructor() { + this.clientRepository = getConnectionManager().get().getRepository(StatsClient); + } + + @Get() + @Authorized("STATSCLIENT:GET") + @ResponseSchema(ResponseStatsClient, { isArray: true }) + @OpenAPI({ description: 'Lists all stats clients. Please remember that the key can only be viewed on creation and update.' }) + async getAll() { + let responseClients: ResponseStatsClient[] = new Array(); + const clients = await this.clientRepository.find(); + clients.forEach(clients => { + responseClients.push(new ResponseStatsClient(clients)); + }); + return responseClients; + } + + @Get('/:id') + @Authorized("STATSCLIENT:GET") + @ResponseSchema(ResponseStatsClient) + @ResponseSchema(StatsClientNotFoundError, { statusCode: 404 }) + @OnUndefined(StatsClientNotFoundError) + @OpenAPI({ description: "Lists all information about the stats client whose id got provided. Please remember that the key can only be viewed on creation and update" }) + async getOne(@Param('id') id: number) { + let client = await this.clientRepository.findOne({ id: id }); + if (!client) { throw new TrackNotFoundError(); } + return new ResponseStatsClient(client); + } + + @Post() + @Authorized("STATSCLIENT:CREATE") + @ResponseSchema(ResponseStatsClient) + @OpenAPI({ description: "Create a new stats client.
Please remember that the client\'s key will be generated automaticly and that it can only be viewed on creation and update." }) + async post( + @Body({ validate: true }) + client: CreateStatsClient + ) { + let newClient = await this.clientRepository.save(client.toStatsClient()); + let responseClient = new ResponseStatsClient(newClient); + responseClient.key = newClient.key; + return responseClient; + } + + + // @Put('/:id') + // @Authorized("STATSCLIENT:UPDATE") + // @ResponseSchema(ResponseStatsClient) + // @ResponseSchema(StatsClientNotFoundError, { statusCode: 404 }) + // @ResponseSchema(StatsClientIdsNotMatchingError, { statusCode: 406 }) + // @OpenAPI({ description: "Update the stats client 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 }); + + // if (!oldTrack) { + // throw new StatsClientNotFoundError(); + // } + + // if (oldTrack.id != track.id) { + // throw new StatsClientIdsNotMatchingError(); + // } + + // await this.trackRepository.save(track); + // return new ResponseTrack(track); + // } + + @Delete('/:id') + @Authorized("STATSCLIENT:DELETE") + @ResponseSchema(ResponseStatsClient) + @ResponseSchema(ResponseEmpty, { statusCode: 204 }) + @OnUndefined(204) + @OpenAPI({ description: "Delete the stats client whose id you provided.
If no client with this id exists it will just return 204(no content)." }) + async remove(@Param("id") id: number) { + let client = await this.clientRepository.findOne({ id: id }); + if (!client) { return null; } + + await this.clientRepository.delete(client); + return new ResponseStatsClient(client); + } +} \ No newline at end of file