Added basics for the runner team controller

ref #13
This commit is contained in:
2020-12-04 17:50:10 +01:00
parent 838cedfe6c
commit ca917b0577
4 changed files with 187 additions and 1 deletions

View File

@@ -0,0 +1,102 @@
import { 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 { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors';
import { CreateRunnerTeam } from '../models/creation/CreateRunnerTeam';
import { Runner } from '../models/entities/Runner';
import { RunnerTeam } from '../models/entities/RunnerTeam';
import { RunnerController } from './RunnerController';
@JsonController('/teams')
//@Authorized('RUNNERS:read')
export class RunnerTeamController {
private runnerTeamRepository: Repository<RunnerTeam>;
/**
* Gets the repository of this controller's model/entity.
*/
constructor() {
this.runnerTeamRepository = getConnectionManager().get().getRepository(RunnerTeam);
}
@Get()
@ResponseSchema(RunnerTeam, { isArray: true })
@OpenAPI({ description: 'Lists all runnerTeams.' })
getAll() {
return this.runnerTeamRepository.find();
}
@Get('/:id')
@ResponseSchema(RunnerTeam)
@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 });
}
@Post()
@ResponseSchema(RunnerTeam)
@OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) {
let runnerTeam;
try {
runnerTeam = await createRunnerTeam.toRunnerTeam();
} catch (error) {
return error;
}
return this.runnerTeamRepository.save(runnerTeam);
}
@Put('/:id')
@ResponseSchema(RunnerTeam)
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." })
async put(@Param('id') id: number, @EntityFromBody() runnerTeam: RunnerTeam) {
let oldRunnerTeam = await this.runnerTeamRepository.findOne({ id: id });
if (!oldRunnerTeam) {
throw new RunnerTeamNotFoundError();
}
if (oldRunnerTeam.id != runnerTeam.id) {
throw new RunnerTeamIdsNotMatchingError();
}
await this.runnerTeamRepository.update(oldRunnerTeam, runnerTeam);
return runnerTeam;
}
@Delete('/:id')
@ResponseSchema(RunnerTeam)
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 })
@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' })
async remove(@Param('id') id: number, @QueryParam("force") force) {
let runnerTeam = await this.runnerTeamRepository.findOne({ id: id });
if (!runnerTeam) {
throw new RunnerTeamNotFoundError();
}
let runners: Runner[] = await runnerTeam.getRunners()
if (!force) {
if (runners.length != 0) {
throw new RunnerTeamHasRunnersError();
}
}
const runnerController = new RunnerController()
runners.forEach(runner => {
runnerController.remove(runner.id)
});
await this.runnerTeamRepository.delete(runnerTeam);
return runnerTeam;
}
}