Runnerteams now with resolving relations and response types :O

ref #13
This commit is contained in:
2020-12-04 21:38:28 +01:00
parent c53e94d205
commit a437ada3b3
3 changed files with 105 additions and 12 deletions

View File

@@ -6,6 +6,7 @@ import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNot
import { CreateRunnerTeam } from '../models/creation/CreateRunnerTeam';
import { Runner } from '../models/entities/Runner';
import { RunnerTeam } from '../models/entities/RunnerTeam';
import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam';
import { RunnerController } from './RunnerController';
@@ -22,23 +23,29 @@ export class RunnerTeamController {
}
@Get()
@ResponseSchema(RunnerTeam, { isArray: true })
@ResponseSchema(ResponseRunnerTeam, { isArray: true })
@OpenAPI({ description: 'Lists all runnerTeams.' })
getAll() {
return this.runnerTeamRepository.find({ relations: ['parentGroup', 'contact'] });
async getAll() {
let responseTeams: ResponseRunnerTeam[] = new Array<ResponseRunnerTeam>();
const runners = await this.runnerTeamRepository.find({ relations: ['parentGroup', 'contact'] });
console.log(runners);
runners.forEach(runner => {
responseTeams.push(new ResponseRunnerTeam(runner));
});
return responseTeams;
}
@Get('/:id')
@ResponseSchema(RunnerTeam)
@ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@OnUndefined(RunnerTeamNotFoundError)
@OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' })
getOne(@Param('id') id: number) {
return this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] });
async getOne(@Param('id') id: number) {
return new ResponseRunnerTeam(await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }));
}
@Post()
@ResponseSchema(RunnerTeam)
@ResponseSchema(ResponseRunnerTeam)
@OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) {
let runnerTeam;
@@ -48,11 +55,14 @@ export class RunnerTeamController {
return error;
}
return this.runnerTeamRepository.save(runnerTeam);
runnerTeam = await this.runnerTeamRepository.save(runnerTeam);
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
return new ResponseRunnerTeam(runnerTeam);
}
@Put('/:id')
@ResponseSchema(RunnerTeam)
@ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." })
@@ -68,11 +78,13 @@ export class RunnerTeamController {
}
await this.runnerTeamRepository.update(oldRunnerTeam, runnerTeam);
return runnerTeam;
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
return new ResponseRunnerTeam(runnerTeam);
}
@Delete('/:id')
@ResponseSchema(RunnerTeam)
@ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 })
@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' })
@@ -95,6 +107,6 @@ export class RunnerTeamController {
});
await this.runnerTeamRepository.delete(runnerTeam);
return runnerTeam;
return new ResponseRunnerTeam(runnerTeam);
}
}