Skip to content

Commit

Permalink
Feature(hynsoo/chat):채팅방 접속시 최근 대화내역 출력 기능 구현 / chats 요청 경로 수정 modern…
Browse files Browse the repository at this point in the history
  • Loading branch information
KimSoo committed Nov 7, 2022
1 parent 1f2b9d8 commit 78234b7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
15 changes: 13 additions & 2 deletions main-project/src/chats/chats-controller.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class ChatsControllerService {

async getChatLog({ userNo, chatRoomNo, currentChatLogNo }: PreviousChatLog) {
try {
// await this.checkRoom({userNo, chatRoomNo})
await this.checkChatRoom({ userNo, chatRoomNo });

const chatLog = await this.chatLogRepository.getPreviousChatLog(
Expand All @@ -52,6 +51,18 @@ export class ChatsControllerService {
}
}

async getRecentChatLog({ userNo, chatRoomNo }: PreviousChatLog) {
try {
await this.checkChatRoom({ userNo, chatRoomNo });

const chatLog = await this.chatLogRepository.getRecentChatLog(chatRoomNo);

return chatLog;
} catch (error) {
throw error;
}
}

private async checkChatRoom(chatUserInfo: ChatUserInfo): Promise<void> {
try {
const { chatRoomNo } = chatUserInfo;
Expand All @@ -75,7 +86,7 @@ export class ChatsControllerService {
chatUserInfo,
);
if (!chatUser) {
throw new BadRequestException(`채팅방에 없는 사용자 입니다.`);
throw new BadRequestException(`채팅방에 존재하지 않는 사용자 입니다.`);
}
} catch (error) {
throw error;
Expand Down
1 change: 1 addition & 0 deletions main-project/src/chats/chats-gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class ChatsGatewayService {

socket.join(`${user.chatRoomNo}`);

//추후 로그 또는 삭제
socket.broadcast.to(`${user.chatRoomNo}`).emit('join-room', {
username: user.nickname,
msg: `${user.nickname}님이 접속하셨습니다.`,
Expand Down
25 changes: 23 additions & 2 deletions main-project/src/chats/chats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ChatsControllerService } from './chats-controller.service';
export class ChatsController {
constructor(private readonly chatControllerService: ChatsControllerService) {}

@Get('/chatroom/:userNo')
@Get('/:userNo')
@ApiOperation({
summary: '채팅 목록 API',
description: ' 채팅 목록 조회',
Expand All @@ -25,7 +25,28 @@ export class ChatsController {
}
}

@Get('/chatRoom/:chatRoomNo/log')
@Get('/join/:chatRoomNo')
@ApiOperation({
summary: '채팅방 입장시 대화내역 API',
description: '채팅방 입장 시 가장 최신 대화내역 출력',
})
async getRecentChatLog(
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
@Body('userNo', ParseIntPipe) userNo: number,
): Promise<any> {
try {
const chatLog = await this.chatControllerService.getRecentChatLog({
userNo,
chatRoomNo,
});

return chatLog;
} catch (err) {
throw err;
}
}

@Get('/:chatRoomNo/log')
async getChatLog(
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
@Body('userNo', ParseIntPipe) userNo: number,
Expand Down
17 changes: 17 additions & 0 deletions main-project/src/chats/repository/chat-log.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,21 @@ export class ChatLogRepository extends Repository<ChatLog> {
);
}
}

async getRecentChatLog(chatRoomNo): Promise<ChatLog[]> {
try {
const recentChatLog = await this.createQueryBuilder('chat_log')
.select(['chat_log.*'])
.where('chat_log.chat_room_no = :chatRoomNo', { chatRoomNo })
.orderBy('no', 'DESC')
.limit(30)
.getRawMany();

return recentChatLog;
} catch (err) {
throw new InternalServerErrorException(
`${err}: 채팅로그 불러오기(getRecentChatLog): 알 수 없는 서버 에러입니다.`,
);
}
}
}

0 comments on commit 78234b7

Please sign in to comment.