From 9517df50826aff1a1cb03e611b990de5829e2132 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 12 Jan 2021 19:00:35 +0100 Subject: [PATCH] Implemented fixed donation updateing ref #66 --- src/controllers/DonationController.ts | 43 ++++++++++--------- src/models/actions/update/UpdateDonation.ts | 41 ++++++++++++++++++ .../actions/update/UpdateFixedDonation.ts | 27 ++++++++++++ 3 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 src/models/actions/update/UpdateDonation.ts create mode 100644 src/models/actions/update/UpdateFixedDonation.ts diff --git a/src/controllers/DonationController.ts b/src/controllers/DonationController.ts index e19c0b9..b7dc153 100644 --- a/src/controllers/DonationController.ts +++ b/src/controllers/DonationController.ts @@ -1,11 +1,12 @@ -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 { getConnectionManager, Repository } from 'typeorm'; -import { DonationNotFoundError } from '../errors/DonationErrors'; +import { DonationIdsNotMatchingError, DonationNotFoundError } from '../errors/DonationErrors'; import { DonorNotFoundError } from '../errors/DonorErrors'; import { RunnerNotFoundError } from '../errors/RunnerErrors'; import { CreateDistanceDonation } from '../models/actions/create/CreateDistanceDonation'; import { CreateFixedDonation } from '../models/actions/create/CreateFixedDonation'; +import { UpdateFixedDonation } from '../models/actions/update/UpdateFixedDonation'; import { DistanceDonation } from '../models/entities/DistanceDonation'; import { Donation } from '../models/entities/Donation'; import { FixedDonation } from '../models/entities/FixedDonation'; @@ -79,28 +80,28 @@ export class DonationController { return (await this.donationRepository.findOne({ id: donation.id }, { relations: ['runner', 'donor', 'runner.scans', 'runner.scans.track'] })).toResponse(); } - // @Put('/:id') - // @Authorized("SCAN:UPDATE") - // @ResponseSchema(ResponseScan) - // @ResponseSchema(DonationNotFoundError, { statusCode: 404 }) - // @ResponseSchema(DonorNotFoundError, { statusCode: 404 }) - // @ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) - // @ResponseSchema(DonationIdsNotMatchingError, { statusCode: 406 }) - // @OpenAPI({ description: "Update the fixed donation (not distance donation - use /donations/fixed instead) whose id you provided.
Please remember that ids can't be changed and amounts must be positive." }) - // async putFixed(@Param('id') id: number, @Body({ validate: true }) donation: UpdateDistanceDonation) { - // let oldDonation = await this.fixedDonationRepository.findOne({ id: id }); + @Put('/:id') + @Authorized("DONATION:UPDATE") + @ResponseSchema(ResponseDonation) + @ResponseSchema(DonationNotFoundError, { statusCode: 404 }) + @ResponseSchema(DonorNotFoundError, { statusCode: 404 }) + @ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) + @ResponseSchema(DonationIdsNotMatchingError, { statusCode: 406 }) + @OpenAPI({ description: "Update the fixed donation (not distance donation - use /donations/fixed instead) whose id you provided.
Please remember that ids can't be changed and amounts must be positive." }) + async putFixed(@Param('id') id: number, @Body({ validate: true }) donation: UpdateFixedDonation) { + let oldDonation = await this.fixedDonationRepository.findOne({ id: id }); - // if (!oldDonation) { - // throw new ScanNotFoundError(); - // } + if (!oldDonation) { + throw new DonationNotFoundError(); + } - // if (oldDonation.id != donation.id) { - // throw new ScanIdsNotMatchingError(); - // } + if (oldDonation.id != donation.id) { + throw new DonationIdsNotMatchingError(); + } - // await this.fixedDonationRepository.save(await donation.update(oldDonation)); - // return (await this.donationRepository.findOne({ id: donation.id }, { relations: ['donor'] })).toResponse(); - // } + await this.fixedDonationRepository.save(await donation.update(oldDonation)); + return (await this.donationRepository.findOne({ id: donation.id }, { relations: ['donor'] })).toResponse(); + } // @Put('/trackscans/:id') // @Authorized("SCAN:UPDATE") diff --git a/src/models/actions/update/UpdateDonation.ts b/src/models/actions/update/UpdateDonation.ts new file mode 100644 index 0000000..7f10f97 --- /dev/null +++ b/src/models/actions/update/UpdateDonation.ts @@ -0,0 +1,41 @@ +import { IsInt, IsPositive } from 'class-validator'; +import { getConnection } from 'typeorm'; +import { DonorNotFoundError } from '../../../errors/DonorErrors'; +import { Donation } from '../../entities/Donation'; +import { Donor } from '../../entities/Donor'; + +/** + * This class is used to update a Donation entity (via put request). + */ +export abstract class UpdateDonation { + /** + * The updated donation'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 donation's associated donor. + * This is important to link donations to donors. + */ + @IsInt() + @IsPositive() + donor: number; + + /** + * Creates a new Donation entity from this. + */ + public abstract update(donation: Donation): Promise; + + /** + * Gets a donor based on the donor id provided via this.donor. + */ + public async getDonor(): Promise { + const donor = await getConnection().getRepository(Donor).findOne({ id: this.donor }); + if (!donor) { + throw new DonorNotFoundError(); + } + return donor; + } +} \ No newline at end of file diff --git a/src/models/actions/update/UpdateFixedDonation.ts b/src/models/actions/update/UpdateFixedDonation.ts new file mode 100644 index 0000000..5e31068 --- /dev/null +++ b/src/models/actions/update/UpdateFixedDonation.ts @@ -0,0 +1,27 @@ +import { IsInt, IsPositive } from 'class-validator'; +import { FixedDonation } from '../../entities/FixedDonation'; +import { UpdateDonation } from './UpdateDonation'; + +/** + * This class is used to update a FixedDonation entity (via put request). + */ +export class UpdateFixedDonation extends UpdateDonation { + /** + * The updated donation's amount. + * The unit is your currency's smallest unit (default: euro cent). + */ + @IsInt() + @IsPositive() + amount: number; + + /** + * Update a FixedDonation entity based on this. + * @param donation The donation that shall be updated. + */ + public async update(donation: FixedDonation): Promise { + donation.amount = this.amount; + donation.donor = await this.getDonor(); + + return donation; + } +} \ No newline at end of file