parent
88a6a768c4
commit
eec5284306
@ -1,8 +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 { ScanNotFoundError } from '../errors/ScanErrors';
|
import { RunnerNotFoundError } from '../errors/RunnerErrors';
|
||||||
|
import { ScanIdsNotMatchingError, ScanNotFoundError } from '../errors/ScanErrors';
|
||||||
import { CreateScan } from '../models/actions/CreateScan';
|
import { CreateScan } from '../models/actions/CreateScan';
|
||||||
|
import { UpdateScan } from '../models/actions/UpdateScan';
|
||||||
import { Scan } from '../models/entities/Scan';
|
import { Scan } from '../models/entities/Scan';
|
||||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||||
import { ResponseScan } from '../models/responses/ResponseScan';
|
import { ResponseScan } from '../models/responses/ResponseScan';
|
||||||
@ -57,26 +59,27 @@ export class ScanController {
|
|||||||
return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner'] })).toResponse();
|
return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner'] })).toResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Put('/:id')
|
@Put('/:id')
|
||||||
// @Authorized("DONOR:UPDATE")
|
@Authorized("SCAN:UPDATE")
|
||||||
// @ResponseSchema(ResponseDonor)
|
@ResponseSchema(ResponseScan)
|
||||||
// @ResponseSchema(DonorNotFoundError, { statusCode: 404 })
|
@ResponseSchema(ScanNotFoundError, { statusCode: 404 })
|
||||||
// @ResponseSchema(DonorIdsNotMatchingError, { statusCode: 406 })
|
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
|
||||||
// @OpenAPI({ description: "Update the runner whose id you provided. <br> Please remember that ids can't be changed." })
|
@ResponseSchema(ScanIdsNotMatchingError, { statusCode: 406 })
|
||||||
// async put(@Param('id') id: number, @Body({ validate: true }) donor: UpdateDonor) {
|
@OpenAPI({ description: "Update the runner whose id you provided. <br> Please remember that ids can't be changed." })
|
||||||
// let oldDonor = await this.donorRepository.findOne({ id: id });
|
async put(@Param('id') id: number, @Body({ validate: true }) scan: UpdateScan) {
|
||||||
|
let oldScan = await this.scanRepository.findOne({ id: id });
|
||||||
|
|
||||||
// if (!oldDonor) {
|
if (!oldScan) {
|
||||||
// throw new DonorNotFoundError();
|
throw new ScanNotFoundError();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if (oldDonor.id != donor.id) {
|
if (oldScan.id != scan.id) {
|
||||||
// throw new DonorIdsNotMatchingError();
|
throw new ScanIdsNotMatchingError();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// await this.donorRepository.save(await donor.updateDonor(oldDonor));
|
await this.scanRepository.save(await scan.updateScan(oldScan));
|
||||||
// return new ResponseDonor(await this.donorRepository.findOne({ id: id }));
|
return (await this.scanRepository.findOne({ id: id }, { relations: ['runner'] })).toResponse();
|
||||||
// }
|
}
|
||||||
|
|
||||||
@Delete('/:id')
|
@Delete('/:id')
|
||||||
@Authorized("SCAN:DELETE")
|
@Authorized("SCAN:DELETE")
|
||||||
|
59
src/models/actions/UpdateScan.ts
Normal file
59
src/models/actions/UpdateScan.ts
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import { IsBoolean, IsInt, IsOptional, IsPositive } from 'class-validator';
|
||||||
|
import { getConnection } from 'typeorm';
|
||||||
|
import { RunnerNotFoundError } from '../../errors/RunnerErrors';
|
||||||
|
import { Runner } from '../entities/Runner';
|
||||||
|
import { Scan } from '../entities/Scan';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This classed is used to create a new Scan entity from a json body (post request).
|
||||||
|
*/
|
||||||
|
export abstract class UpdateScan {
|
||||||
|
/**
|
||||||
|
* The updated scan'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()
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The updated scan's associated runner.
|
||||||
|
* This is important to link ran distances to runners.
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
@IsPositive()
|
||||||
|
runner: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the updated scan valid (for fraud reasons).
|
||||||
|
*/
|
||||||
|
@IsBoolean()
|
||||||
|
@IsOptional()
|
||||||
|
valid?: boolean = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The updated scan's distance in meters.
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
@IsPositive()
|
||||||
|
public distance: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a Scan entity based on this.
|
||||||
|
* @param scan The scan that shall be updated.
|
||||||
|
*/
|
||||||
|
public async updateScan(scan: Scan): Promise<Scan> {
|
||||||
|
scan.distance = this.distance;
|
||||||
|
scan.valid = this.valid;
|
||||||
|
scan.runner = await this.getRunner();
|
||||||
|
|
||||||
|
return scan;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getRunner(): Promise<Runner> {
|
||||||
|
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