import { Controller, Get, Post, Patch, Delete, Body, Param, UseGuards, Request, HttpCode, HttpStatus, ParseUUIDPipe, } from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { RoomsService } from './rooms.service'; import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; import { CreateRoomDto, UpdateRoomDto, AddRoomMemberDto } from './dto'; // Custom request interface with user interface RequestWithUser extends Request { user: { id: string; email: string; }; } @ApiTags('Rooms') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @Controller() export class RoomsController { constructor(private readonly roomsService: RoomsService) {} /** * POST /spaces/:spaceId/rooms * Create a new room in a space */ @Post('spaces/:spaceId/rooms') @HttpCode(HttpStatus.CREATED) async createRoom( @Param('spaceId') spaceId: string, @Body() dto: CreateRoomDto, @Request() req: RequestWithUser, ): Promise { const room = await this.roomsService.createRoom(spaceId, req.user.id, dto); return { success: true, data: room, }; } /** * GET /rooms/:roomId * Get room details */ @Get('rooms/:roomId') @HttpCode(HttpStatus.OK) async getRoom( @Param('roomId', ParseUUIDPipe) roomId: string, @Request() req: RequestWithUser, ): Promise { const room = await this.roomsService.getRoomById(roomId, req.user.id); return { success: true, data: room, }; } /** * PATCH /rooms/:roomId * Update room */ @Patch('rooms/:roomId') @HttpCode(HttpStatus.OK) async updateRoom( @Param('roomId') roomId: string, @Body() dto: UpdateRoomDto, @Request() req: RequestWithUser, ): Promise { const room = await this.roomsService.updateRoom(roomId, req.user.id, dto); return { success: true, data: room, }; } /** * DELETE /rooms/:roomId * Delete room */ @Delete('rooms/:roomId') @HttpCode(HttpStatus.OK) async deleteRoom( @Param('roomId') roomId: string, @Request() req: RequestWithUser, ): Promise { await this.roomsService.deleteRoom(roomId, req.user.id); return { success: true, message: 'Room deleted successfully', }; } /** * GET /spaces/:spaceId/rooms * Get all rooms in a space */ @Get('spaces/:spaceId/rooms') @HttpCode(HttpStatus.OK) async getSpaceRooms( @Param('spaceId') spaceId: string, @Request() req: RequestWithUser, ): Promise { const rooms = await this.roomsService.getSpaceRooms(spaceId, req.user.id); return { success: true, data: rooms, }; } /** * GET /rooms/:roomId/members * Get room members */ @Get('rooms/:roomId/members') @HttpCode(HttpStatus.OK) async getRoomMembers( @Param('roomId', ParseUUIDPipe) roomId: string, @Request() req: RequestWithUser, ): Promise { const members = await this.roomsService.getRoomMembers(roomId, req.user.id); return { success: true, data: members, }; } /** * POST /rooms/:roomId/members * Add member to room */ @Post('rooms/:roomId/members') @HttpCode(HttpStatus.CREATED) async addRoomMember( @Param('roomId', ParseUUIDPipe) roomId: string, @Body() dto: AddRoomMemberDto, @Request() req: RequestWithUser, ): Promise { // Get room to find space_id const room = await this.roomsService.getRoomById(roomId); await this.roomsService.addRoomMember( roomId, room.space_id, req.user.id, dto, ); return { success: true, message: 'Member added successfully', }; } /** * DELETE /rooms/:roomId/members/:userId * Remove member from room */ @Delete('rooms/:roomId/members/:userId') @HttpCode(HttpStatus.OK) async removeRoomMember( @Param('roomId', ParseUUIDPipe) roomId: string, @Param('userId', ParseUUIDPipe) userId: string, @Request() req: RequestWithUser, ): Promise { await this.roomsService.removeRoomMember(roomId, userId, req.user.id); return { success: true, message: 'Member removed successfully', }; } /** * GET /rooms/:roomId/stats * Get room statistics */ @Get('rooms/:roomId/stats') @HttpCode(HttpStatus.OK) async getRoomStats( @Param('roomId', ParseUUIDPipe) roomId: string, @Request() req: RequestWithUser, ): Promise { const stats = await this.roomsService.getRoomStats(roomId, req.user.id); return { success: true, data: stats, }; } }