diff --git a/src/controllers/PermissionController.ts b/src/controllers/PermissionController.ts index 3998013..d6778df 100644 --- a/src/controllers/PermissionController.ts +++ b/src/controllers/PermissionController.ts @@ -1,9 +1,9 @@ -import { Get, JsonController } from 'routing-controllers'; +import { Get, JsonController, OnUndefined, Param } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; +import { PermissionNotFoundError } from '../errors/PermissionErrors'; import { Permission } from '../models/entities/Permission'; import { ResponsePermission } from '../models/responses/ResponsePermission'; -import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam'; @JsonController('/permissions') @@ -19,8 +19,8 @@ export class PermissionController { } @Get() - @ResponseSchema(ResponseRunnerTeam, { isArray: true }) - @OpenAPI({ description: 'Lists all runnerTeams.' }) + @ResponseSchema(ResponsePermission, { isArray: true }) + @OpenAPI({ description: 'Lists all permissions.' }) async getAll() { let responsePermissions: ResponsePermission[] = new Array(); const permissions = await this.permissionController.find({ relations: ['principal'] }); @@ -30,18 +30,19 @@ export class PermissionController { return responsePermissions; } - /* + @Get('/:id') - @ResponseSchema(ResponseRunnerTeam) - @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) - @OnUndefined(RunnerTeamNotFoundError) - @OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' }) + @ResponseSchema(ResponsePermission) + @ResponseSchema(PermissionNotFoundError, { statusCode: 404 }) + @OnUndefined(PermissionNotFoundError) + @OpenAPI({ description: 'Returns a permissions of a specified id (if it exists)' }) async getOne(@Param('id') id: number) { - let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }); - if (!runnerTeam) { throw new RunnerTeamNotFoundError(); } - return new ResponseRunnerTeam(runnerTeam); + let permission = await this.permissionController.findOne({ id: id }, { relations: ['principal'] }); + if (!permission) { throw new PermissionNotFoundError(); } + return new ResponsePermission(permission); } + /* @Post() @ResponseSchema(ResponseRunnerTeam) @OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' }) @@ -58,7 +59,7 @@ export class PermissionController { return new ResponseRunnerTeam(runnerTeam); } - + /* @Put('/:id') @ResponseSchema(ResponseRunnerTeam) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) diff --git a/src/errors/PermissionErrors.ts b/src/errors/PermissionErrors.ts new file mode 100644 index 0000000..e53b143 --- /dev/null +++ b/src/errors/PermissionErrors.ts @@ -0,0 +1,25 @@ +import { IsString } from 'class-validator'; +import { NotAcceptableError, NotFoundError } from 'routing-controllers'; + +/** + * Error to throw when a permission couldn't be found. + */ +export class PermissionNotFoundError extends NotFoundError { + @IsString() + name = "PermissionNotFoundError" + + @IsString() + message = "Permission not found!" +} + +/** + * Error to throw when two permission' ids don't match. + * Usually occurs when a user tries to change a permission's id. + */ +export class PermissionIdsNotMatchingError extends NotAcceptableError { + @IsString() + name = "PermissionIdsNotMatchingError" + + @IsString() + message = "The id's don't match!! \n And if you wanted to change a permission's id: This isn't allowed" +} \ No newline at end of file diff --git a/src/errors/PrincipalsErrors.ts b/src/errors/PrincipalsErrors.ts new file mode 100644 index 0000000..73e9819 --- /dev/null +++ b/src/errors/PrincipalsErrors.ts @@ -0,0 +1,13 @@ +import { IsString } from 'class-validator'; +import { NotFoundError } from 'routing-controllers'; + +/** + * Error to throw when a user couldn't be found. + */ +export class PrincipalNotFoundError extends NotFoundError { + @IsString() + name = "PrincipalNotFoundError" + + @IsString() + message = "Principal not found!" +} \ No newline at end of file