parent
a437ada3b3
commit
7b08489533
@ -7,11 +7,12 @@ import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganis
|
|||||||
import { Runner } from '../models/entities/Runner';
|
import { Runner } from '../models/entities/Runner';
|
||||||
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
|
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
|
||||||
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
||||||
|
import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation';
|
||||||
import { RunnerController } from './RunnerController';
|
import { RunnerController } from './RunnerController';
|
||||||
import { RunnerTeamController } from './RunnerTeamController';
|
import { RunnerTeamController } from './RunnerTeamController';
|
||||||
|
|
||||||
|
|
||||||
@JsonController('/organisations')
|
@JsonController('/organisation')
|
||||||
//@Authorized('RUNNERS:read')
|
//@Authorized('RUNNERS:read')
|
||||||
export class RunnerOrganisationController {
|
export class RunnerOrganisationController {
|
||||||
private runnerOrganisationRepository: Repository<RunnerOrganisation>;
|
private runnerOrganisationRepository: Repository<RunnerOrganisation>;
|
||||||
@ -24,23 +25,29 @@ export class RunnerOrganisationController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
@ResponseSchema(RunnerOrganisation, { isArray: true })
|
@ResponseSchema(ResponseRunnerOrganisation, { isArray: true })
|
||||||
@OpenAPI({ description: 'Lists all runnerOrganisations.' })
|
@OpenAPI({ description: 'Lists all runnerOrganisations.' })
|
||||||
getAll() {
|
async getAll() {
|
||||||
return this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] });
|
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')
|
@Get('/:id')
|
||||||
@ResponseSchema(RunnerOrganisation)
|
@ResponseSchema(ResponseRunnerOrganisation)
|
||||||
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||||
@OnUndefined(RunnerOrganisationNotFoundError)
|
@OnUndefined(RunnerOrganisationNotFoundError)
|
||||||
@OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' })
|
@OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' })
|
||||||
getOne(@Param('id') id: number) {
|
async getOne(@Param('id') id: number) {
|
||||||
return this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] });
|
return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
@ResponseSchema(RunnerOrganisation)
|
@ResponseSchema(ResponseRunnerOrganisation)
|
||||||
@OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' })
|
@OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' })
|
||||||
async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) {
|
async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) {
|
||||||
let runnerOrganisation;
|
let runnerOrganisation;
|
||||||
@ -50,11 +57,14 @@ export class RunnerOrganisationController {
|
|||||||
return error;
|
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')
|
@Put('/:id')
|
||||||
@ResponseSchema(RunnerOrganisation)
|
@ResponseSchema(ResponseRunnerOrganisation)
|
||||||
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||||
@ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 })
|
@ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 })
|
||||||
@OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." })
|
@OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." })
|
||||||
@ -70,14 +80,15 @@ export class RunnerOrganisationController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation);
|
await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation);
|
||||||
return runnerOrganisation;
|
|
||||||
|
runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] });
|
||||||
|
return new ResponseRunnerOrganisation(runnerOrganisation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete('/:id')
|
@Delete('/:id')
|
||||||
@ResponseSchema(RunnerOrganisation)
|
@ResponseSchema(ResponseRunnerOrganisation)
|
||||||
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||||
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 })
|
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 })
|
||||||
@ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 })
|
|
||||||
@OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' })
|
@OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' })
|
||||||
async remove(@Param('id') id: number, @QueryParam("force") force: boolean) {
|
async remove(@Param('id') id: number, @QueryParam("force") force: boolean) {
|
||||||
let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] });
|
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()
|
let runners: Runner[] = await runnerOrganisation.getRunners()
|
||||||
|
if (!force) {
|
||||||
if (!force && runners.length != 0) {
|
if (runners.length != 0) {
|
||||||
throw new RunnerOrganisationHasRunnersError();
|
throw new RunnerOrganisationHasRunnersError();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const runnerController = new RunnerController()
|
const runnerController = new RunnerController()
|
||||||
await runners.forEach(async runner => {
|
runners.forEach(runner => {
|
||||||
await runnerController.remove(runner.id, true);
|
runnerController.remove(runner.id, true)
|
||||||
});
|
});
|
||||||
|
|
||||||
let teams: RunnerTeam[] = await runnerOrganisation.getTeams()
|
let teams: RunnerTeam[] = await runnerOrganisation.getTeams()
|
||||||
if (!force && teams.length != 0) {
|
if (!force) {
|
||||||
|
if (teams.length != 0) {
|
||||||
throw new RunnerOrganisationHasTeamsError();
|
throw new RunnerOrganisationHasTeamsError();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const teamController = new RunnerTeamController()
|
const teamController = new RunnerTeamController()
|
||||||
await teams.forEach(async team => {
|
teams.forEach(team => {
|
||||||
await teamController.remove(team.id, true);
|
teamController.remove(team.id, true)
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.runnerOrganisationRepository.delete(runnerOrganisation);
|
const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation);
|
||||||
return runnerOrganisation;
|
await this.runnerOrganisationRepository.delete({ id: runnerOrganisation.id });
|
||||||
|
return responseOrganisation;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,8 @@ export class RunnerTeamController {
|
|||||||
runnerController.remove(runner.id, true)
|
runnerController.remove(runner.id, true)
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.runnerTeamRepository.delete(runnerTeam);
|
const responseTeam = new ResponseRunnerTeam(runnerTeam);
|
||||||
return new ResponseRunnerTeam(runnerTeam);
|
await this.runnerTeamRepository.delete({ id: runnerTeam.id });
|
||||||
|
return responseTeam;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
37
src/models/responses/ResponseRunnerOrganisation.ts
Normal file
37
src/models/responses/ResponseRunnerOrganisation.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user