From 4aed13d1c7fbf105133b54ec78408ad735dd4c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=92?= =?UTF-8?q?=E1=85=AE=E1=86=AB?= Date: Thu, 30 Nov 2023 16:08:56 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20Feat=20:=20=EC=B1=84=ED=8C=85=20DB=20?= =?UTF-8?q?=EC=97=90=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/chat/chat.controller.ts | 2 +- BE/src/chat/chat.module.ts | 3 ++- BE/src/chat/chat.service.ts | 36 ++++++++++++++++++---------------- BE/src/chat/chats.gateway.ts | 13 +++++++----- BE/src/chat/dto/chat.dto.ts | 12 ++++++++++++ 5 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 BE/src/chat/dto/chat.dto.ts diff --git a/BE/src/chat/chat.controller.ts b/BE/src/chat/chat.controller.ts index b3dafb2..99676ff 100644 --- a/BE/src/chat/chat.controller.ts +++ b/BE/src/chat/chat.controller.ts @@ -22,6 +22,6 @@ export class ChatController { @UseGuards(AuthGuard) async roomCreate(@Body() body: CreateRoomDto, @UserHash() userId: string) { console.log(userId); - await this.chatService.createOrFindRoom(body.post_id, userId, body.writer); + await this.chatService.createRoom(body.post_id, userId, body.writer); } } diff --git a/BE/src/chat/chat.module.ts b/BE/src/chat/chat.module.ts index 3f8d889..ee183aa 100644 --- a/BE/src/chat/chat.module.ts +++ b/BE/src/chat/chat.module.ts @@ -5,9 +5,10 @@ import { ChatsGateway } from './chats.gateway'; import { TypeOrmModule } from '@nestjs/typeorm'; import { ChatRoomEntity } from '../entities/chatRoom.entity'; import { PostEntity } from '../entities/post.entity'; +import { ChatEntity } from 'src/entities/chat.entity'; @Module({ - imports: [TypeOrmModule.forFeature([ChatRoomEntity, PostEntity])], + imports: [TypeOrmModule.forFeature([ChatRoomEntity, PostEntity, ChatEntity])], controllers: [ChatController], providers: [ChatService, ChatsGateway], }) diff --git a/BE/src/chat/chat.service.ts b/BE/src/chat/chat.service.ts index 5b022a3..e1b97e6 100644 --- a/BE/src/chat/chat.service.ts +++ b/BE/src/chat/chat.service.ts @@ -1,8 +1,10 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { PostEntity } from '../entities/post.entity'; -import { Repository } from 'typeorm'; +import { In, Repository } from 'typeorm'; import { ChatRoomEntity } from '../entities/chatRoom.entity'; +import { ChatEntity } from 'src/entities/chat.entity'; +import { ChatDto } from './dto/chat.dto'; @Injectable() export class ChatService { @@ -11,24 +13,24 @@ export class ChatService { private chatRoomRepository: Repository, @InjectRepository(PostEntity) private postRepository: Repository, + @InjectRepository(ChatEntity) + private chatRepository: Repository, ) {} - async createOrFindRoom(postId: number, userId: string, writerId: string) { - const roomNumber = await this.chatRoomRepository.findOne({ - where: { writer: writerId, user: userId, post_id: postId }, - }); // 해당 게시글과 사람들에 대한 채팅방이 있는지 확인한다 - - if (roomNumber) { - return roomNumber; // 있으면 채팅방 번호 리턴 - } else { - const chatRoom = new ChatRoomEntity(); - chatRoom.post_id = postId; - chatRoom.writer = writerId; - chatRoom.user = userId; - const newChatRoom = await this.chatRoomRepository.save(chatRoom); // 없으면 새로 만들어서 저장후 리턴 - return newChatRoom; - } + async saveMessage(message: ChatDto) { + const chat = new ChatEntity(); + chat.sender = message.sender; + chat.message = message.message; + chat.chat_room = message.room_id; + await this.chatRepository.save(chat); + } - //이후에 채팅과 Join 해서 채팅 목록도 가져와야함 + async createRoom(postId: number, userId: string, writerId: string) { + const chatRoom = new ChatRoomEntity(); + chatRoom.post_id = postId; + chatRoom.writer = writerId; + chatRoom.user = userId; + const newChatRoom = await this.chatRoomRepository.save(chatRoom); // 없으면 새로 만들어서 저장후 리턴 + return newChatRoom; } } diff --git a/BE/src/chat/chats.gateway.ts b/BE/src/chat/chats.gateway.ts index 4c61da9..1d6b333 100644 --- a/BE/src/chat/chats.gateway.ts +++ b/BE/src/chat/chats.gateway.ts @@ -8,13 +8,14 @@ import { } from '@nestjs/websockets'; import { Server, Websocket } from 'ws'; import { ChatService } from './chat.service'; +import { ChatDto } from './dto/chat.dto'; @WebSocketGateway({ path: 'chats', }) export class ChatsGateway implements OnGatewayConnection { @WebSocketServer() server: Server; - private rooms = new Map>(); + private rooms = new Map>(); constructor(private readonly chatService: ChatService) {} handleConnection(client: Websocket) { @@ -27,17 +28,19 @@ export class ChatsGateway implements OnGatewayConnection { } @SubscribeMessage('send-message') - sendMessage( - @MessageBody() message: object, + async sendMessage( + @MessageBody() message: ChatDto, @ConnectedSocket() client: Websocket, ) { console.log(message); - const room = this.rooms.get(message['room']); + const room = this.rooms.get(message['room_id']); const sender = message['sender']; + console.log(room); room.forEach((people) => { if (people !== client) people.send(JSON.stringify({ sender, message: message['message'] })); }); + await this.chatService.saveMessage(message); } @SubscribeMessage('join-room') @@ -45,7 +48,7 @@ export class ChatsGateway implements OnGatewayConnection { @MessageBody() message: object, @ConnectedSocket() client: Websocket, ) { - const roomName = message['room']; + const roomName = message['room_id']; if (this.rooms.has(roomName)) this.rooms.get(roomName).add(client); else this.rooms.set(roomName, new Set([client])); } diff --git a/BE/src/chat/dto/chat.dto.ts b/BE/src/chat/dto/chat.dto.ts new file mode 100644 index 0000000..b0c282b --- /dev/null +++ b/BE/src/chat/dto/chat.dto.ts @@ -0,0 +1,12 @@ +import { IsString } from 'class-validator'; + +export class ChatDto { + @IsString() + sender: string; + + @IsString() + message: string; + + @IsString() + room_id: number; +}