86
src/controllers/UserGroupController.ts
Normal file
86
src/controllers/UserGroupController.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
|
||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||
import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors';
|
||||
import { CreateUserGroup } from '../models/creation/CreateUserGroup';
|
||||
import { UserGroup } from '../models/entities/UserGroup';
|
||||
|
||||
|
||||
@JsonController('/usergroups')
|
||||
export class UserController {
|
||||
private userGroupsRepository: Repository<UserGroup>;
|
||||
|
||||
/**
|
||||
* Gets the repository of this controller's model/entity.
|
||||
*/
|
||||
constructor() {
|
||||
this.userGroupsRepository = getConnectionManager().get().getRepository(UserGroup);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ResponseSchema(UserGroup, { isArray: true })
|
||||
@OpenAPI({ description: 'Lists all usergroups.' })
|
||||
getAll() {
|
||||
return this.userGroupsRepository.find();
|
||||
}
|
||||
|
||||
@Get('/:id')
|
||||
@ResponseSchema(UserGroup)
|
||||
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
|
||||
@OnUndefined(UserGroupNotFoundError)
|
||||
@OpenAPI({ description: 'Returns a usergroup of a specified id (if it exists)' })
|
||||
getOne(@Param('id') id: number) {
|
||||
return this.userGroupsRepository.findOne({ id: id });
|
||||
}
|
||||
|
||||
@Post()
|
||||
@ResponseSchema(UserGroup)
|
||||
@ResponseSchema(UserGroupNotFoundError)
|
||||
@OpenAPI({ description: 'Create a new usergroup object (id will be generated automagicly).' })
|
||||
async post(@Body({ validate: true }) createUserGroup: CreateUserGroup) {
|
||||
let userGroup;
|
||||
try {
|
||||
userGroup = await createUserGroup.toUserGroup();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
return this.userGroupsRepository.save(userGroup);
|
||||
}
|
||||
|
||||
@Put('/:id')
|
||||
@ResponseSchema(UserGroup)
|
||||
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
|
||||
@ResponseSchema(UserGroupIdsNotMatchingError, { statusCode: 406 })
|
||||
@OpenAPI({ description: "Update a usergroup object (id can't be changed)." })
|
||||
async put(@Param('id') id: number, @EntityFromBody() userGroup: UserGroup) {
|
||||
let oldUserGroup = await this.userGroupsRepository.findOne({ id: id });
|
||||
|
||||
if (!oldUserGroup) {
|
||||
throw new UserGroupNotFoundError()
|
||||
}
|
||||
|
||||
if (oldUserGroup.id != userGroup.id) {
|
||||
throw new UserGroupIdsNotMatchingError();
|
||||
}
|
||||
|
||||
await this.userGroupsRepository.update(oldUserGroup, userGroup);
|
||||
return userGroup;
|
||||
}
|
||||
|
||||
@Delete('/:id')
|
||||
@ResponseSchema(UserGroup)
|
||||
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
|
||||
@OpenAPI({ description: 'Delete a specified usergroup (if it exists).' })
|
||||
async remove(@Param('id') id: number) {
|
||||
let userGroup = await this.userGroupsRepository.findOne({ id: id });
|
||||
|
||||
if (!userGroup) {
|
||||
throw new UserGroupNotFoundError();
|
||||
}
|
||||
|
||||
await this.userGroupsRepository.delete(userGroup);
|
||||
return userGroup;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user