From 57f62a608764914c5a213f0c2aebde0d67df70e0 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 12 Jan 2021 18:46:02 +0100 Subject: [PATCH] Implemented donation deletion ref #66 --- src/controllers/DonationController.ts | 30 ++++++++++++++------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/controllers/DonationController.ts b/src/controllers/DonationController.ts index ab5e9f0..c31c84e 100644 --- a/src/controllers/DonationController.ts +++ b/src/controllers/DonationController.ts @@ -1,4 +1,4 @@ -import { Authorized, Get, JsonController, OnUndefined, Param } from 'routing-controllers'; +import { Authorized, Delete, Get, JsonController, OnUndefined, Param, QueryParam } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { DonationNotFoundError } from '../errors/DonationErrors'; @@ -6,6 +6,7 @@ import { DistanceDonation } from '../models/entities/DistanceDonation'; import { Donation } from '../models/entities/Donation'; import { ResponseDistanceDonation } from '../models/responses/ResponseDistanceDonation'; import { ResponseDonation } from '../models/responses/ResponseDonation'; +import { ResponseEmpty } from '../models/responses/ResponseEmpty'; @JsonController('/donations') @OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) @@ -115,18 +116,19 @@ export class DonationController { // return (await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.scans.track', 'card', 'station'] })).toResponse(); // } - // @Delete('/:id') - // @Authorized("SCAN:DELETE") - // @ResponseSchema(ResponseScan) - // @ResponseSchema(ResponseEmpty, { statusCode: 204 }) - // @OnUndefined(204) - // @OpenAPI({ description: 'Delete the scan whose id you provided.
If no scan with this id exists it will just return 204(no content).' }) - // async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { - // let scan = await this.scanRepository.findOne({ id: id }); - // if (!scan) { return null; } - // const responseScan = await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.scans.track', 'card', 'station'] }); + @Delete('/:id') + @Authorized("DONATION:DELETE") + @ResponseSchema(ResponseDonation) + @ResponseSchema(ResponseDistanceDonation) + @ResponseSchema(ResponseEmpty, { statusCode: 204 }) + @OnUndefined(204) + @OpenAPI({ description: 'Delete the donation whose id you provided.
If no donation with this id exists it will just return 204(no content).' }) + async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { + let donation = await this.donationRepository.findOne({ id: id }); + if (!donation) { return null; } + const responseScan = await this.donationRepository.findOne({ id: donation.id }, { relations: ['runner', 'donor', 'runner.scans', 'runner.scans.track'] }); - // await this.scanRepository.delete(scan); - // return responseScan.toResponse(); - // } + await this.donationRepository.delete(donation); + return responseScan.toResponse(); + } }