All things deletion for runner* now are clean af and cascadeing
ref #13
This commit is contained in:
		| @@ -1,7 +1,7 @@ | ||||
| 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 { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; | ||||
| import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors'; | ||||
| import { CreateRunner } from '../models/creation/CreateRunner'; | ||||
| import { Runner } from '../models/entities/Runner'; | ||||
| @@ -65,7 +65,7 @@ export class RunnerController { | ||||
| 	@ResponseSchema(RunnerIdsNotMatchingError, { statusCode: 406 }) | ||||
| 	@OpenAPI({ description: "Update a runner object (id can't be changed)." }) | ||||
| 	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) { | ||||
| 			throw new RunnerNotFoundError(); | ||||
| @@ -83,14 +83,14 @@ export class RunnerController { | ||||
| 	@ResponseSchema(ResponseRunner) | ||||
| 	@ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) | ||||
| 	@OpenAPI({ description: 'Delete a specified runner (if it exists).' }) | ||||
| 	async remove(@Param('id') id: number, @QueryParam("force") force: boolean) { | ||||
| 		const runner = await this.runnerRepository.findOne({ id: id }); | ||||
| 	async remove(@EntityFromParam('id') runner: Runner, @QueryParam("force") force: boolean) { | ||||
| 		const responseRunner = await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] }); | ||||
|  | ||||
| 		if (!runner) { | ||||
| 			throw new RunnerNotFoundError(); | ||||
| 		} | ||||
|  | ||||
| 		await this.runnerRepository.delete(runner); | ||||
| 		return new ResponseRunner(runner); | ||||
| 		return new ResponseRunner(responseRunner); | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,12 +1,10 @@ | ||||
| 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 { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; | ||||
| import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; | ||||
| import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganisation'; | ||||
| 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'; | ||||
| @@ -87,39 +85,38 @@ export class RunnerOrganisationController { | ||||
| 	@Delete('/:id') | ||||
| 	@ResponseSchema(ResponseRunnerOrganisation) | ||||
| 	@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) | ||||
| 	@ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 }) | ||||
| 	@ResponseSchema(RunnerOrganisationHasRunnersError, { 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'] }); | ||||
| 	async remove(@EntityFromParam('id') organisation: RunnerOrganisation, @QueryParam("force") force: boolean) { | ||||
| 		let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: ['address', 'contact', 'runners', 'teams'] }); | ||||
|  | ||||
| 		if (!runnerOrganisation) { | ||||
| 			throw new RunnerOrganisationNotFoundError(); | ||||
| 		} | ||||
|  | ||||
| 		let runners: Runner[] = await runnerOrganisation.getRunners() | ||||
| 		if (!force) { | ||||
| 			if (runners.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) { | ||||
| 			if (runnerOrganisation.teams.length != 0) { | ||||
| 				throw new RunnerOrganisationHasTeamsError(); | ||||
| 			} | ||||
| 		} | ||||
| 		const teamController = new RunnerTeamController() | ||||
| 		teams.forEach(team => { | ||||
| 			teamController.remove(team.id, true) | ||||
| 		}); | ||||
| 		for (let team of runnerOrganisation.teams) { | ||||
| 			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); | ||||
| 		await this.runnerOrganisationRepository.delete({ id: runnerOrganisation.id }); | ||||
| 		await this.runnerOrganisationRepository.delete(organisation); | ||||
| 		return responseOrganisation; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| 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 { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; | ||||
| import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors'; | ||||
| import { CreateRunnerTeam } from '../models/creation/CreateRunnerTeam'; | ||||
| import { RunnerTeam } from '../models/entities/RunnerTeam'; | ||||
| @@ -87,8 +87,8 @@ export class RunnerTeamController { | ||||
| 	@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) | ||||
| 	@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 }) | ||||
| 	@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' }) | ||||
| 	async remove(@Param('id') id: number, @QueryParam("force") force: boolean) { | ||||
| 		let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact', 'runners'] }); | ||||
| 	async remove(@EntityFromParam('id') team: RunnerTeam, @QueryParam("force") force: boolean) { | ||||
| 		let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] }); | ||||
|  | ||||
| 		if (!runnerTeam) { | ||||
| 			throw new RunnerTeamNotFoundError(); | ||||
| @@ -100,12 +100,12 @@ export class RunnerTeamController { | ||||
| 			} | ||||
| 		} | ||||
| 		const runnerController = new RunnerController() | ||||
| 		await runnerTeam.runners.forEach(async runner => { | ||||
| 			await runnerController.remove(runner.id, true); | ||||
| 		}); | ||||
| 		for (let runner of runnerTeam.runners) { | ||||
| 			await runnerController.remove(runner, true); | ||||
| 		} | ||||
|  | ||||
| 		const responseTeam = new ResponseRunnerTeam(runnerTeam); | ||||
| 		await this.runnerTeamRepository.delete({ id: runnerTeam.id }); | ||||
| 		await this.runnerTeamRepository.delete(team); | ||||
| 		return responseTeam; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -42,6 +42,4 @@ export abstract class RunnerGroup { | ||||
|    */ | ||||
|   @OneToMany(() => Runner, runner => runner.group, { nullable: true }) | ||||
|   runners: Runner[]; | ||||
|  | ||||
|   public abstract getRunners(); | ||||
| } | ||||
| @@ -1,7 +1,6 @@ | ||||
| import { IsOptional } from "class-validator"; | ||||
| import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm"; | ||||
| import { ChildEntity, ManyToOne, OneToMany } from "typeorm"; | ||||
| import { Address } from "./Address"; | ||||
| import { Runner } from './Runner'; | ||||
| import { RunnerGroup } from "./RunnerGroup"; | ||||
| import { RunnerTeam } from "./RunnerTeam"; | ||||
|  | ||||
| @@ -24,27 +23,4 @@ export class RunnerOrganisation extends RunnerGroup { | ||||
|  */ | ||||
|   @OneToMany(() => RunnerTeam, team => team.parentGroup, { nullable: true }) | ||||
|   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 }); | ||||
|   } | ||||
| } | ||||
| @@ -1,6 +1,5 @@ | ||||
| import { IsNotEmpty } from "class-validator"; | ||||
| import { ChildEntity, getConnectionManager, ManyToOne } from "typeorm"; | ||||
| import { Runner } from './Runner'; | ||||
| import { ChildEntity, ManyToOne } from "typeorm"; | ||||
| import { RunnerGroup } from "./RunnerGroup"; | ||||
| import { RunnerOrganisation } from "./RunnerOrganisation"; | ||||
|  | ||||
| @@ -17,11 +16,4 @@ export class RunnerTeam extends RunnerGroup { | ||||
|   @IsNotEmpty() | ||||
|   @ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: true }) | ||||
|   parentGroup?: RunnerOrganisation; | ||||
|  | ||||
|   /** | ||||
|    * Returns all runners associated with this team. | ||||
|    */ | ||||
|   public async getRunners() { | ||||
|     return await getConnectionManager().get().getRepository(Runner).find({ group: this }); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user