From 532b5a56a5226d524f864bb8e965b4f57483101b Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sun, 20 Dec 2020 18:07:33 +0100 Subject: [PATCH] Switched runner orgs to the cleaner syntax via a update entity ref #39 --- .../RunnerOrganisationController.ts | 11 ++-- .../actions/UpdateRunnerOrganisation.ts | 52 +++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 src/models/actions/UpdateRunnerOrganisation.ts diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts index 5504dbb..fb86cf2 100644 --- a/src/controllers/RunnerOrganisationController.ts +++ b/src/controllers/RunnerOrganisationController.ts @@ -1,9 +1,9 @@ import { Authorized, 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 { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; import { CreateRunnerOrganisation } from '../models/actions/CreateRunnerOrganisation'; +import { UpdateRunnerOrganisation } from '../models/actions/UpdateRunnerOrganisation'; import { RunnerOrganisation } from '../models/entities/RunnerOrganisation'; import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation'; @@ -71,21 +71,20 @@ export class RunnerOrganisationController { @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 }) @OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." }) - async put(@Param('id') id: number, @EntityFromBody() runnerOrganisation: RunnerOrganisation) { + async put(@Param('id') id: number, @Body({ validate: true }) updateOrganisation: UpdateRunnerOrganisation) { let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }); if (!oldRunnerOrganisation) { throw new RunnerOrganisationNotFoundError(); } - if (oldRunnerOrganisation.id != runnerOrganisation.id) { + if (oldRunnerOrganisation.id != updateOrganisation.id) { throw new RunnerOrganisationIdsNotMatchingError(); } - await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation); + await this.runnerOrganisationRepository.save(await updateOrganisation.updateRunnerOrganisation(oldRunnerOrganisation)); - runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }); - return new ResponseRunnerOrganisation(runnerOrganisation); + return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(id, { relations: ['address', 'contact', 'teams'] })); } @Delete('/:id') diff --git a/src/models/actions/UpdateRunnerOrganisation.ts b/src/models/actions/UpdateRunnerOrganisation.ts new file mode 100644 index 0000000..19d5d34 --- /dev/null +++ b/src/models/actions/UpdateRunnerOrganisation.ts @@ -0,0 +1,52 @@ +import { IsInt, IsOptional } from 'class-validator'; +import { getConnectionManager } from 'typeorm'; +import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; +import { Address } from '../entities/Address'; +import { RunnerOrganisation } from '../entities/RunnerOrganisation'; +import { CreateRunnerGroup } from './CreateRunnerGroup'; + +export class UpdateRunnerOrganisation extends CreateRunnerGroup { + + /** + * The updated orgs's id. + */ + @IsInt() + id: number; + + /** + * The updated organisation's address. + * Must be of type number (address id). + * Optional. + */ + @IsInt() + @IsOptional() + address?: number; + + /** + * Get's this org's address from this.address. + */ + public async getAddress(): Promise
{ + if (this.address === undefined) { + return null; + } + if (!isNaN(this.address)) { + let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); + if (!address) { throw new AddressNotFoundError; } + return address; + } + + throw new AddressWrongTypeError; + } + + /** + * Creates a RunnerTeam entity from this. + */ + public async updateRunnerOrganisation(organisation: RunnerOrganisation): Promise { + + organisation.name = this.name; + organisation.contact = await this.getContact(); + organisation.address = await this.getAddress(); + + return organisation; + } +} \ No newline at end of file