From 2e760ff46149a25183172a8793275c6c76d39c75 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 12 Jan 2021 18:39:14 +0100 Subject: [PATCH] Implemented the donation creation action models ref #66 --- .../actions/create/CreateDistanceDonation.ts | 52 +++++++++++++++++++ src/models/actions/create/CreateDonation.ts | 34 ++++++++++++ .../actions/create/CreateFixedDonation.ts | 28 ++++++++++ 3 files changed, 114 insertions(+) create mode 100644 src/models/actions/create/CreateDistanceDonation.ts create mode 100644 src/models/actions/create/CreateDonation.ts create mode 100644 src/models/actions/create/CreateFixedDonation.ts diff --git a/src/models/actions/create/CreateDistanceDonation.ts b/src/models/actions/create/CreateDistanceDonation.ts new file mode 100644 index 0000000..e212068 --- /dev/null +++ b/src/models/actions/create/CreateDistanceDonation.ts @@ -0,0 +1,52 @@ +import { IsInt, IsPositive } from 'class-validator'; +import { getConnection } from 'typeorm'; +import { RunnerNotFoundError } from '../../../errors/RunnerErrors'; +import { DistanceDonation } from '../../entities/DistanceDonation'; +import { Runner } from '../../entities/Runner'; +import { CreateDonation } from './CreateDonation'; + +/** + * This class is used to create a new FixedDonation entity from a json body (post request). + */ +export class CreateDistanceDonation extends CreateDonation { + + /** + * The donation's associated runner. + * This is important to link the runner's distance ran to the donation. + */ + @IsInt() + @IsPositive() + runner: number; + + /** + * The donation's amount per distance (full kilometer aka 1000 meters). + * The unit is your currency's smallest unit (default: euro cent). + */ + @IsInt() + @IsPositive() + amountPerDistance: number; + + /** + * Creates a new FixedDonation entity from this. + */ + public async toEntity(): Promise { + let newDonation = new DistanceDonation; + + newDonation.amountPerDistance = this.amountPerDistance; + newDonation.donor = await this.getDonor(); + newDonation.runner = await this.getRunner(); + + return newDonation; + } + + /** + * Gets a runner based on the runner id provided via this.runner. + */ + public async getRunner(): Promise { + const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner }); + if (!runner) { + throw new RunnerNotFoundError(); + } + return runner; + } +} \ No newline at end of file diff --git a/src/models/actions/create/CreateDonation.ts b/src/models/actions/create/CreateDonation.ts new file mode 100644 index 0000000..dbed745 --- /dev/null +++ b/src/models/actions/create/CreateDonation.ts @@ -0,0 +1,34 @@ +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 create a new Donation entity from a json body (post request). + */ +export abstract class CreateDonation { + /** + * The 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 toEntity(): Promise; + + /** + * Gets a runner based on the runner id provided via this.runner. + */ + 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/create/CreateFixedDonation.ts b/src/models/actions/create/CreateFixedDonation.ts new file mode 100644 index 0000000..4d73f50 --- /dev/null +++ b/src/models/actions/create/CreateFixedDonation.ts @@ -0,0 +1,28 @@ +import { IsInt, IsPositive } from 'class-validator'; +import { FixedDonation } from '../../entities/FixedDonation'; +import { CreateDonation } from './CreateDonation'; + +/** + * This class is used to create a new FixedDonation entity from a json body (post request). + */ +export class CreateFixedDonation extends CreateDonation { + /** + * The donation's amount. + * The unit is your currency's smallest unit (default: euro cent). + */ + @IsInt() + @IsPositive() + amount: number; + + /** + * Creates a new FixedDonation entity from this. + */ + public async toEntity(): Promise { + let newDonation = new FixedDonation; + + newDonation.amount = this.amount; + newDonation.donor = await this.getDonor(); + + return newDonation; + } +} \ No newline at end of file