Moded group updateing to a updateusergroup action model
All checks were successful
continuous-integration/drone/pr Build is passing
All checks were successful
continuous-integration/drone/pr Build is passing
ref #76
This commit is contained in:
parent
dc6ec23cb9
commit
be4050768e
@ -4,6 +4,7 @@ import { getConnectionManager, Repository } from 'typeorm';
|
|||||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||||
import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors';
|
import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors';
|
||||||
import { CreateUserGroup } from '../models/actions/create/CreateUserGroup';
|
import { CreateUserGroup } from '../models/actions/create/CreateUserGroup';
|
||||||
|
import { UpdateUserGroup } from '../models/actions/update/UpdateUserGroup';
|
||||||
import { UserGroup } from '../models/entities/UserGroup';
|
import { UserGroup } from '../models/entities/UserGroup';
|
||||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||||
import { ResponseUserGroup } from '../models/responses/ResponseUserGroup';
|
import { ResponseUserGroup } from '../models/responses/ResponseUserGroup';
|
||||||
@ -62,19 +63,19 @@ export class UserGroupController {
|
|||||||
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
|
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
|
||||||
@ResponseSchema(UserGroupIdsNotMatchingError, { statusCode: 406 })
|
@ResponseSchema(UserGroupIdsNotMatchingError, { statusCode: 406 })
|
||||||
@OpenAPI({ description: "Update the group whose id you provided. <br> To change the permissions granted to the group please use /api/permissions instead. <br> Please remember that ids can't be changed." })
|
@OpenAPI({ description: "Update the group whose id you provided. <br> To change the permissions granted to the group please use /api/permissions instead. <br> Please remember that ids can't be changed." })
|
||||||
async put(@Param('id') id: number, @EntityFromBody() userGroup: UserGroup) {
|
async put(@Param('id') id: number, @EntityFromBody() updateGroup: UpdateUserGroup) {
|
||||||
let oldUserGroup = await this.userGroupsRepository.findOne({ id: id }, { relations: ["permissions"] });
|
let oldGroup = await this.userGroupsRepository.findOne({ id: id });
|
||||||
|
|
||||||
if (!oldUserGroup) {
|
if (!oldGroup) {
|
||||||
throw new UserGroupNotFoundError()
|
throw new UserGroupNotFoundError();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldUserGroup.id != userGroup.id) {
|
if (oldGroup.id != updateGroup.id) {
|
||||||
throw new UserGroupIdsNotMatchingError();
|
throw new UserGroupIdsNotMatchingError();
|
||||||
}
|
}
|
||||||
|
await this.userGroupsRepository.save(await updateGroup.update(oldGroup));
|
||||||
|
|
||||||
await this.userGroupsRepository.save(userGroup);
|
return (await this.userGroupsRepository.findOne({ id: id }, { relations: ['permissions', 'groups'] })).toResponse();
|
||||||
return userGroup;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete('/:id')
|
@Delete('/:id')
|
||||||
|
39
src/models/actions/update/UpdateUserGroup.ts
Normal file
39
src/models/actions/update/UpdateUserGroup.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import { IsInt, IsOptional, IsString } from 'class-validator';
|
||||||
|
import { UserGroup } from '../../entities/UserGroup';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is used to update a UserGroup entity (via put request).
|
||||||
|
*/
|
||||||
|
export class UpdateUserGroup {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The updated group's id.
|
||||||
|
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The updated group's name.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The updated groups's description.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
description?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates a group entity based on this.
|
||||||
|
* @param group The group that shall be updated.
|
||||||
|
*/
|
||||||
|
public async update(group: UserGroup): Promise<UserGroup> {
|
||||||
|
group.name = this.name;
|
||||||
|
group.description = this.description;
|
||||||
|
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user