diff --git a/src/controllers/DonationController.ts b/src/controllers/DonationController.ts index 9bd4b96..9c2696d 100644 --- a/src/controllers/DonationController.ts +++ b/src/controllers/DonationController.ts @@ -6,6 +6,7 @@ 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 { UpdateDistanceDonation } from '../models/actions/update/UpdateDistanceDonation'; import { UpdateFixedDonation } from '../models/actions/update/UpdateFixedDonation'; import { DistanceDonation } from '../models/entities/DistanceDonation'; import { Donation } from '../models/entities/Donation'; diff --git a/src/models/actions/update/UpdateDistanceDonation.ts b/src/models/actions/update/UpdateDistanceDonation.ts new file mode 100644 index 0000000..85a5473 --- /dev/null +++ b/src/models/actions/update/UpdateDistanceDonation.ts @@ -0,0 +1,51 @@ +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 { UpdateDonation } from './UpdateDonation'; + +/** + * This class is used to update a DistanceDonation entity (via put request). + */ +export class UpdateDistanceDonation extends UpdateDonation { + + /** + * 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; + + /** + * Update a DistanceDonation entity based on this. + * @param donation The donation that shall be updated. + */ + public async update(donation: DistanceDonation): Promise { + donation.amountPerDistance = this.amountPerDistance; + donation.donor = await this.getDonor(); + donation.runner = await this.getRunner(); + + return donation; + } + + /** + * 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