Implemented fixed donation updateing

ref #66
This commit is contained in:
Nicolai Ort 2021-01-12 19:00:35 +01:00
parent 56cedf0144
commit 9517df5082
3 changed files with 90 additions and 21 deletions

View File

@ -1,11 +1,12 @@
import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, QueryParam } from 'routing-controllers';
import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm';
import { DonationNotFoundError } from '../errors/DonationErrors';
import { DonationIdsNotMatchingError, DonationNotFoundError } from '../errors/DonationErrors';
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 { UpdateFixedDonation } from '../models/actions/update/UpdateFixedDonation';
import { DistanceDonation } from '../models/entities/DistanceDonation';
import { Donation } from '../models/entities/Donation';
import { FixedDonation } from '../models/entities/FixedDonation';
@ -79,28 +80,28 @@ export class DonationController {
return (await this.donationRepository.findOne({ id: donation.id }, { relations: ['runner', 'donor', 'runner.scans', 'runner.scans.track'] })).toResponse();
}
// @Put('/:id')
// @Authorized("SCAN:UPDATE")
// @ResponseSchema(ResponseScan)
// @ResponseSchema(DonationNotFoundError, { statusCode: 404 })
// @ResponseSchema(DonorNotFoundError, { statusCode: 404 })
// @ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
// @ResponseSchema(DonationIdsNotMatchingError, { statusCode: 406 })
// @OpenAPI({ description: "Update the fixed donation (not distance donation - use /donations/fixed instead) whose id you provided. <br> Please remember that ids can't be changed and amounts must be positive." })
// async putFixed(@Param('id') id: number, @Body({ validate: true }) donation: UpdateDistanceDonation) {
// let oldDonation = await this.fixedDonationRepository.findOne({ id: id });
@Put('/:id')
@Authorized("DONATION:UPDATE")
@ResponseSchema(ResponseDonation)
@ResponseSchema(DonationNotFoundError, { statusCode: 404 })
@ResponseSchema(DonorNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
@ResponseSchema(DonationIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update the fixed donation (not distance donation - use /donations/fixed instead) whose id you provided. <br> Please remember that ids can't be changed and amounts must be positive." })
async putFixed(@Param('id') id: number, @Body({ validate: true }) donation: UpdateFixedDonation) {
let oldDonation = await this.fixedDonationRepository.findOne({ id: id });
// if (!oldDonation) {
// throw new ScanNotFoundError();
// }
if (!oldDonation) {
throw new DonationNotFoundError();
}
// if (oldDonation.id != donation.id) {
// throw new ScanIdsNotMatchingError();
// }
if (oldDonation.id != donation.id) {
throw new DonationIdsNotMatchingError();
}
// await this.fixedDonationRepository.save(await donation.update(oldDonation));
// return (await this.donationRepository.findOne({ id: donation.id }, { relations: ['donor'] })).toResponse();
// }
await this.fixedDonationRepository.save(await donation.update(oldDonation));
return (await this.donationRepository.findOne({ id: donation.id }, { relations: ['donor'] })).toResponse();
}
// @Put('/trackscans/:id')
// @Authorized("SCAN:UPDATE")

View 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;
}
}

View 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;
}
}