From 0c6528bdc5eb314399bc722087e3b907f0f59ff8 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 4 Dec 2020 17:06:37 +0100 Subject: [PATCH] Impementing more methods for the runner orgs --- .../RunnerOrganisationController.ts | 23 +++++++++---- src/errors/RunnerOrganisationErrors.ts | 34 ++++++++++++++++--- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts index 8033c9b..0bae97f 100644 --- a/src/controllers/RunnerOrganisationController.ts +++ b/src/controllers/RunnerOrganisationController.ts @@ -1,11 +1,10 @@ -import { JsonController, Param, Body, Get, Post, Put, Delete, OnUndefined } from 'routing-controllers'; +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 { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; -import { RunnerOrganisation } from '../models/entities/RunnerOrganisation'; -import { RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; +import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganisation'; -import { RunnerGroup } from '../models/entities/RunnerGroup'; +import { RunnerOrganisation } from '../models/entities/RunnerOrganisation'; @JsonController('/organisations') @@ -74,13 +73,25 @@ export class RunnerOrganisationController { @ResponseSchema(RunnerOrganisation) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' }) - async remove(@Param('id') id: number) { + async remove(@Param('id') id: number, @QueryParam("force") force) { let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }); if (!runnerOrganisation) { throw new RunnerOrganisationNotFoundError(); } + if (!force) { + if (runnerOrganisation.runners.length != 0) { + throw new RunnerOrganisationHasRunnersError(); + } + + if (runnerOrganisation.teams.length != 0) { + throw new RunnerOrganisationHasTeamsError(); + } + } + + //TODO: Delete runner and teams + await this.runnerOrganisationRepository.delete(runnerOrganisation); return runnerOrganisation; } diff --git a/src/errors/RunnerOrganisationErrors.ts b/src/errors/RunnerOrganisationErrors.ts index f846ffa..1dfb68e 100644 --- a/src/errors/RunnerOrganisationErrors.ts +++ b/src/errors/RunnerOrganisationErrors.ts @@ -1,8 +1,8 @@ -import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers'; -import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator'; +import { IsString } from 'class-validator'; +import { NotAcceptableError, NotFoundError } from 'routing-controllers'; /** - * Error to throw when a runner couldn't be found. + * Error to throw when a runner organisation couldn't be found. * Implemented this ways to work with the json-schema conversion for openapi. */ export class RunnerOrganisationNotFoundError extends NotFoundError { @@ -14,9 +14,9 @@ export class RunnerOrganisationNotFoundError extends NotFoundError { } /** - * Error to throw when two runners' ids don't match. + * Error to throw when two runner organisations' ids don't match. * Usually occurs when a user tries to change a runner's id. - * Implemented this ways to work with the json-schema conversion for openapi. + * Implemented this way to work with the json-schema conversion for openapi. */ export class RunnerOrganisationIdsNotMatchingError extends NotAcceptableError { @IsString() @@ -24,4 +24,28 @@ export class RunnerOrganisationIdsNotMatchingError extends NotAcceptableError { @IsString() message = "The id's don't match!! \n And if you wanted to change a runner's id: This isn't allowed" +} + +/** + * Error to throw when a organisation still has runners associated. + * Implemented this waysto work with the json-schema conversion for openapi. + */ +export class RunnerOrganisationHasRunnersError extends NotAcceptableError { + @IsString() + name = "RunnerOrganisationHasRunnersError" + + @IsString() + message = "This organisation still has runners associated with it. \n If you want to delete this organisation with all it's runners and teams ass `?force` to your query." +} + +/** + * Error to throw when a organisation still has runners associated. + * Implemented this waysto work with the json-schema conversion for openapi. + */ +export class RunnerOrganisationHasTeamsError extends NotAcceptableError { + @IsString() + name = "RunnerOrganisationHasTeamsError" + + @IsString() + message = "This organisation still has teams associated with it. \n If you want to delete this organisation with all it's runners and teams ass `?force` to your query." } \ No newline at end of file