All things deletion for runner* now are clean af and cascadeing

ref #13
This commit is contained in:
Nicolai Ort 2020-12-05 12:15:51 +01:00
parent 9c63a34fe1
commit 45675b0699
6 changed files with 33 additions and 70 deletions

View File

@ -1,7 +1,7 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions';
import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors'; import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors';
import { CreateRunner } from '../models/creation/CreateRunner'; import { CreateRunner } from '../models/creation/CreateRunner';
import { Runner } from '../models/entities/Runner'; import { Runner } from '../models/entities/Runner';
@ -65,7 +65,7 @@ export class RunnerController {
@ResponseSchema(RunnerIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(RunnerIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a runner object (id can't be changed)." }) @OpenAPI({ description: "Update a runner object (id can't be changed)." })
async put(@Param('id') id: number, @EntityFromBody() runner: Runner) { async put(@Param('id') id: number, @EntityFromBody() runner: Runner) {
let oldRunner = await this.runnerRepository.findOne({ id: id }); let oldRunner = await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] });
if (!oldRunner) { if (!oldRunner) {
throw new RunnerNotFoundError(); throw new RunnerNotFoundError();
@ -83,14 +83,14 @@ export class RunnerController {
@ResponseSchema(ResponseRunner) @ResponseSchema(ResponseRunner)
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
@OpenAPI({ description: 'Delete a specified runner (if it exists).' }) @OpenAPI({ description: 'Delete a specified runner (if it exists).' })
async remove(@Param('id') id: number, @QueryParam("force") force: boolean) { async remove(@EntityFromParam('id') runner: Runner, @QueryParam("force") force: boolean) {
const runner = await this.runnerRepository.findOne({ id: id }); const responseRunner = await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] });
if (!runner) { if (!runner) {
throw new RunnerNotFoundError(); throw new RunnerNotFoundError();
} }
await this.runnerRepository.delete(runner); await this.runnerRepository.delete(runner);
return new ResponseRunner(runner); return new ResponseRunner(responseRunner);
} }
} }

View File

@ -1,12 +1,10 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions';
import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors';
import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganisation'; import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganisation';
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 { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation'; import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation';
import { RunnerController } from './RunnerController'; import { RunnerController } from './RunnerController';
import { RunnerTeamController } from './RunnerTeamController'; import { RunnerTeamController } from './RunnerTeamController';
@ -87,39 +85,38 @@ export class RunnerOrganisationController {
@Delete('/:id') @Delete('/:id')
@ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(ResponseRunnerOrganisation)
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 })
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 }) @ResponseSchema(RunnerOrganisationHasRunnersError, { 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(@EntityFromParam('id') organisation: RunnerOrganisation, @QueryParam("force") force: boolean) {
let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }); let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: ['address', 'contact', 'runners', 'teams'] });
if (!runnerOrganisation) { if (!runnerOrganisation) {
throw new RunnerOrganisationNotFoundError(); throw new RunnerOrganisationNotFoundError();
} }
let runners: Runner[] = await runnerOrganisation.getRunners()
if (!force) { if (!force) {
if (runners.length != 0) { if (runnerOrganisation.teams.length != 0) {
throw new RunnerOrganisationHasRunnersError();
}
}
const runnerController = new RunnerController()
runners.forEach(runner => {
runnerController.remove(runner.id, true)
});
let teams: RunnerTeam[] = await runnerOrganisation.getTeams()
if (!force) {
if (teams.length != 0) {
throw new RunnerOrganisationHasTeamsError(); throw new RunnerOrganisationHasTeamsError();
} }
} }
const teamController = new RunnerTeamController() const teamController = new RunnerTeamController()
teams.forEach(team => { for (let team of runnerOrganisation.teams) {
teamController.remove(team.id, true) await teamController.remove(team, true);
}); }
if (!force) {
if (runnerOrganisation.runners.length != 0) {
throw new RunnerOrganisationHasRunnersError();
}
}
const runnerController = new RunnerController()
for (let runner of runnerOrganisation.runners) {
await runnerController.remove(runner, true);
}
const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation); const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation);
await this.runnerOrganisationRepository.delete({ id: runnerOrganisation.id }); await this.runnerOrganisationRepository.delete(organisation);
return responseOrganisation; return responseOrganisation;
} }
} }

