parent
f53894b16a
commit
3e7190e279
|
@ -0,0 +1,107 @@
|
|||
import { JsonController } from 'routing-controllers';
|
||||
import { OpenAPI } from 'routing-controllers-openapi';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { GroupContact } from '../models/entities/GroupContact';
|
||||
|
||||
@JsonController('/contacts')
|
||||
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
|
||||
export class ContactController {
|
||||
private contactRepository: Repository<GroupContact>;
|
||||
|
||||
/**
|
||||
* Gets the repository of this controller's model/entity.
|
||||
*/
|
||||
constructor() {
|
||||
this.contactRepository = getConnectionManager().get().getRepository(GroupContact);
|
||||
}
|
||||
|
||||
// @Get()
|
||||
// @Authorized("DONOR:GET")
|
||||
// @ResponseSchema(ResponseDonor, { isArray: true })
|
||||
// @OpenAPI({ description: 'Lists all contact. <br> This includes the contact\'s current donation amount.' })
|
||||
// async getAll() {
|
||||
// let responseDonors: ResponseDonor[] = new Array<ResponseDonor>();
|
||||
// const contacts = await this.contactRepository.find({ relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] });
|
||||
// contacts.forEach(contact => {
|
||||
// responseDonors.push(new ResponseDonor(contact));
|
||||
// });
|
||||
// return responseDonors;
|
||||
// }
|
||||
|
||||
// @Get('/:id')
|
||||
// @Authorized("DONOR:GET")
|
||||
// @ResponseSchema(ResponseDonor)
|
||||
// @ResponseSchema(DonorNotFoundError, { statusCode: 404 })
|
||||
// @OnUndefined(DonorNotFoundError)
|
||||
// @OpenAPI({ description: 'Lists all information about the contact whose id got provided. <br> This includes the contact\'s current donation amount.' })
|
||||
// async getOne(@Param('id') id: number) {
|
||||
// let contact = await this.contactRepository.findOne({ id: id }, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] })
|
||||
// if (!contact) { throw new DonorNotFoundError(); }
|
||||
// return new ResponseDonor(contact);
|
||||
// }
|
||||
|
||||
// @Post()
|
||||
// @Authorized("DONOR:CREATE")
|
||||
// @ResponseSchema(ResponseDonor)
|
||||
// @OpenAPI({ description: 'Create a new contact.' })
|
||||
// async post(@Body({ validate: true }) createRunner: CreateDonor) {
|
||||
// let contact;
|
||||
// try {
|
||||
// contact = await createRunner.toEntity();
|
||||
// } catch (error) {
|
||||
// throw error;
|
||||
// }
|
||||
|
||||
// contact = await this.contactRepository.save(contact)
|
||||
// return new ResponseDonor(await this.contactRepository.findOne(contact, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] }));
|
||||
// }
|
||||
|
||||
// @Put('/:id')
|
||||
// @Authorized("DONOR:UPDATE")
|
||||
// @ResponseSchema(ResponseDonor)
|
||||
// @ResponseSchema(DonorNotFoundError, { statusCode: 404 })
|
||||
// @ResponseSchema(DonorIdsNotMatchingError, { statusCode: 406 })
|
||||
// @OpenAPI({ description: "Update the contact whose id you provided. <br> Please remember that ids can't be changed." })
|
||||
// async put(@Param('id') id: number, @Body({ validate: true }) contact: UpdateDonor) {
|
||||
// let oldDonor = await this.contactRepository.findOne({ id: id });
|
||||
|
||||
// if (!oldDonor) {
|
||||
// throw new DonorNotFoundError();
|
||||
// }
|
||||
|
||||
// if (oldDonor.id != contact.id) {
|
||||
// throw new DonorIdsNotMatchingError();
|
||||
// }
|
||||
|
||||
// await this.contactRepository.save(await contact.update(oldDonor));
|
||||
// return new ResponseDonor(await this.contactRepository.findOne({ id: id }, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] }));
|
||||
// }
|
||||
|
||||
// @Delete('/:id')
|
||||
// @Authorized("DONOR:DELETE")
|
||||
// @ResponseSchema(ResponseDonor)
|
||||
// @ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
||||
// @OnUndefined(204)
|
||||
// @OpenAPI({ description: 'Delete the contact whose id you provided. <br> If no contact with this id exists it will just return 204(no content). <br> If the contact still has donations associated this will fail, please provide the query param ?force=true to delete the contact with all associated donations.' })
|
||||
// async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
||||
// let contact = await this.contactRepository.findOne({ id: id });
|
||||
// if (!contact) { return null; }
|
||||
// const responseDonor = await this.contactRepository.findOne(contact, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] });
|
||||
|
||||
// if (!contact) {
|
||||
// throw new DonorNotFoundError();
|
||||
// }
|
||||
|
||||
// const contactDonations = (await this.contactRepository.findOne({ id: contact.id }, { relations: ["donations"] })).donations;
|
||||
// if (contactDonations.length > 0 && !force) {
|
||||
// throw new DonorHasDonationsError();
|
||||
// }
|
||||
// const donationController = new DonationController();
|
||||
// for (let donation of contactDonations) {
|
||||
// await donationController.remove(donation.id, force);
|
||||
// }
|
||||
|
||||
// await this.contactRepository.delete(contact);
|
||||
// return new ResponseDonor(responseDonor);
|
||||
// }
|
||||
}
|
Loading…
Reference in New Issue