parent
36ecae7e6e
commit
32fda46f0a
@ -1,9 +1,10 @@
|
|||||||
import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, QueryParam } from 'routing-controllers';
|
import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, 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, RunnerCardIdsNotMatchingError, RunnerCardNotFoundError } from '../errors/RunnerCardErrors';
|
||||||
import { RunnerNotFoundError } from '../errors/RunnerErrors';
|
import { RunnerNotFoundError } from '../errors/RunnerErrors';
|
||||||
import { CreateRunnerCard } from '../models/actions/CreateRunnerCard';
|
import { CreateRunnerCard } from '../models/actions/CreateRunnerCard';
|
||||||
|
import { UpdateRunnerCard } from '../models/actions/UpdateRunnerCard';
|
||||||
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';
|
||||||
@ -57,27 +58,27 @@ export class RunnerCardController {
|
|||||||
return (await this.cardRepository.findOne({ id: card.id }, { relations: ['runner'] })).toResponse();
|
return (await this.cardRepository.findOne({ id: card.id }, { relations: ['runner'] })).toResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Put('/:id')
|
@Put('/:id')
|
||||||
// @Authorized("CARD:UPDATE")
|
@Authorized("CARD:UPDATE")
|
||||||
// @ResponseSchema(ResponseTrack)
|
@ResponseSchema(ResponseRunnerCard)
|
||||||
// @ResponseSchema(TrackNotFoundError, { statusCode: 404 })
|
@ResponseSchema(RunnerCardNotFoundError, { statusCode: 404 })
|
||||||
// @ResponseSchema(TrackIdsNotMatchingError, { statusCode: 406 })
|
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
|
||||||
// @ResponseSchema(TrackLapTimeCantBeNegativeError, { statusCode: 406 })
|
@ResponseSchema(RunnerCardIdsNotMatchingError, { statusCode: 406 })
|
||||||
// @OpenAPI({ description: "Update the track whose id you provided. <br> Please remember that ids can't be changed." })
|
@OpenAPI({ description: "Update the card whose id you provided. <br> Scans created via this card will still be associated with the old runner. <br> Please remember that ids can't be changed." })
|
||||||
// async put(@Param('id') id: number, @Body({ validate: true }) updateTrack: UpdateTrack) {
|
async put(@Param('id') id: number, @Body({ validate: true }) card: UpdateRunnerCard) {
|
||||||
// let oldTrack = await this.trackRepository.findOne({ id: id });
|
let oldCard = await this.cardRepository.findOne({ id: id });
|
||||||
|
|
||||||
// if (!oldTrack) {
|
if (!oldCard) {
|
||||||
// throw new TrackNotFoundError();
|
throw new RunnerCardNotFoundError();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if (oldTrack.id != updateTrack.id) {
|
if (oldCard.id != card.id) {
|
||||||
// throw new TrackIdsNotMatchingError();
|
throw new RunnerCardIdsNotMatchingError();
|
||||||
// }
|
}
|
||||||
// await this.trackRepository.save(await updateTrack.updateTrack(oldTrack));
|
|
||||||
|
|
||||||
// return new ResponseTrack(await this.trackRepository.findOne({ id: id }));
|
await this.cardRepository.save(await card.update(oldCard));
|
||||||
// }
|
return (await this.cardRepository.findOne({ id: id }, { relations: ['runner'] })).toResponse();
|
||||||
|
}
|
||||||
|
|
||||||
@Delete('/:id')
|
@Delete('/:id')
|
||||||
@Authorized("CARD:DELETE")
|
@Authorized("CARD:DELETE")
|
||||||
|
51
src/models/actions/UpdateRunnerCard.ts
Normal file
51
src/models/actions/UpdateRunnerCard.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import { IsBoolean, IsInt, IsOptional, IsPositive } 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 UpdateRunnerCard {
|
||||||
|
/**
|
||||||
|
* The updated card's id.
|
||||||
|
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
@IsPositive()
|
||||||
|
id?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The updated card's associated runner.
|
||||||
|
*/
|
||||||
|
@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