From 0d8fbf1eca512c3a14cd4ca09b0eda820742ed43 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 17 Dec 2020 17:25:17 +0100 Subject: [PATCH] Consolidated the json import for a cleaner result ref #22 --- src/controllers/ImportController.ts | 7 ++- src/models/actions/ImportRunner.ts | 58 +++++++++++++++++++++++- src/models/actions/ImportSchoolRunner.ts | 56 ----------------------- 3 files changed, 59 insertions(+), 62 deletions(-) delete mode 100644 src/models/actions/ImportSchoolRunner.ts diff --git a/src/controllers/ImportController.ts b/src/controllers/ImportController.ts index d82bf8c..48d3f78 100644 --- a/src/controllers/ImportController.ts +++ b/src/controllers/ImportController.ts @@ -3,7 +3,6 @@ import { OpenAPI } from 'routing-controllers-openapi'; import { RunnerGroupNeededError } from '../errors/RunnerErrors'; import RawBodyMiddleware from '../middlewares/RawBody'; import { ImportRunner } from '../models/actions/ImportRunner'; -import { ImportSchoolRunner } from '../models/actions/ImportSchoolRunner'; import { ResponseRunner } from '../models/responses/ResponseRunner'; import { RunnerController } from './RunnerController'; @@ -22,7 +21,7 @@ export class ImportController { @Post('/runners/import') @ContentType("application/json") @OpenAPI({ description: "Create new runners from json" }) - async postJSON(@Body({ validate: true, type: ImportSchoolRunner }) importRunners: ImportRunner[], @QueryParam("group") groupID: number) { + async postJSON(@Body({ validate: true, type: ImportRunner }) importRunners: ImportRunner[], @QueryParam("group") groupID: number) { if (!groupID) { throw new RunnerGroupNeededError(); } let responseRunners: ResponseRunner[] = new Array(); for await (let runner of importRunners) { @@ -34,14 +33,14 @@ export class ImportController { @Post('/organisations/:id/import') @ContentType("application/json") @OpenAPI({ description: "Create new runners from json" }) - async postOrgsJSON(@Body({ validate: true, type: ImportSchoolRunner }) importRunners: ImportRunner[], @Param('id') id: number) { + async postOrgsJSON(@Body({ validate: true, type: ImportRunner }) importRunners: ImportRunner[], @Param('id') id: number) { return await this.postJSON(importRunners, id) } @Post('/teams/:id/import') @ContentType("application/json") @OpenAPI({ description: "Create new runners from json" }) - async postTeamsJSON(@Body({ validate: true, type: ImportSchoolRunner }) importRunners: ImportRunner[], @Param('id') id: number) { + async postTeamsJSON(@Body({ validate: true, type: ImportRunner }) importRunners: ImportRunner[], @Param('id') id: number) { return await this.postJSON(importRunners, id) } diff --git a/src/models/actions/ImportRunner.ts b/src/models/actions/ImportRunner.ts index ba7daa2..3f5d97e 100644 --- a/src/models/actions/ImportRunner.ts +++ b/src/models/actions/ImportRunner.ts @@ -1,6 +1,13 @@ import { IsNotEmpty, IsOptional, IsString } from 'class-validator'; +import { getConnectionManager } from 'typeorm'; +import { RunnerGroupNeededError } from '../../errors/RunnerErrors'; +import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors'; +import { RunnerGroup } from '../entities/RunnerGroup'; +import { RunnerOrganisation } from '../entities/RunnerOrganisation'; +import { RunnerTeam } from '../entities/RunnerTeam'; +import { CreateRunner } from './CreateRunner'; -export abstract class ImportRunner { +export class ImportRunner { /** * The new runner's first name. @@ -24,5 +31,52 @@ export abstract class ImportRunner { @IsNotEmpty() lastname: string; - public abstract toCreateRunner(groupID?: number); + /** + * The new runner's class (if not provided otherwise). + */ + @IsString() + @IsOptional() + team?: string; + + @IsOptional() + @IsString() + public set class(value: string) { + this.team = value; + } + + public async toCreateRunner(groupID: number): Promise { + let newRunner: CreateRunner = new CreateRunner(); + + newRunner.firstname = this.firstname; + newRunner.middlename = this.middlename; + newRunner.lastname = this.lastname; + newRunner.group = (await this.getGroup(groupID)).id; + + return newRunner; + } + + public async getGroup(groupID: number): Promise { + if (this.team === undefined && groupID === undefined) { + throw new RunnerGroupNeededError(); + } + + let team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: groupID }); + if (team) { return team; } + + let org = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: groupID }); + if (!org) { + throw new RunnerOrganisationNotFoundError(); + } + if (this.team === undefined) { return org; } + + team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ name: this.team, parentGroup: org }); + if (!team) { + let newRunnerTeam: RunnerTeam = new RunnerTeam(); + newRunnerTeam.name = this.team; + newRunnerTeam.parentGroup = org; + team = await getConnectionManager().get().getRepository(RunnerTeam).save(newRunnerTeam); + } + + return team; + } } \ No newline at end of file diff --git a/src/models/actions/ImportSchoolRunner.ts b/src/models/actions/ImportSchoolRunner.ts deleted file mode 100644 index a12b9ec..0000000 --- a/src/models/actions/ImportSchoolRunner.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { IsNotEmpty, IsString } from 'class-validator'; -import { getConnectionManager } from 'typeorm'; -import { RunnerGroupNeededError } from '../../errors/RunnerErrors'; -import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors'; -import { RunnerGroup } from '../entities/RunnerGroup'; -import { RunnerOrganisation } from '../entities/RunnerOrganisation'; -import { RunnerTeam } from '../entities/RunnerTeam'; -import { CreateRunner } from './CreateRunner'; -import { ImportRunner } from './ImportRunner'; - -/** - * A special class for importing runners from tabels provided by schools. - */ -export class ImportSchoolRunner extends ImportRunner { - /** - * The pupils's class. - */ - @IsString() - @IsNotEmpty() - class: string; - - public async toCreateRunner(groupID: number): Promise { - let newRunner: CreateRunner = new CreateRunner(); - - newRunner.firstname = this.firstname; - newRunner.middlename = this.middlename; - newRunner.lastname = this.lastname; - newRunner.group = (await this.getGroup(groupID)).id; - - return newRunner; - } - - public async getGroup(groupID: number): Promise { - if (this.class === undefined && groupID === undefined) { - throw new RunnerGroupNeededError(); - } - - let team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: groupID }); - if (team) { return team; } - - let org = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: groupID }); - if (!org) { - throw new RunnerOrganisationNotFoundError(); - } - - team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ name: this.class, parentGroup: org }); - if (!team) { - let newRunnerTeam: RunnerTeam = new RunnerTeam(); - newRunnerTeam.name = this.class; - newRunnerTeam.parentGroup = org; - team = await getConnectionManager().get().getRepository(RunnerTeam).save(newRunnerTeam); - } - - return team; - } -} \ No newline at end of file