Added runner card get endpoints

ref #77
This commit is contained in:
Nicolai Ort 2021-01-09 11:23:12 +01:00
parent 98f7bf366f
commit 4faeddc3f3
2 changed files with 52 additions and 25 deletions

View File

@ -1,7 +1,9 @@
import { JsonController } from 'routing-controllers'; import { Authorized, Get, JsonController, OnUndefined, Param } from 'routing-controllers';
import { OpenAPI } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { RunnerCardNotFoundError } from '../errors/RunnerCardErrors';
import { RunnerCard } from '../models/entities/RunnerCard'; import { RunnerCard } from '../models/entities/RunnerCard';
import { ResponseRunnerCard } from '../models/responses/ResponseRunnerCard';
@JsonController('/cards') @JsonController('/cards')
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) @OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
@ -15,30 +17,30 @@ export class RunnerCardController {
this.cardRepository = getConnectionManager().get().getRepository(RunnerCard); this.cardRepository = getConnectionManager().get().getRepository(RunnerCard);
} }
// @Get() @Get()
// @Authorized("CARD:GET") @Authorized("CARD:GET")
// @ResponseSchema(ResponseTrack, { isArray: true }) @ResponseSchema(ResponseRunnerCard, { isArray: true })
// @OpenAPI({ description: 'Lists all tracks.' }) @OpenAPI({ description: 'Lists all card.' })
// async getAll() { async getAll() {
// let responseTracks: ResponseTrack[] = new Array<ResponseTrack>(); let responseCards: ResponseRunnerCard[] = new Array<ResponseRunnerCard>();
// const tracks = await this.trackRepository.find(); const cards = await this.cardRepository.find({ relations: ['runner'] });
// tracks.forEach(track => { cards.forEach(card => {
// responseTracks.push(new ResponseTrack(track)); responseCards.push(new ResponseRunnerCard(card));
// }); });
// return responseTracks; return responseCards;
// } }
// @Get('/:id') @Get('/:id')
// @Authorized("CARD:GET") @Authorized("CARD:GET")
// @ResponseSchema(ResponseTrack) @ResponseSchema(ResponseRunnerCard)
// @ResponseSchema(TrackNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerCardNotFoundError, { statusCode: 404 })
// @OnUndefined(TrackNotFoundError) @OnUndefined(RunnerCardNotFoundError)
// @OpenAPI({ description: "Lists all information about the track whose id got provided." }) @OpenAPI({ description: "Lists all information about the card whose id got provided." })
// async getOne(@Param('id') id: number) { async getOne(@Param('id') id: number) {
// let track = await this.trackRepository.findOne({ id: id }); let card = await this.cardRepository.findOne({ id: id }, { relations: ['runner'] });
// if (!track) { throw new TrackNotFoundError(); } if (!card) { throw new RunnerCardNotFoundError(); }
// return new ResponseTrack(track); return card.toResponse();
// } }
// @Post() // @Post()
// @Authorized("CARD:CREATE") // @Authorized("CARD:CREATE")

View File

@ -0,0 +1,25 @@
import { IsString } from 'class-validator';
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
/**
* Error to throw when a card couldn't be found.
*/
export class RunnerCardNotFoundError extends NotFoundError {
@IsString()
name = "RunnerCardNotFoundError"
@IsString()
message = "Card not found!"
}
/**
* Error to throw when two cards' ids don't match.
* Usually occurs when a user tries to change a card's id.
*/
export class RunnerCardIdsNotMatchingError extends NotAcceptableError {
@IsString()
name = "RunnerCardIdsNotMatchingError"
@IsString()
message = "The ids don't match! \n And if you wanted to change a cards's id: This isn't allowed"
}