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