Skip to content

Commit

Permalink
Merge pull request #602 from boostcampwm2023/BE-saveLastChat-#001
Browse files Browse the repository at this point in the history
[BE/#576] 마지막 채팅 DB 에 함께 저장
  • Loading branch information
koomin1227 authored Jan 10, 2024
2 parents 300dff7 + cbdf454 commit 3dbb785
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
43 changes: 14 additions & 29 deletions BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -79,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',
Expand Down Expand Up @@ -162,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',
Expand Down
8 changes: 8 additions & 0 deletions BE/src/entities/chatRoom.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
OneToOne,
} from 'typeorm';
import { PostEntity } from './post.entity';
import { ChatEntity } from './chat.entity';
Expand Down Expand Up @@ -42,6 +43,9 @@ export class ChatRoomEntity {
@DeleteDateColumn()
delete_date: Date;

@Column()
last_chat_id: number;

@Column({ default: false })
writer_left: boolean;

Expand All @@ -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;
Expand Down

0 comments on commit 3dbb785

Please sign in to comment.