Now with working runner orga controller including responses

ref #13
This commit is contained in:
Nicolai Ort 2020-12-04 21:55:09 +01:00
parent a437ada3b3
commit 7b08489533
3 changed files with 79 additions and 26 deletions

View File

@ -7,11 +7,12 @@ import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganis
import { Runner } from '../models/entities/Runner';
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
import { RunnerTeam } from '../models/entities/RunnerTeam';
import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation';
import { RunnerController } from './RunnerController';
import { RunnerTeamController } from './RunnerTeamController';
@JsonController('/organisations')
@JsonController('/organisation')
//@Authorized('RUNNERS:read')
export class RunnerOrganisationController {
private runnerOrganisationRepository: Repository<RunnerOrganisation>;
@ -24,23 +25,29 @@ export class RunnerOrganisationController {
}
@Get()
@ResponseSchema(RunnerOrganisation, { isArray: true })
@ResponseSchema(ResponseRunnerOrganisation, { isArray: true })
@OpenAPI({ description: 'Lists all runnerOrganisations.' })
getAll() {
return this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] });
async getAll() {
let responseTeams: ResponseRunnerOrganisation[] = new Array<ResponseRunnerOrganisation>();
const runners = await this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] });
console.log(runners);
runners.forEach(runner => {
responseTeams.push(new ResponseRunnerOrganisation(runner));
});
return responseTeams;
}
@Get('/:id')
@ResponseSchema(RunnerOrganisation)
@ResponseSchema(ResponseRunnerOrganisation)
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
@OnUndefined(RunnerOrganisationNotFoundError)
@OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' })
getOne(@Param('id') id: number) {
return this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] });
async getOne(@Param('id') id: number) {
return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }));
}
@Post()
@ResponseSchema(RunnerOrganisation)
@ResponseSchema(ResponseRunnerOrganisation)
@OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) {
let runnerOrganisation;
@ -50,11 +57,14 @@ export class RunnerOrganisationController {
return error;
}
return this.runnerOrganisationRepository.save(runnerOrganisation);
runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation);
runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] });
return new ResponseRunnerOrganisation(runnerOrganisation);
}
@Put('/:id')
@ResponseSchema(RunnerOrganisation)
@ResponseSchema(ResponseRunnerOrganisation)
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." })
@ -70,14 +80,15 @@ export class RunnerOrganisationController {
}
await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation);
return runnerOrganisation;
runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] });
return new ResponseRunnerOrganisation(runnerOrganisation);
}
@Delete('/:id')
@ResponseSchema(RunnerOrganisation)
@ResponseSchema(ResponseRunnerOrganisation)
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 })
@ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 })
@OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' })
async remove(@Param('id') id: number, @QueryParam("force") force: boolean) {
let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] });
@ -87,25 +98,29 @@ export class RunnerOrganisationController {
}
let runners: Runner[] = await runnerOrganisation.getRunners()
if (!force && runners.length != 0) {
throw new RunnerOrganisationHasRunnersError();
if (!force) {
if (runners.length != 0) {
throw new RunnerOrganisationHasRunnersError();
}
}
const runnerController = new RunnerController()
await runners.forEach(async runner => {
await runnerController.remove(runner.id, true);
runners.forEach(runner => {
runnerController.remove(runner.id, true)
});
let teams: RunnerTeam[] = await runnerOrganisation.getTeams()
if (!force && teams.length != 0) {
throw new RunnerOrganisationHasTeamsError();
if (!force) {
if (teams.length != 0) {
throw new RunnerOrganisationHasTeamsError();
}
}
const teamController = new RunnerTeamController()
await teams.forEach(async team => {
await teamController.remove(team.id, true);
teams.forEach(team => {
teamController.remove(team.id, true)
});
await this.runnerOrganisationRepository.delete(runnerOrganisation);
return runnerOrganisation;
const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation);
await this.runnerOrganisationRepository.delete({ id: runnerOrganisation.id });
return responseOrganisation;
}
}

View File

@ -106,7 +106,8 @@ export class RunnerTeamController {
runnerController.remove(runner.id, true)
});
await this.runnerTeamRepository.delete(runnerTeam);
return new ResponseRunnerTeam(runnerTeam);
const responseTeam = new ResponseRunnerTeam(runnerTeam);
await this.runnerTeamRepository.delete({ id: runnerTeam.id });
return responseTeam;
}
}

View File

@ -0,0 +1,37 @@
import {
IsArray,
IsNotEmpty,
IsObject
} from "class-validator";
import { Address } from '../entities/Address';
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { RunnerTeam } from '../entities/RunnerTeam';
import { ResponseRunnerGroup } from './ResponseRunnerGroup';
/**
* Defines RunnerOrgs's response class.
*/
export class ResponseRunnerOrganisation extends ResponseRunnerGroup {
/**
* The orgs's address.
* Optional.
*/
@IsObject()
@IsNotEmpty()
address?: Address;
/**
* The orgs associated teams.
*/
@IsObject()
@IsArray()
teams: RunnerTeam[];
public constructor(org: RunnerOrganisation) {
super(org);
this.address = org.address;
this.teams = org.teams;
}
}