parent
0d5a2109d5
commit
a35e6d0a9f
87
src/controllers/RunnerOrganisationController.ts
Normal file
87
src/controllers/RunnerOrganisationController.ts
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
import { JsonController, Param, Body, Get, Post, Put, Delete, OnUndefined } from 'routing-controllers';
|
||||||
|
import { getConnectionManager, Repository } from 'typeorm';
|
||||||
|
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||||
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||||
|
import { RunnerOrganisation } from '../models/RunnerOrganisation';
|
||||||
|
import { RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors';
|
||||||
|
import { CreateRunnerOrganisation } from '../models/CreateRunnerOrganisation';
|
||||||
|
import { RunnerGroup } from '../models/RunnerGroup';
|
||||||
|
|
||||||
|
|
||||||
|
@JsonController('/organisations')
|
||||||
|
//@Authorized('RUNNERS:read')
|
||||||
|
export class RunnerOrganisationController {
|
||||||
|
private runnerOrganisationRepository: Repository<RunnerGroup>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the repository of this controller's model/entity.
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
this.runnerOrganisationRepository = getConnectionManager().get().getRepository(RunnerGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get()
|
||||||
|
@ResponseSchema(RunnerOrganisation, { isArray: true })
|
||||||
|
@OpenAPI({ description: 'Lists all runnerOrganisations.' })
|
||||||
|
getAll() {
|
||||||
|
return this.runnerOrganisationRepository.find();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('/:id')
|
||||||
|
@ResponseSchema(RunnerOrganisation)
|
||||||
|
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||||
|
@OnUndefined(RunnerOrganisationNotFoundError)
|
||||||
|
@OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' })
|
||||||
|
getOne(@Param('id') id: number) {
|
||||||
|
return this.runnerOrganisationRepository.findOne({ id: id });
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
@ResponseSchema(RunnerOrganisation)
|
||||||
|
@OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' })
|
||||||
|
async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) {
|
||||||
|
let runnerOrganisation;
|
||||||
|
try {
|
||||||
|
runnerOrganisation = await createRunnerOrganisation.toRunnerOrganisation();
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.runnerOrganisationRepository.save(runnerOrganisation);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Put('/:id')
|
||||||
|
@ResponseSchema(RunnerOrganisation)
|
||||||
|
@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) {
|
||||||
|
let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id });
|
||||||
|
|
||||||
|
if (!oldRunnerOrganisation) {
|
||||||
|
throw new RunnerOrganisationNotFoundError();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldRunnerOrganisation.id != runnerOrganisation.id) {
|
||||||
|
throw new RunnerOrganisationIdsNotMatchingError();
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation);
|
||||||
|
return runnerOrganisation;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete('/:id')
|
||||||
|
@ResponseSchema(RunnerOrganisation)
|
||||||
|
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||||
|
@OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' })
|
||||||
|
async remove(@Param('id') id: number) {
|
||||||
|
let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id });
|
||||||
|
|
||||||
|
if (!runnerOrganisation) {
|
||||||
|
throw new RunnerOrganisationNotFoundError();
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.runnerOrganisationRepository.delete(runnerOrganisation);
|
||||||
|
return runnerOrganisation;
|
||||||
|
}
|
||||||
|
}
|
27
src/errors/RunnerOrganisationErrors.ts
Normal file
27
src/errors/RunnerOrganisationErrors.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers';
|
||||||
|
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when a runner couldn't be found.
|
||||||
|
* Implemented this ways to work with the json-schema conversion for openapi.
|
||||||
|
*/
|
||||||
|
export class RunnerOrganisationNotFoundError extends NotFoundError {
|
||||||
|
@IsString()
|
||||||
|
name = "RunnerOrganisationNotFoundError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "RunnerOrganisation not found!"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when two runners' 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.
|
||||||
|
*/
|
||||||
|
export class RunnerOrganisationIdsNotMatchingError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "RunnerOrganisationIdsNotMatchingError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "The id's don't match!! \n And if you wanted to change a runner's id: This isn't allowed"
|
||||||
|
}
|
15
src/models/CreateRunnerOrganisation.ts
Normal file
15
src/models/CreateRunnerOrganisation.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { IsString } from 'class-validator';
|
||||||
|
import { RunnerOrganisation } from './RunnerOrganisation';
|
||||||
|
|
||||||
|
export class CreateRunnerOrganisation {
|
||||||
|
@IsString()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
public async toRunnerOrganisation(): Promise<RunnerOrganisation> {
|
||||||
|
let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation();
|
||||||
|
|
||||||
|
newRunnerOrganisation.name = this.name;
|
||||||
|
|
||||||
|
return newRunnerOrganisation;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user