parent
5b7f3ae12f
commit
3275b5fd80
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;
|
||||||
|
}
|
||||||
|
}
|
36
src/errors/UserGroupErrors.ts
Normal file
36
src/errors/UserGroupErrors.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { IsString } from 'class-validator';
|
||||||
|
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when no groupname is set
|
||||||
|
*/
|
||||||
|
export class GroupNameNeededError extends NotFoundError {
|
||||||
|
@IsString()
|
||||||
|
name = "GroupNameNeededError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "no groupname is set!"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when a usergroup couldn't be found.
|
||||||
|
*/
|
||||||
|
export class UserGroupNotFoundError extends NotFoundError {
|
||||||
|
@IsString()
|
||||||
|
name = "UserGroupNotFoundError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "User Group not found!"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when two usergroups' ids don't match.
|
||||||
|
* Usually occurs when a user tries to change a usergroups's id.
|
||||||
|
*/
|
||||||
|
export class UserGroupIdsNotMatchingError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "UserGroupIdsNotMatchingError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "The id's don't match!! \n If you wanted to change a usergroup's id: This isn't allowed"
|
||||||
|
}
|
28
src/models/creation/CreateUserGroup.ts
Normal file
28
src/models/creation/CreateUserGroup.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { IsOptional, IsString } from 'class-validator';
|
||||||
|
import { GroupNameNeededError } from '../../errors/UserGroupErrors';
|
||||||
|
import { UserGroup } from '../entities/UserGroup';
|
||||||
|
|
||||||
|
export class CreateUserGroup {
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
name: string;
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
description?: string;
|
||||||
|
|
||||||
|
public async toUserGroup(): Promise<UserGroup> {
|
||||||
|
let newUserGroup: UserGroup = new UserGroup();
|
||||||
|
|
||||||
|
if (this.name === undefined) {
|
||||||
|
throw new GroupNameNeededError();
|
||||||
|
}
|
||||||
|
|
||||||
|
newUserGroup.name = this.name
|
||||||
|
if (this.description) {
|
||||||
|
newUserGroup.description = this.description
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(newUserGroup)
|
||||||
|
return newUserGroup;
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,17 @@
|
|||||||
import { PrimaryGeneratedColumn, Column, OneToMany, Entity, ManyToOne } from "typeorm";
|
|
||||||
import {
|
import {
|
||||||
IsInt,
|
IsInt,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString
|
||||||
} from "class-validator";
|
} from "class-validator";
|
||||||
|
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
|
||||||
import { Permission } from "./Permission";
|
import { Permission } from "./Permission";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the UserGroup interface.
|
* Defines the UserGroup interface.
|
||||||
*/
|
*/
|
||||||
@Entity()
|
@Entity()
|
||||||
export abstract class UserGroup {
|
export class UserGroup {
|
||||||
/**
|
/**
|
||||||
* Autogenerated unique id (primary key).
|
* Autogenerated unique id (primary key).
|
||||||
*/
|
*/
|
||||||
@ -37,7 +37,7 @@ export abstract class UserGroup {
|
|||||||
/**
|
/**
|
||||||
* The group's description
|
* The group's description
|
||||||
*/
|
*/
|
||||||
@Column({nullable: true})
|
@Column({ nullable: true })
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
description?: string;
|
description?: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user