From e9b228c2bc284a259cd40c55b9ff37a2fa9f477f 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: Tue, 9 Jan 2024 22:26:43 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Feat=20:=20=EB=A7=88=EC=A7=80=EB=A7=89=20?= =?UTF-8?q?=EC=B1=84=ED=8C=85=20chatRoom=20=EC=97=90=20=EA=B0=99=EC=9D=B4?= =?UTF-8?q?=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.service.ts | 11 ++++++++++- BE/src/entities/chatRoom.entity.ts | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/BE/src/chat/chat.service.ts b/BE/src/chat/chat.service.ts index a0ffa6f..0ba7284 100644 --- a/BE/src/chat/chat.service.ts +++ b/BE/src/chat/chat.service.ts @@ -40,7 +40,16 @@ export class ChatService { chat.chat_room = message.room_id; chat.is_read = is_read; chat.count = message.count; - await this.chatRepository.save(chat); + const lastChat = await this.chatRepository.save(chat); + + await this.chatRoomRepository.update( + { + id: message.room_id, + }, + { + last_chat_id: lastChat.id, + }, + ); } async createRoom( diff --git a/BE/src/entities/chatRoom.entity.ts b/BE/src/entities/chatRoom.entity.ts index 4a37ffe..f9bc493 100644 --- a/BE/src/entities/chatRoom.entity.ts +++ b/BE/src/entities/chatRoom.entity.ts @@ -8,6 +8,7 @@ import { ManyToOne, OneToMany, PrimaryGeneratedColumn, + OneToOne, } from 'typeorm'; import { PostEntity } from './post.entity'; import { ChatEntity } from './chat.entity'; @@ -42,6 +43,9 @@ export class ChatRoomEntity { @DeleteDateColumn() delete_date: Date; + @Column() + last_chat_id: number; + @Column({ default: false }) writer_left: boolean; @@ -51,6 +55,10 @@ export class ChatRoomEntity { @OneToMany(() => ChatEntity, (chat) => chat.chatRoom) chats: ChatEntity[]; + @OneToOne(() => ChatEntity, (chat) => chat.id) + @JoinColumn({ name: 'last_chat_id' }) + lastChat: ChatEntity; + @ManyToOne(() => PostEntity, (post) => post.id) @JoinColumn({ name: 'post_id' }) post: PostEntity; From cbdf45452d96dd16594179cf74af1db326de940d 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: Tue, 9 Jan 2024 22:50:57 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Feat=20:=20=EB=A7=88=EC=A7=80=EB=A7=89=20?= =?UTF-8?q?=EC=B1=84=ED=8C=85=20join=20=EC=9C=BC=EB=A1=9C=20=EC=B1=84?= =?UTF-8?q?=ED=8C=85=EB=B0=A9=EB=AA=A9=EB=A1=9D=20api=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/chat/chat.service.ts | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/BE/src/chat/chat.service.ts b/BE/src/chat/chat.service.ts index 0ba7284..67e4803 100644 --- a/BE/src/chat/chat.service.ts +++ b/BE/src/chat/chat.service.ts @@ -88,24 +88,12 @@ export class ChatService { async findRoomList(userId: string) { const chatListInfo = { all_read: true, chat_list: [] }; - const subquery = this.chatRepository - .createQueryBuilder('chat') - .select('chat.id', 'id') - .addSelect('chat.chat_room', 'chat_room') - .addSelect('chat.message', 'message') - .addSelect('chat.create_date', 'create_date') - .addSelect('chat.is_read', 'is_read') - .addSelect('chat.sender', 'sender') - .where( - 'chat.id IN (SELECT MAX(chat.id) FROM chat GROUP BY chat.chat_room)', - ); - const rooms = await this.chatRoomRepository .createQueryBuilder('chat_room') .innerJoin( - '(' + subquery.getQuery() + ')', + 'chat_room.lastChat', 'chat_info', - 'chat_room.id = chat_info.chat_room', + 'chat_room.lastChat = chat_info.id', ) .innerJoin( 'chat_room.writerUser', @@ -171,24 +159,12 @@ export class ChatService { } async unreadChat(userId: string) { - const subquery = this.chatRepository - .createQueryBuilder('chat') - .select('chat.id', 'id') - .addSelect('chat.chat_room', 'chat_room') - .addSelect('chat.message', 'message') - .addSelect('chat.create_date', 'create_date') - .addSelect('chat.is_read', 'is_read') - .addSelect('chat.sender', 'sender') - .where( - 'chat.id IN (SELECT MAX(chat.id) FROM chat GROUP BY chat.chat_room)', - ); - const rooms = await this.chatRoomRepository .createQueryBuilder('chat_room') .innerJoin( - '(' + subquery.getQuery() + ')', + 'chat_room.lastChat', 'chat_info', - 'chat_room.id = chat_info.chat_room', + 'chat_room.lastChat = chat_info.id', ) .innerJoin( 'chat_room.writerUser',