| @@ -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'; | ||||
|   | ||||
							
								
								
									
										51
									
								
								src/models/actions/update/UpdateDistanceDonation.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								src/models/actions/update/UpdateDistanceDonation.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -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<DistanceDonation> { | ||||
|         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<Runner> { | ||||
|         const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner }); | ||||
|         if (!runner) { | ||||
|             throw new RunnerNotFoundError(); | ||||
|         } | ||||
|         return runner; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user