114 lines
4.3 KiB
TypeScript
114 lines
4.3 KiB
TypeScript
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/actions/CreateRunnerTeam';
|
|
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
|
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
|
import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam';
|
|
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(ResponseRunnerTeam, { isArray: true })
|
|
@OpenAPI({ description: 'Lists all runnerTeams.' })
|
|
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(ResponseRunnerTeam)
|
|
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
|
|
@OnUndefined(RunnerTeamNotFoundError)
|
|
@OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' })
|
|
async getOne(@Param('id') id: number) {
|
|
let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] });
|
|
if (!runnerTeam) { throw new RunnerTeamNotFoundError(); }
|
|
return new ResponseRunnerTeam(runnerTeam);
|
|
}
|
|
|
|
@Post()
|
|
@ResponseSchema(ResponseRunnerTeam)
|
|
@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) {
|
|
throw error;
|
|
}
|
|
|
|
runnerTeam = await this.runnerTeamRepository.save(runnerTeam);
|
|
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
|
|
|
|
return new ResponseRunnerTeam(runnerTeam);
|
|
}
|
|
|
|
@Put('/:id')
|
|
@ResponseSchema(ResponseRunnerTeam)
|
|
@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 }, { relations: ['parentGroup', 'contact'] });
|
|
|
|
if (!oldRunnerTeam) {
|
|
throw new RunnerTeamNotFoundError();
|
|
}
|
|
|
|
if (oldRunnerTeam.id != runnerTeam.id) {
|
|
throw new RunnerTeamIdsNotMatchingError();
|
|
}
|
|
|
|
await this.runnerTeamRepository.update(oldRunnerTeam, runnerTeam);
|
|
|
|
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
|
|
return new ResponseRunnerTeam(runnerTeam);
|
|
}
|
|
|
|
@Delete('/:id')
|
|
@ResponseSchema(ResponseRunnerTeam)
|
|
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
|
@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 })
|
|
@OnUndefined(204)
|
|
@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' })
|
|
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
|
let team = await this.runnerTeamRepository.findOne({ id: id });
|
|
if (!team) { return null; }
|
|
let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] });
|
|
|
|
if (!force) {
|
|
if (runnerTeam.runners.length != 0) {
|
|
throw new RunnerTeamHasRunnersError();
|
|
}
|
|
}
|
|
const runnerController = new RunnerController()
|
|
for (let runner of runnerTeam.runners) {
|
|
await runnerController.remove(runner.id, true);
|
|
}
|
|
|
|
const responseTeam = new ResponseRunnerTeam(runnerTeam);
|
|
await this.runnerTeamRepository.delete(team);
|
|
return responseTeam;
|
|
}
|
|
}
|