parent
0df26cbd54
commit
2e760ff461
52
src/models/actions/create/CreateDistanceDonation.ts
Normal file
52
src/models/actions/create/CreateDistanceDonation.ts
Normal file
@ -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<DistanceDonation> {
|
||||||
|
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<Runner> {
|
||||||
|
const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner });
|
||||||
|
if (!runner) {
|
||||||
|
throw new RunnerNotFoundError();
|
||||||
|
}
|
||||||
|
return runner;
|
||||||
|
}
|
||||||
|
}
|
34
src/models/actions/create/CreateDonation.ts
Normal file
34
src/models/actions/create/CreateDonation.ts
Normal file
@ -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<Donation>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a runner based on the runner id provided via this.runner.
|
||||||
|
*/
|
||||||
|
public async getDonor(): Promise<Donor> {
|
||||||
|
const donor = await getConnection().getRepository(Donor).findOne({ id: this.donor });
|
||||||
|
if (!donor) {
|
||||||
|
throw new DonorNotFoundError();
|
||||||
|
}
|
||||||
|
return donor;
|
||||||
|
}
|
||||||
|
}
|
28
src/models/actions/create/CreateFixedDonation.ts
Normal file
28
src/models/actions/create/CreateFixedDonation.ts
Normal file
@ -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<FixedDonation> {
|
||||||
|
let newDonation = new FixedDonation;
|
||||||
|
|
||||||
|
newDonation.amount = this.amount;
|
||||||
|
newDonation.donor = await this.getDonor();
|
||||||
|
|
||||||
|
return newDonation;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user