Implemented the get endpoints

ref #104
This commit is contained in:
Nicolai Ort 2021-01-19 16:05:35 +01:00
parent 1407fe36f3
commit ab70f7e498
1 changed files with 27 additions and 25 deletions

View File

@ -1,7 +1,9 @@
import { JsonController } from 'routing-controllers';
import { OpenAPI } from 'routing-controllers-openapi';
import { Authorized, Get, JsonController, OnUndefined, Param } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm';
import { GroupContactNotFoundError } from '../errors/GroupContactErrors';
import { GroupContact } from '../models/entities/GroupContact';
import { ResponseGroupContact } from '../models/responses/ResponseGroupContact';
@JsonController('/contacts')
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
@ -15,30 +17,30 @@ export class ContactController {
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()
@Authorized("CONTACT:GET")
@ResponseSchema(ResponseGroupContact, { isArray: true })
@OpenAPI({ description: 'Lists all contacts. <br> This includes the contact\'s associated groups.' })
async getAll() {
let responseContacts: ResponseGroupContact[] = new Array<ResponseGroupContact>();
const contacts = await this.contactRepository.find({ relations: ['groups'] });
contacts.forEach(contact => {
responseContacts.push(contact.toResponse());
});
return responseContacts;
}
// @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);
// }
@Get('/:id')
@Authorized("DONOR:GET")
@ResponseSchema(ResponseGroupContact)
@ResponseSchema(GroupContactNotFoundError, { statusCode: 404 })
@OnUndefined(GroupContactNotFoundError)
@OpenAPI({ description: 'Lists all information about the contact whose id got provided. <br> This includes the contact\'s associated groups.' })
async getOne(@Param('id') id: number) {
let contact = await this.contactRepository.findOne({ id: id }, { relations: ['groups'] })
if (!contact) { throw new GroupContactNotFoundError(); }
return contact.toResponse();
}
// @Post()
// @Authorized("DONOR:CREATE")