From 6c53701a59a0f9559d3668ead3970b7eacefe2ec Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 12 Jan 2021 18:16:09 +0100 Subject: [PATCH] Implemented the donation response ref #66 --- src/models/entities/DistanceDonation.ts | 4 ++-- src/models/entities/Donation.ts | 8 ++++--- src/models/entities/FixedDonation.ts | 24 ++++++++++++++++--- ...esponseDonation.ts => ResponseDonation.ts} | 6 ++--- 4 files changed, 31 insertions(+), 11 deletions(-) rename src/models/responses/{IResponseDonation.ts => ResponseDonation.ts} (84%) diff --git a/src/models/entities/DistanceDonation.ts b/src/models/entities/DistanceDonation.ts index 6b9ba5d..436dc3f 100644 --- a/src/models/entities/DistanceDonation.ts +++ b/src/models/entities/DistanceDonation.ts @@ -43,7 +43,7 @@ export class DistanceDonation extends Donation { /** * Turns this entity into it's response class. */ - public toResponse() { - return new Error("NotImplemented"); + public toResponse(): DistanceDonation { + return null; } } diff --git a/src/models/entities/Donation.ts b/src/models/entities/Donation.ts index 46d7d45..1dd023d 100644 --- a/src/models/entities/Donation.ts +++ b/src/models/entities/Donation.ts @@ -3,6 +3,7 @@ import { IsNotEmpty } from "class-validator"; import { Entity, ManyToOne, PrimaryGeneratedColumn, TableInheritance } from "typeorm"; +import { ResponseDonation } from '../responses/ResponseDonation'; import { Donor } from './Donor'; /** @@ -31,12 +32,13 @@ export abstract class Donation { * The donation's amount in cents (or whatever your currency's smallest unit is.). * The exact implementation may differ for each type of donation. */ - abstract amount: number; + public abstract get amount(): number; + /** * Turns this entity into it's response class. */ - public toResponse() { - return new Error("NotImplemented"); + public toResponse(): ResponseDonation { + return new ResponseDonation(this); } } \ No newline at end of file diff --git a/src/models/entities/FixedDonation.ts b/src/models/entities/FixedDonation.ts index 6a32066..c6454d4 100644 --- a/src/models/entities/FixedDonation.ts +++ b/src/models/entities/FixedDonation.ts @@ -1,5 +1,6 @@ import { IsInt, IsPositive } from "class-validator"; import { ChildEntity, Column } from "typeorm"; +import { ResponseDonation } from '../responses/ResponseDonation'; import { Donation } from "./Donation"; /** @@ -11,16 +12,33 @@ export class FixedDonation extends Donation { /** * The donation's amount in cents (or whatever your currency's smallest unit is.). + * This is the "real" value used by fixed donations. */ @Column() @IsInt() @IsPositive() - amount: number; + private _amount: number; + + /** + * The donation's amount in cents (or whatever your currency's smallest unit is.). + */ + @IsInt() + @IsPositive() + public get amount(): number { + return this._amount; + } + + /** + * The donation's amount in cents (or whatever your currency's smallest unit is.). + */ + public set amount(value: number) { + this._amount = value; + } /** * Turns this entity into it's response class. */ - public toResponse() { - return new Error("NotImplemented"); + public toResponse(): ResponseDonation { + return new ResponseDonation(this); } } \ No newline at end of file diff --git a/src/models/responses/IResponseDonation.ts b/src/models/responses/ResponseDonation.ts similarity index 84% rename from src/models/responses/IResponseDonation.ts rename to src/models/responses/ResponseDonation.ts index f07b7d0..c2789e3 100644 --- a/src/models/responses/IResponseDonation.ts +++ b/src/models/responses/ResponseDonation.ts @@ -3,9 +3,9 @@ import { Donation } from '../entities/Donation'; import { ResponseDonor } from './ResponseDonor'; /** - * Defines the donation response interface. + * Defines the donation response. */ -export abstract class IResponseDonation { +export class ResponseDonation { /** * The donation's id. */ @@ -26,7 +26,7 @@ export abstract class IResponseDonation { amount: number; /** - * Creates a IResponseDonation object from a scan. + * Creates a ResponseDonation object from a scan. * @param donation The donation the response shall be build for. */ public constructor(donation: Donation) {