41
src/models/actions/update/UpdateDonation.ts
Normal file
41
src/models/actions/update/UpdateDonation.ts
Normal file
@@ -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<Donation>;
|
||||
|
||||
/**
|
||||
* Gets a donor based on the donor id provided via this.donor.
|
||||
*/
|
||||
public async getDonor(): Promise<Donor> {
|
||||
const donor = await getConnection().getRepository(Donor).findOne({ id: this.donor });
|
||||
if (!donor) {
|
||||
throw new DonorNotFoundError();
|
||||
}
|
||||
return donor;
|
||||
}
|
||||
}
|
||||
27
src/models/actions/update/UpdateFixedDonation.ts
Normal file
27
src/models/actions/update/UpdateFixedDonation.ts
Normal file
@@ -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<FixedDonation> {
|
||||
donation.amount = this.amount;
|
||||
donation.donor = await this.getDonor();
|
||||
|
||||
return donation;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user