85 lines
2.7 KiB
TypeScript
85 lines
2.7 KiB
TypeScript
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, EntityFromParam } from 'typeorm-routing-controllers-extensions';
|
|
import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors';
|
|
import { CreateUserGroup } from '../models/actions/CreateUserGroup';
|
|
import { UserGroup } from '../models/entities/UserGroup';
|
|
|
|
|
|
@JsonController('/usergroups')
|
|
export class UserGroupController {
|
|
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) {
|
|
throw 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(@EntityFromParam('id') group: UserGroup) {
|
|
if (!group) {
|
|
throw new UserGroupNotFoundError();
|
|
}
|
|
|
|
await this.userGroupsRepository.delete(group);
|
|
return group;
|
|
}
|
|
}
|