delete rooms

This commit is contained in:
Evert Prants 2023-01-19 17:08:17 +02:00
parent 04aa26288f
commit 6207554c99
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
4 changed files with 36 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import {
Body,
ClassSerializerInterceptor,
Controller,
Delete,
Get,
Param,
ParseIntPipe,
@ -18,6 +19,7 @@ import {
ApiBody,
ApiNotFoundResponse,
ApiParam,
ApiNoContentResponse,
} from '@nestjs/swagger';
import { User } from 'src/objects/user/user.entity';
import { LoggedInUser } from 'src/shared/decorators/user.decorator';
@ -200,4 +202,17 @@ export class AppBuildingController {
): Promise<BuildingRoomResponseDto> {
return this.service.updateRoom(user, body, id, roomId);
}
@Delete(':id/rooms/:roomId')
@ApiParam({ name: 'id', description: 'Building ID' })
@ApiParam({ name: 'roomId', description: 'Room ID' })
@ApiOperation({ summary: 'Delete room by ID' })
@ApiNoContentResponse()
async deleteRoomById(
@Param('id', ParseIntPipe) id: number,
@Param('roomId', ParseIntPipe) roomId: number,
@LoggedInUser() user: User,
): Promise<BuildingRoomResponseDto> {
return this.service.deleteRoom(user, id, roomId);
}
}

View File

@ -220,4 +220,20 @@ export class AppBuildingService {
return room;
}
async deleteRoom(user: User, buildingId: number, roomId: number) {
const room = await this.buildingService.getRoomByBuildingAndUserSub(
buildingId,
roomId,
user.sub,
);
if (!room) {
throw new NotFoundException('Room not found');
}
await this.buildingService.deleteRoom(room);
return room;
}
}

View File

@ -40,6 +40,6 @@ async function bootstrap() {
whitelist: true,
}),
);
await app.listen(+configService.get('PORT'));
await app.listen(+configService.get('PORT'), configService.get('HOST'));
}
bootstrap();

View File

@ -200,4 +200,8 @@ export class BuildingService {
Object.assign(newRoom, data);
return this.roomRepository.save(newRoom);
}
async deleteRoom(room: Room) {
return this.roomRepository.remove(room);
}
}