Deletes now work based on EntityFromParam

ref #14
This commit is contained in:
Nicolai Ort 2020-12-05 12:24:38 +01:00
parent 0e3cf07b91
commit 8870b26ce6
2 changed files with 10 additions and 14 deletions

View File

@ -1,7 +1,7 @@
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 { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions';
import { UserGroupNotFoundError, UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors';
import { CreateUser } from '../models/creation/CreateUser';
import { User } from '../models/entities/User';
@ -73,14 +73,12 @@ export class UserController {
@ResponseSchema(User)
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
@OpenAPI({ description: 'Delete a specified runner (if it exists).' })
async remove(@Param('id') id: number) {
let runner = await this.userRepository.findOne({ id: id });
if (!runner) {
async remove(@EntityFromParam('id') user: User) {
if (!user) {
throw new UserNotFoundError();
}
await this.userRepository.delete(runner);
return runner;
await this.userRepository.delete(user);
return user;
}
}

View File

@ -1,7 +1,7 @@
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 { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions';
import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors';
import { CreateUserGroup } from '../models/creation/CreateUserGroup';
import { UserGroup } from '../models/entities/UserGroup';
@ -73,14 +73,12 @@ export class UserGroupController {
@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) {
async remove(@EntityFromParam('id') group: UserGroup) {
if (!group) {
throw new UserGroupNotFoundError();
}
await this.userGroupsRepository.delete(userGroup);
return userGroup;
await this.userGroupsRepository.delete(group);
return group;
}
}