parent
cc5a30980a
commit
efecffb72d
|
@ -1,4 +1,4 @@
|
|||
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
|
||||
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
|
||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||
|
@ -7,6 +7,7 @@ import { UserGroupNotFoundError } from '../errors/UserGroupErrors';
|
|||
import { CreateUser } from '../models/actions/CreateUser';
|
||||
import { User } from '../models/entities/User';
|
||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||
import { ResponseUser } from '../models/responses/ResponseUser';
|
||||
|
||||
|
||||
@JsonController('/users')
|
||||
|
@ -23,8 +24,13 @@ export class UserController {
|
|||
@Get()
|
||||
@ResponseSchema(User, { isArray: true })
|
||||
@OpenAPI({ description: 'Lists all users.' })
|
||||
getAll() {
|
||||
return this.userRepository.find();
|
||||
async getAll() {
|
||||
let responseUsers: ResponseUser[] = new Array<ResponseUser>();
|
||||
const users = await this.userRepository.find({ relations: ['permissions', 'groups'] });
|
||||
users.forEach(user => {
|
||||
responseUsers.push(new ResponseUser(user));
|
||||
});
|
||||
return responseUsers;
|
||||
}
|
||||
|
||||
@Get('/:id')
|
||||
|
@ -32,8 +38,10 @@ export class UserController {
|
|||
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
|
||||
@OnUndefined(UserNotFoundError)
|
||||
@OpenAPI({ description: 'Returns a user of a specified id (if it exists)' })
|
||||
getOne(@Param('id') id: number) {
|
||||
return this.userRepository.findOne({ id: id });
|
||||
async getOne(@Param('id') id: number) {
|
||||
let user = await this.userRepository.findOne({ id: id }, { relations: ['permissions', 'groups'] })
|
||||
if (!user) { throw new UserNotFoundError(); }
|
||||
return new ResponseUser(user);
|
||||
}
|
||||
|
||||
@Post()
|
||||
|
@ -48,7 +56,8 @@ export class UserController {
|
|||
throw error;
|
||||
}
|
||||
|
||||
return this.userRepository.save(user);
|
||||
user = await this.userRepository.save(user)
|
||||
return new ResponseUser(await this.userRepository.findOne(user, { relations: ['permissions', 'groups'] }));
|
||||
}
|
||||
|
||||
@Put('/:id')
|
||||
|
@ -57,7 +66,7 @@ export class UserController {
|
|||
@ResponseSchema(UserIdsNotMatchingError, { statusCode: 406 })
|
||||
@OpenAPI({ description: "Update a user object (id can't be changed)." })
|
||||
async put(@Param('id') id: number, @EntityFromBody() user: User) {
|
||||
let oldUser = await this.userRepository.findOne({ id: id });
|
||||
let oldUser = await this.userRepository.findOne({ id: id }, { relations: ['permissions', 'groups'] });
|
||||
|
||||
if (!oldUser) {
|
||||
throw new UserNotFoundError();
|
||||
|
@ -68,7 +77,7 @@ export class UserController {
|
|||
}
|
||||
|
||||
await this.userRepository.update(oldUser, user);
|
||||
return user;
|
||||
return new ResponseUser(await this.userRepository.findOne({ id: id }, { relations: ['permissions', 'groups'] }));
|
||||
}
|
||||
|
||||
@Delete('/:id')
|
||||
|
@ -76,13 +85,16 @@ export class UserController {
|
|||
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
||||
@OnUndefined(204)
|
||||
@OpenAPI({ description: 'Delete a specified runner (if it exists).' })
|
||||
async remove(@Param("id") id: number) {
|
||||
let user = await this.userRepository.findOne({ id: id });
|
||||
if (!user) {
|
||||
return null;
|
||||
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
||||
let runner = await this.userRepository.findOne({ id: id });
|
||||
if (!runner) { return null; }
|
||||
const responseUser = await this.userRepository.findOne(runner, { relations: ['permissions', 'groups'] });
|
||||
|
||||
if (!runner) {
|
||||
throw new UserNotFoundError();
|
||||
}
|
||||
|
||||
await this.userRepository.delete(user);
|
||||
return user;
|
||||
await this.userRepository.delete(runner);
|
||||
return new ResponseUser(responseUser);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsInt,
|
||||
IsOptional,
|
||||
IsString
|
||||
} from "class-validator";
|
||||
import { Permission } from '../entities/Permission';
|
||||
import { User } from '../entities/User';
|
||||
import { UserGroup } from '../entities/UserGroup';
|
||||
|
||||
/**
|
||||
* Defines a user response.
|
||||
*/
|
||||
export class ResponseUser {
|
||||
/**
|
||||
* Autogenerated unique id (primary key).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;;
|
||||
|
||||
/**
|
||||
* The user's first name.
|
||||
*/
|
||||
@IsString()
|
||||
firstname: string;
|
||||
|
||||
/**
|
||||
* The user's middle name.
|
||||
* Optional.
|
||||
*/
|
||||
@IsString()
|
||||
middlename?: string;
|
||||
|
||||
/**
|
||||
* The user's last name.
|
||||
*/
|
||||
@IsString()
|
||||
lastname: string;
|
||||
|
||||
/**
|
||||
* The user's phone number.
|
||||
* Optional.
|
||||
*/
|
||||
@IsString()
|
||||
phone?: string;
|
||||
|
||||
/**
|
||||
* The user's e-mail address.
|
||||
* Optional.
|
||||
*/
|
||||
@IsString()
|
||||
email?: string;
|
||||
|
||||
/**
|
||||
* is user enabled?
|
||||
*/
|
||||
@IsBoolean()
|
||||
enabled: boolean = true;
|
||||
|
||||
/**
|
||||
* profilepic
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
profilePic?: string;
|
||||
|
||||
/**
|
||||
* Groups
|
||||
*/
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
groups: UserGroup[];
|
||||
|
||||
/**
|
||||
* permissions
|
||||
*/
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
permissions: Permission[];
|
||||
|
||||
public constructor(user: User) {
|
||||
this.id = user.id;
|
||||
this.firstname = user.firstname;
|
||||
this.middlename = user.middlename;
|
||||
this.lastname = user.lastname;
|
||||
this.phone = user.phone;
|
||||
this.email = user.email;
|
||||
this.enabled = user.enabled;
|
||||
this.profilePic = user.profilePic;
|
||||
this.groups = user.groups;
|
||||
this.permissions = user.permissions;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue