Runnerteams now with resolving relations and response types :O

ref #13
This commit is contained in:
Nicolai Ort 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);
}
}

View File

@ -0,0 +1,55 @@
import {
IsInt,
IsNotEmpty,
IsObject,
IsOptional,
IsString
} from "class-validator";
import { GroupContact } from '../entities/GroupContact';
import { RunnerGroup } from '../entities/RunnerGroup';
/**
* Defines a track of given length.
*/
export abstract class ResponseRunnerGroup {
/**
* Autogenerated unique id (primary key).
*/
@IsInt()
@IsNotEmpty()
id: number;;
/**
* The groups's name.
*/
@IsString()
@IsNotEmpty()
name: string;
/**
* The group's contact.
* Optional.
*/
@IsObject()
@IsOptional()
contact?: GroupContact;
public constructor(group: RunnerGroup) {
this.id = group.id;
this.name = group.name;
this.contact = group.contact;
}
}

View File

@ -0,0 +1,26 @@
import {
IsNotEmpty,
IsObject
} from "class-validator";
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { RunnerTeam } from '../entities/RunnerTeam';
import { ResponseRunnerGroup } from './ResponseRunnerGroup';
/**
* Defines RunnerTeam's response class.
*/
export class ResponseRunnerTeam extends ResponseRunnerGroup {
/**
* The team's parent group (organisation).
* Optional.
*/
@IsObject()
@IsNotEmpty()
parentGroup: RunnerOrganisation;
public constructor(team: RunnerTeam) {
super(team);
this.parentGroup = team.parentGroup;
}
}