wip db request with array not working

This commit is contained in:
simplonco
2023-01-08 20:21:19 +01:00
parent f4dc5cde53
commit ebbd5ff530
8 changed files with 214 additions and 120 deletions

View File

@@ -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 { createRoomDto } from './dto/createRoom.dto';
import { joinRoomDto } from './dto/joinRoom.dto';
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
@Controller('chat')
@@ -48,16 +49,18 @@ export class ChatController {
async createRoom(@Body() createRoomDto: createRoomDto, @Req() req, @Res() res): Promise<object>
{
console.log("- in createRoom controller");
const response = await this.chatService.addUserToRoom(req.user, createRoomDto);
const response = await this.chatService.addUserToNewRoom(req.user, createRoomDto);
return res.status(HttpStatus.OK).json({ room_name: createRoomDto.room_name, message: response });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('join')
async joinRoom(@Body() body)
async joinRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res): Promise<object>
{
console.log("- in joinRoom controller");
const response = await this.chatService.addUserToRoom(req.user, joinRoomDto);
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: response });
}
@UseGuards(AuthenticateGuard)

View File

@@ -5,6 +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 { messagesDto } from './dto/messages.dto';
@Injectable()
@@ -29,7 +30,17 @@ export class ChatService {
console.log("-- in getMyRooms service");
const rooms = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where(':user_id IN (chatroom.users)', { user_id: user.fortyTwoId })
.where(':user_name ANY(chatroom.users)', { user_name: user.username })
.getMany();
return rooms;
}
async getAllRooms()
{
console.log("-- in getAllRooms service");
const rooms = await this.chatroomRepository
.createQueryBuilder('chatroom')
.getMany();
return rooms;
@@ -38,11 +49,11 @@ export class ChatService {
async getAllNotMyRooms(user: User)
{
console.log("-- in getAllNotMyRooms service");
const user_db = await this.usersService.findOneByFourtyTwoId(user.fortyTwoId);
const user_db = await this.findUserByName(user.username);
const rooms = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.type != :type', { type: 'private' })
.andWhere(':user_id NOT IN (chatroom.users)', { user_id: user.fortyTwoId })
.andWhere(':user_name NOT IN (chatroom.users)', { user_name: user.username })
.getMany();
//const users = await this.findAllUsers();
//let allRooms = [...rooms, ...users];
@@ -102,20 +113,17 @@ export class ChatService {
async setCurrentRoom(user: User, name: string)
{
console.log("-- in setCurrentRoom service");
const user_db = await this.usersService.findOneByFourtyTwoId(user.fortyTwoId);
const user_db = await this.findUserByName(user.username);
user_db.currentRoom = name;
this.userRepository.save(user_db);
return `room "${name}" is now current room`;
}
async addUserToRoom(user: User, createRoomDto: createRoomDto)
async addUserToNewRoom(user: User, createRoomDto: createRoomDto)
{
console.log("-- in addUserToRoom service");
const room = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.name = :name', { name: createRoomDto.room_name })
.getOne();
const room = await this.findRoomByName(createRoomDto.room_name);
if (room)
throw new HttpException(`This room already exist`, HttpStatus.CONFLICT);
@@ -123,8 +131,8 @@ export class ChatService {
const newChatroom = new Chatroom();
newChatroom.name = createRoomDto.room_name;
newChatroom.type = createRoomDto.room_type;
newChatroom.owner = user.fortyTwoId;
newChatroom.users = [user.fortyTwoId];
newChatroom.owner = user.username;
newChatroom.users = [user.username];
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${createRoomDto.room_name}` }];
this.chatroomRepository.save(newChatroom);
@@ -133,6 +141,27 @@ export class ChatService {
return "successfull room creation";
}
async addUserToRoom(user: User, joinRoomDto: joinRoomDto)
{
console.log("-- in addUserToRoom service");
const room = await this.findRoomByName(joinRoomDto.room_name);
if (room.users.includes(user.username))
throw new HttpException(`your have already join this room`, HttpStatus.CONFLICT);
// update room with new user
room.users.push(user.username);
this.chatroomRepository.save(room);
const rooms = await this.getMyRooms(user);
console.log("rooms:", rooms);
const allRooms = await this.getAllRooms();
console.log("allRooms:", allRooms);
this.setCurrentRoom(user, joinRoomDto.room_name)
return "successfull joining room";
}
async removeUserFromRoom(user: User, room_name: string)
{
console.log("-- in removeUserFromRoom service");

View File

@@ -0,0 +1,10 @@
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

@@ -27,10 +27,10 @@ export class Chatroom {
// users: User[];
@Column()
owner: string; // fortytwo id
owner: string; // name
@Column("simple-array")
users: string[]; // fortytwo id
users: string[]; // names
@Column("json")
messages: { name: string, message: string }[];