View File

@ -1,7 +1,7 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions';
import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors'; import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors';
import { CreateRunnerTeam } from '../models/creation/CreateRunnerTeam'; import { CreateRunnerTeam } from '../models/creation/CreateRunnerTeam';
import { RunnerTeam } from '../models/entities/RunnerTeam'; import { RunnerTeam } from '../models/entities/RunnerTeam';
@ -87,8 +87,8 @@ export class RunnerTeamController {
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 }) @ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 })
@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' }) @OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' })
async remove(@Param('id') id: number, @QueryParam("force") force: boolean) { async remove(@EntityFromParam('id') team: RunnerTeam, @QueryParam("force") force: boolean) {
let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact', 'runners'] }); let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] });
if (!runnerTeam) { if (!runnerTeam) {
throw new RunnerTeamNotFoundError(); throw new RunnerTeamNotFoundError();
@ -100,12 +100,12 @@ export class RunnerTeamController {
} }
} }
const runnerController = new RunnerController() const runnerController = new RunnerController()
await runnerTeam.runners.forEach(async runner => { for (let runner of runnerTeam.runners) {
await runnerController.remove(runner.id, true); await runnerController.remove(runner, true);
}); }
const responseTeam = new ResponseRunnerTeam(runnerTeam); const responseTeam = new ResponseRunnerTeam(runnerTeam);
await this.runnerTeamRepository.delete({ id: runnerTeam.id }); await this.runnerTeamRepository.delete(team);
return responseTeam; return responseTeam;
} }
} }

View File

@ -42,6 +42,4 @@ export abstract class RunnerGroup {
*/ */
@OneToMany(() => Runner, runner => runner.group, { nullable: true }) @OneToMany(() => Runner, runner => runner.group, { nullable: true })
runners: Runner[]; runners: Runner[];
public abstract getRunners();
} }

View File

@ -1,7 +1,6 @@
import { IsOptional } from "class-validator"; import { IsOptional } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm"; import { ChildEntity, ManyToOne, OneToMany } from "typeorm";
import { Address } from "./Address"; import { Address } from "./Address";
import { Runner } from './Runner';
import { RunnerGroup } from "./RunnerGroup"; import { RunnerGroup } from "./RunnerGroup";
import { RunnerTeam } from "./RunnerTeam"; import { RunnerTeam } from "./RunnerTeam";
@ -24,27 +23,4 @@ export class RunnerOrganisation extends RunnerGroup {
*/ */
@OneToMany(() => RunnerTeam, team => team.parentGroup, { nullable: true }) @OneToMany(() => RunnerTeam, team => team.parentGroup, { nullable: true })
teams: RunnerTeam[]; teams: RunnerTeam[];
/**
* Returns all runners associated with this organisation or it's teams.
*/
public async getRunners() {
let runners: Runner[] = new Array<Runner>();
const teams = await this.getTeams();
await teams.forEach(async team => {
runners.push(... await team.getRunners());
});
await runners.push(... await getConnectionManager().get().getRepository(Runner).find({ group: this }));
return runners;
}
/**
* Returns all teams associated with this organisation.
*/
public async getTeams() {
return await getConnectionManager().get().getRepository(RunnerTeam).find({ parentGroup: this });
}
} }

View File

@ -1,6 +1,5 @@
import { IsNotEmpty } from "class-validator"; import { IsNotEmpty } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne } from "typeorm"; import { ChildEntity, ManyToOne } from "typeorm";
import { Runner } from './Runner';
import { RunnerGroup } from "./RunnerGroup"; import { RunnerGroup } from "./RunnerGroup";
import { RunnerOrganisation } from "./RunnerOrganisation"; import { RunnerOrganisation } from "./RunnerOrganisation";
@ -17,11 +16,4 @@ export class RunnerTeam extends RunnerGroup {
@IsNotEmpty() @IsNotEmpty()
@ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: true }) @ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: true })
parentGroup?: RunnerOrganisation; parentGroup?: RunnerOrganisation;
/**
* Returns all runners associated with this team.
*/
public async getRunners() {
return await getConnectionManager().get().getRepository(Runner).find({ group: this });
}
} }