feat(RunnerCardController): putByCode
This commit is contained in:
parent
03e0a29096
commit
8237d5f210
@ -5,6 +5,7 @@ import { RunnerCardHasScansError, RunnerCardIdsNotMatchingError, RunnerCardNotFo
|
|||||||
import { RunnerNotFoundError } from '../errors/RunnerErrors';
|
import { RunnerNotFoundError } from '../errors/RunnerErrors';
|
||||||
import { CreateRunnerCard } from '../models/actions/create/CreateRunnerCard';
|
import { CreateRunnerCard } from '../models/actions/create/CreateRunnerCard';
|
||||||
import { UpdateRunnerCard } from '../models/actions/update/UpdateRunnerCard';
|
import { UpdateRunnerCard } from '../models/actions/update/UpdateRunnerCard';
|
||||||
|
import { UpdateRunnerCardByCode } from '../models/actions/update/UpdateRunnerCardByCode';
|
||||||
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';
|
||||||
@ -112,6 +113,28 @@ export class RunnerCardController {
|
|||||||
return (await this.cardRepository.findOne({ id: id }, { relations: ['runner', 'runner.group', 'runner.group.parentGroup'] })).toResponse();
|
return (await this.cardRepository.findOne({ id: id }, { relations: ['runner', 'runner.group', 'runner.group.parentGroup'] })).toResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Put('/:code')
|
||||||
|
@Authorized("CARD:UPDATE")
|
||||||
|
@ResponseSchema(ResponseRunnerCard)
|
||||||
|
@ResponseSchema(RunnerCardNotFoundError, { statusCode: 404 })
|
||||||
|
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
|
||||||
|
@ResponseSchema(RunnerCardIdsNotMatchingError, { statusCode: 406 })
|
||||||
|
@OpenAPI({ description: "Update the card whose code you provided." })
|
||||||
|
async putByCode(@Param('code') code: string, @Body({ validate: true }) card: UpdateRunnerCardByCode) {
|
||||||
|
let oldCard = await this.cardRepository.findOne({ code: code });
|
||||||
|
|
||||||
|
if (!oldCard) {
|
||||||
|
throw new RunnerCardNotFoundError();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldCard.code != card.code) {
|
||||||
|
throw new RunnerCardIdsNotMatchingError();
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.cardRepository.save(await card.update(oldCard));
|
||||||
|
return (await this.cardRepository.findOne({ code: code }, { relations: ['runner', 'runner.group', 'runner.group.parentGroup'] })).toResponse();
|
||||||
|
}
|
||||||
|
|
||||||
@Delete('/:id')
|
@Delete('/:id')
|
||||||
@Authorized("CARD:DELETE")
|
@Authorized("CARD:DELETE")
|
||||||
@ResponseSchema(ResponseRunnerCard)
|
@ResponseSchema(ResponseRunnerCard)
|
||||||
|
50
src/models/actions/update/UpdateRunnerCardByCode.ts
Normal file
50
src/models/actions/update/UpdateRunnerCardByCode.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import { IsBoolean, IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||||
|
import { getConnection } from 'typeorm';
|
||||||
|
import { RunnerNotFoundError } from '../../../errors/RunnerErrors';
|
||||||
|
import { Runner } from '../../entities/Runner';
|
||||||
|
import { RunnerCard } from '../../entities/RunnerCard';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is used to update a RunnerCard entity (via put request).
|
||||||
|
*/
|
||||||
|
export class UpdateRunnerCardByCode {
|
||||||
|
/**
|
||||||
|
* The card's code.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
code?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The runner's id.
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
@IsOptional()
|
||||||
|
runner?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the updated card enabled (for fraud reasons)?
|
||||||
|
* Default: true
|
||||||
|
*/
|
||||||
|
@IsBoolean()
|
||||||
|
enabled: boolean = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new RunnerCard entity from this.
|
||||||
|
*/
|
||||||
|
public async update(card: RunnerCard): Promise<RunnerCard> {
|
||||||
|
card.enabled = this.enabled;
|
||||||
|
card.runner = await this.getRunner();
|
||||||
|
|
||||||
|
return card;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user