Added card creation

#17
This commit is contained in:
Nicolai Ort 2021-01-09 11:48:13 +01:00
parent a5bfe4e3d5
commit 36ecae7e6e
4 changed files with 72 additions and 29 deletions

View File

@ -1,7 +1,9 @@
import { Authorized, Delete, Get, JsonController, OnUndefined, Param, QueryParam } from 'routing-controllers'; import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { RunnerCardHasScansError, RunnerCardNotFoundError } from '../errors/RunnerCardErrors'; import { RunnerCardHasScansError, RunnerCardNotFoundError } from '../errors/RunnerCardErrors';
import { RunnerNotFoundError } from '../errors/RunnerErrors';
import { CreateRunnerCard } from '../models/actions/CreateRunnerCard';
import { RunnerCard } from '../models/entities/RunnerCard'; import { RunnerCard } from '../models/entities/RunnerCard';
import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { ResponseRunnerCard } from '../models/responses/ResponseRunnerCard'; import { ResponseRunnerCard } from '../models/responses/ResponseRunnerCard';
@ -44,17 +46,16 @@ export class RunnerCardController {
return card.toResponse(); return card.toResponse();
} }
// @Post() @Post()
// @Authorized("CARD:CREATE") @Authorized("CARD:CREATE")
// @ResponseSchema(ResponseTrack) @ResponseSchema(ResponseRunnerCard)
// @ResponseSchema(TrackLapTimeCantBeNegativeError, { statusCode: 406 }) @ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
// @OpenAPI({ description: "Create a new track. <br> Please remember that the track\'s distance must be greater than 0." }) @OpenAPI({ description: "Create a new card. <br> You can provide a associated runner by id but you don't have to." })
// async post( async post(@Body({ validate: true }) createCard: CreateRunnerCard) {
// @Body({ validate: true }) let card = await createCard.toEntity();
// track: CreateTrack card = await this.cardRepository.save(card);
// ) { return (await this.cardRepository.findOne({ id: card.id }, { relations: ['runner'] })).toResponse();
// return new ResponseTrack(await this.trackRepository.save(track.toTrack())); }
// }
// @Put('/:id') // @Put('/:id')
// @Authorized("CARD:UPDATE") // @Authorized("CARD:UPDATE")

View File

@ -0,0 +1,45 @@
import { IsBoolean, IsInt, IsOptional } from 'class-validator';
import { getConnection } from 'typeorm';
import { RunnerNotFoundError } from '../../errors/RunnerErrors';
import { Runner } from '../entities/Runner';
import { RunnerCard } from '../entities/RunnerCard';
/**
* This classed is used to create a new RunnerCard entity from a json body (post request).
*/
export class CreateRunnerCard {
/**
* The card's associated runner.
*/
@IsInt()
@IsOptional()
runner?: number;
/**
* Is the new card enabled (for fraud reasons)?
* Default: true
*/
@IsBoolean()
enabled: boolean = true;
/**
* Creates a new RunnerCard entity from this.
*/
public async toEntity(): Promise<RunnerCard> {
let newCard: RunnerCard = new RunnerCard();
newCard.enabled = this.enabled;
newCard.runner = await this.getRunner();
return newCard;
}
public async getRunner(): Promise<Runner> {
if (!this.runner) { return null; }
const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner });
if (!runner) {
throw new RunnerNotFoundError();
}
return runner;
}
}

View File

@ -1,10 +1,9 @@
import { import {
IsBoolean, IsBoolean,
IsEAN,
IsInt, IsInt,
IsNotEmpty,
IsOptional, IsOptional
IsString
} from "class-validator"; } from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { ResponseRunnerCard } from '../responses/ResponseRunnerCard'; import { ResponseRunnerCard } from '../responses/ResponseRunnerCard';
@ -33,17 +32,6 @@ export class RunnerCard {
@ManyToOne(() => Runner, runner => runner.cards, { nullable: true }) @ManyToOne(() => Runner, runner => runner.cards, { nullable: true })
runner: Runner; runner: Runner;
/**
* The card's code.
* This has to be able to being converted to something barcode compatible.
* Will get automaticlly generated (not implemented yet).
*/
@Column()
@IsEAN()
@IsString()
@IsNotEmpty()
code: string;
/** /**
* Is the card enabled (for fraud reasons)? * Is the card enabled (for fraud reasons)?
* Default: true * Default: true
@ -59,6 +47,14 @@ export class RunnerCard {
@OneToMany(() => TrackScan, scan => scan.track, { nullable: true }) @OneToMany(() => TrackScan, scan => scan.track, { nullable: true })
scans: TrackScan[]; scans: TrackScan[];
/**
* Generates a ean-13 compliant string for barcode generation.
*/
public get code(): string {
//TODO: Implement the real deal
return '0000000000000'
}
/** /**
* Turns this entity into it's response class. * Turns this entity into it's response class.
*/ */

View File

@ -17,7 +17,7 @@ export class ResponseRunnerCard {
* This is important to link scans to runners. * This is important to link scans to runners.
*/ */
@IsObject() @IsObject()
runner: ResponseRunner; runner: ResponseRunner | null;
/** /**
* The card's code. * The card's code.
@ -40,7 +40,8 @@ export class ResponseRunnerCard {
*/ */
public constructor(card: RunnerCard) { public constructor(card: RunnerCard) {
this.id = card.id; this.id = card.id;
this.runner = card.runner.toResponse() || null; if (!card.runner) { this.runner = null }
else { this.runner = card.runner.toResponse(); }
this.code = card.code; this.code = card.code;
this.enabled = card.enabled; this.enabled = card.enabled;
} }