88 lines
3.3 KiB
TypeScript
88 lines
3.3 KiB
TypeScript
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;
|
|
}
|
|
}
|