home rooms are charging correctly
This commit is contained in:
@@ -3,6 +3,7 @@ import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
|
||||
import { ChatService } from './chat.service';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { joinRoomDto } from './dto/joinRoom.dto';
|
||||
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
|
||||
|
||||
@Controller('chat')
|
||||
export class ChatController {
|
||||
@@ -16,17 +17,26 @@ export class ChatController {
|
||||
@Get('rooms')
|
||||
async getRooms(@Req() req, @Res() res)
|
||||
{
|
||||
console.log("in getRooms");
|
||||
//console.log("in getRooms");
|
||||
return await this.chatService.getRooms(req.user, res);
|
||||
}
|
||||
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
@Get('current')
|
||||
async setCurrentRoom(@Body() setCurrentRoomDto: setCurrentRoomDto, @Req() req, @Res() res)
|
||||
{
|
||||
//console.log("in setCurrentRoom");
|
||||
return await this.chatService.setCurrentRoom(req.user, setCurrentRoomDto.name, res);
|
||||
}
|
||||
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
@Post('join')
|
||||
async joinRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res)
|
||||
{
|
||||
const user: User = req.user;
|
||||
return await this.chatService.addUserToRoom(user, joinRoomDto, res);
|
||||
//console.log("in joinRoom");
|
||||
return await this.chatService.addUserToRoom(req.user, joinRoomDto, res);
|
||||
}
|
||||
|
||||
@UseGuards(AuthenticateGuard)
|
||||
|
||||
@@ -17,22 +17,49 @@ export class ChatService {
|
||||
private readonly chatroomRepository: Repository<Chatroom>,
|
||||
) { }
|
||||
|
||||
//async add_message(server, message) {
|
||||
// return server.emit('message', message);
|
||||
//}
|
||||
|
||||
// temp for test
|
||||
sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async getRooms(user: User, @Res() res)
|
||||
{
|
||||
const rooms = await this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
.where(':user_id IN (chatroom.users)', { user_id: user.fortyTwoId })
|
||||
.getMany();
|
||||
|
||||
/*
|
||||
return res.status(HttpStatus.OK).json({ rooms: rooms });
|
||||
}
|
||||
|
||||
async findRoomByName(name: string, @Res() res)
|
||||
{
|
||||
const room = await this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
.where(':user_id IN chatroom.users', { user_id: user.fortyTwoId })
|
||||
.getMany();
|
||||
console.log(room);
|
||||
*/
|
||||
.where('chatroom.name = :name', { name: name })
|
||||
.getOne();
|
||||
|
||||
//return chatrooms;
|
||||
return res.status(HttpStatus.OK).json({ room: room });
|
||||
}
|
||||
|
||||
async findRoomById(id: number, @Res() res)
|
||||
{
|
||||
const room = await this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
.where('chatroom.id = :id', { id: id })
|
||||
.getOne();
|
||||
|
||||
return res.status(HttpStatus.OK).json({ room: room });
|
||||
}
|
||||
|
||||
async setCurrentRoom(user: User, name: string, @Res() res)
|
||||
{
|
||||
const user_db = await this.usersService.findOneByFourtyTwoId(user.fortyTwoId);
|
||||
user_db.currentRoom = name;
|
||||
this.userRepository.save(user_db);
|
||||
|
||||
return res.status(HttpStatus.OK).json({ message: `room "${name}" is now current room` });
|
||||
}
|
||||
|
||||
async addUserToRoom(user: User, joinRoomDto: joinRoomDto, @Res() res)
|
||||
@@ -41,7 +68,6 @@ export class ChatService {
|
||||
.createQueryBuilder('chatroom')
|
||||
.where('chatroom.name = :name', { name: joinRoomDto.room_name })
|
||||
.getOne();
|
||||
console.log(room);
|
||||
if (room)
|
||||
throw new HttpException(`This room already exist`, HttpStatus.CONFLICT);
|
||||
|
||||
@@ -53,6 +79,8 @@ export class ChatService {
|
||||
newChatroom.users = [user.fortyTwoId];
|
||||
this.chatroomRepository.save(newChatroom);
|
||||
|
||||
this.setCurrentRoom(user, joinRoomDto.room_name, res)
|
||||
|
||||
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: "successfull room creation" });
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
|
||||
import { IsNull } from "typeorm";
|
||||
|
||||
export class joinRoomDto {
|
||||
export class joinRoomDto
|
||||
{
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
room_name: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
room_name : string
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
room_type : 'public' | 'private' | 'direct';
|
||||
room_type: 'public' | 'private' | 'direct';
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
room_password : string
|
||||
room_password: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
|
||||
import { IsNull } from "typeorm";
|
||||
|
||||
export class setCurrentRoomDto
|
||||
{
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
}
|
||||
|
||||
@@ -54,10 +54,15 @@ export class User {
|
||||
@OneToOne(() => UserStats, { cascade: true })
|
||||
stats: UserStats;
|
||||
|
||||
// @OneToMany(type => Chatroom, chatroom => chatroom.owner)
|
||||
// ownedRooms: Chatroom[];
|
||||
//
|
||||
// @ManyToMany(type => Chatroom)
|
||||
// @JoinTable()
|
||||
// userRooms: Chatroom[];
|
||||
// ROOMS :
|
||||
|
||||
//@OneToMany(type => Chatroom, chatroom => chatroom.owner)
|
||||
//ownedRooms: Chatroom[];
|
||||
|
||||
//@ManyToMany(type => Chatroom)
|
||||
//@JoinTable()
|
||||
//userRooms: Chatroom[];
|
||||
|
||||
@Column({ nullable: true })
|
||||
currentRoom: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user