changed return value of controller for void

This commit is contained in:
simplonco
2023-01-11 10:59:54 +01:00
parent e97de4f137
commit 9db3c053ea
6 changed files with 48 additions and 47 deletions

View File

@@ -5,7 +5,7 @@ import { ChatService } from './chat.service';
import { User } from 'src/users/entities/user.entity';
import { PartialUsersDto } from 'src/users/dto/partial-users.dto';
import { createRoomDto } from './dto/createRoom.dto';
import { joinRoomDto } from './dto/joinRoom.dto';
import { roomDto } from './dto/room.dto';
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
import { ChatGateway } from './chat.gateway';
import { socketDto } from './dto/socket.dto';
@@ -28,47 +28,47 @@ export class ChatController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('myrooms')
async getMyRooms(@Req() req, @Res() res): Promise<object>
async getMyRooms(@Req() req, @Res() res): Promise<void>
{
console.log("- in getMyRooms controller");
const rooms = await this.chatService.getMyRooms(req.user.username);
return res.status(HttpStatus.OK).json({ rooms: rooms });
res.status(HttpStatus.OK).json({ rooms: rooms });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('allrooms')
async getAllRooms(@Req() req, @Res() res): Promise<object>
async getAllRooms(@Req() req, @Res() res): Promise<void>
{
console.log("- in getAllRooms controller");
const rooms = await this.chatService.getAllNotMyRooms(req.user.username);
const users = await this.chatService.getAllUsersNotMyRooms(req.user.username);
return res.status(HttpStatus.OK).json({ rooms: rooms, users: users });
res.status(HttpStatus.OK).json({ rooms: rooms, users: users });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('current')
async setCurrentRoom(@Body() setCurrentRoomDto: setCurrentRoomDto, @Req() req, @Res() res): Promise<object>
async setCurrentRoom(@Body() setCurrentRoomDto: setCurrentRoomDto, @Req() req, @Res() res): Promise<void>
{
console.log("- in setCurrentRoom controller");
const response = await this.chatService.setCurrentRoom(req.user.username, setCurrentRoomDto.name);
return res.status(HttpStatus.OK).json({ message: response });
res.status(HttpStatus.OK).json({ message: response });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('allowedchars')
async allowedChars(@Res() res): Promise<object>
async allowedChars(@Res() res): Promise<void>
{
console.log("- in allowedChars controller");
return res.status(HttpStatus.OK).json({ chars: this.allowed_chars });
res.status(HttpStatus.OK).json({ chars: this.allowed_chars });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('create')
async createRoom(@Body() createRoomDto: createRoomDto, @Req() req, @Res() res): Promise<object>
async createRoom(@Body() createRoomDto: createRoomDto, @Req() req, @Res() res): Promise<void>
{
console.log("- in createRoom controller");
@@ -82,35 +82,35 @@ export class ChatController {
}
const response = await this.chatService.addUserToNewRoom(req.user.username, createRoomDto);
return res.status(HttpStatus.OK).json({ room_name: createRoomDto.room_name, message: response });
res.status(HttpStatus.OK).json({ room_name: createRoomDto.room_name, message: response });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('join')
async joinRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res): Promise<object>
async joinRoom(@Body() room: roomDto, @Req() req, @Res() res): Promise<void>
{
console.log("- in joinRoom controller");
const response = await this.chatService.addUserToRoom(req.user.username, joinRoomDto.room_name);
const response = await this.chatService.addUserToRoom(req.user.username, room.room_name);
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
await this.chatService.socketJoinRoom(socket, joinRoomDto.room_name);
await this.chatService.socketJoinRoom(socket, room.room_name);
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: response });
res.status(HttpStatus.OK).json({ room_name: room.room_name, message: response });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('change')
async changeRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res): Promise<object>
async changeRoom(@Body() room: roomDto, @Req() req, @Res() res): Promise<void>
{
console.log("- in changeRoom controller");
const response = await this.chatService.setCurrentRoom(req.user.username, joinRoomDto.room_name);
const response = await this.chatService.setCurrentRoom(req.user.username, room.room_name);
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
await this.chatService.socketChangeRoom(socket, joinRoomDto.room_name);
await this.chatService.socketChangeRoom(socket, room.room_name);
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: response });
res.status(HttpStatus.OK).json({ room_name: room.room_name, message: response });
}
@UseGuards(AuthenticateGuard)
@@ -125,34 +125,34 @@ export class ChatController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('messages')
async getMessages(@Req() req, @Res() res): Promise<object>
async getMessages(@Req() req, @Res() res): Promise<void>
{
console.log("- in getMessages controller");
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user.username);
return res.status(HttpStatus.OK).json({ messages: messages });
res.status(HttpStatus.OK).json({ messages: messages });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('roomusers')
async getRoomUsers(@Req() req, @Res() res): Promise<object>
async getRoomUsers(@Req() req, @Res() res): Promise<void>
{
console.log("- in getRoomUsers controller");
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
const room = await this.chatService.getRoomByName(room_name);
const users = room.users;
return res.status(HttpStatus.OK).json({ users: users });
res.status(HttpStatus.OK).json({ users: users });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Delete('removeuser')
async removeUser(@Req() req, @Res() res): Promise<object>
async removeUser(@Req() req, @Res() res): Promise<void>
{
console.log("- in removeUser controller");
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
let response = await this.chatService.removeUserFromRoom(req.user.username, room_name);
return res.status(HttpStatus.OK).json({ message: response });
res.status(HttpStatus.OK).json({ message: response });
}
}

View File

@@ -5,7 +5,7 @@ import { Chatroom } from './entities/chatroom.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { createRoomDto } from './dto/createRoom.dto';
import { joinRoomDto } from './dto/joinRoom.dto';
import { roomDto } from './dto/room.dto';
import { messagesDto } from './dto/messages.dto';
import { socketDto } from './dto/socket.dto';
@@ -137,7 +137,7 @@ export class ChatService {
console.log("-- in addUserToNewRoom service");
const room = await this.getRoomByName(createRoomDto.room_name);
if (room)
throw new HttpException(`This room already exist`, HttpStatus.CONFLICT);
throw new HttpException(`This room name already exist`, HttpStatus.CONFLICT);
// create chatroom
const newChatroom = new Chatroom();

View File

@@ -1,17 +1,9 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional, IsEnum } from "class-validator";
import { RoomType } from 'src/chat/enums/roomType.enum';
import { roomDto } from './room.dto';
export class createRoomDto
export class createRoomDto extends roomDto
{
@IsString()
@IsNotEmpty()
room_name: string;
@IsString()
@IsNotEmpty()
@IsEnum(RoomType)
room_type: RoomType;
@IsString()
@IsOptional()
room_password: string;

View File

@@ -1,10 +0,0 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
import { IsNull } from "typeorm";
export class joinRoomDto
{
@IsString()
@IsNotEmpty()
room_name: string;
}

View File

@@ -0,0 +1,18 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional, IsEnum } from "class-validator";
import { Expose } from 'class-transformer';
import { RoomType } from 'src/chat/enums/roomType.enum';
export class roomDto
{
@Expose()
@IsString()
@IsNotEmpty()
room_name: string;
@Expose()
@IsString()
@IsNotEmpty()
@IsEnum(RoomType)
room_type: RoomType;
}

View File

@@ -4,5 +4,6 @@ export enum RoomType {
protected = 'protected',
private = 'private',
direct = 'direct',
user = 'user',
}