Skip to content

Commit

Permalink
Feature(hyunsoo/chat): 채팅로그 불러오는 기능 구현, 채팅방에 유저가 있는 지 확인하는 기능 구현 mode…
Browse files Browse the repository at this point in the history
  • Loading branch information
KimSoo committed Nov 4, 2022
1 parent b675b7d commit 55619c3
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
38 changes: 37 additions & 1 deletion main-project/src/chats/chats-controller.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ChatRoomList } from './interface/chat.interface';
import {
ChatRoomList,
ChatUserInfo,
PreviousChatLog,
} from './interface/chat.interface';
import { ChatLogRepository } from './repository/chat-log.repository';
import { ChatUsersRepository } from './repository/chat-users.repository';

@Injectable()
export class ChatsControllerService {
constructor(
@InjectRepository(ChatUsersRepository)
private readonly chatUsersRepository: ChatUsersRepository,

@InjectRepository(ChatLogRepository)
private readonly chatLogRepository: ChatLogRepository,
) {}

async getChatRoomListByUserNo(userNo): Promise<ChatRoomList[]> {
Expand All @@ -23,4 +31,32 @@ export class ChatsControllerService {
throw err;
}
}

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

const chatLog = await this.chatLogRepository.getPreviousChatLog(
chatRoomNo,
currentChatLogNo,
);
console.log(chatLog);
} catch (err) {
throw err;
}
}

private async checkUserInChatRoom(chatUserInfo: ChatUserInfo) {
try {
const result = await this.chatUsersRepository.checkUserInChatRoom(
chatUserInfo,
);
if (!result) {
throw new BadRequestException(`채팅방에 없는 사용자 입니다.`);
}
} catch (err) {
throw err;
}
}
}
19 changes: 18 additions & 1 deletion main-project/src/chats/chats.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Param } from '@nestjs/common';
import { Body, Controller, Get, Param, ParseIntPipe } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { ChatsControllerService } from './chats-controller.service';

Expand All @@ -24,4 +24,21 @@ export class ChatsController {
throw err;
}
}

@Get('/chatRoom/:chatRoomNo/log')
async getChatLog(
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
@Body('userNo', ParseIntPipe) userNo: number,
@Body('currentChatLogNo', ParseIntPipe) currentChatLogNo: number,
): Promise<any> {
try {
await this.chatControllerService.getChatLog({
userNo,
chatRoomNo,
currentChatLogNo,
});
} catch (err) {
throw err;
}
}
}
13 changes: 13 additions & 0 deletions main-project/src/chats/interface/chat.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ export interface ChatRoomList {
roomName: string;
chatRoomNo: number;
}

export interface PreviousChatLog {
userNo: number;
currentChatLogNo?: number;
chatRoomNo: number;
message?: string;
timeStamp?: Date;
}

export interface ChatUserInfo {
userNo: number;
chatRoomNo: number;
}
1 change: 1 addition & 0 deletions main-project/src/chats/repository/chat-list.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class ChatListRepository extends Repository<ChatList> {
.where(`chat_list.no = :chatRoomNo`, { chatRoomNo })
.andWhere('chatUserNo.user_no = :userNo', { userNo })
.getRawOne();

return result;
} catch (err) {
throw new InternalServerErrorException(
Expand Down
18 changes: 18 additions & 0 deletions main-project/src/chats/repository/chat-log.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,22 @@ export class ChatLogRepository extends Repository<ChatLog> {
);
}
}

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

return previousChatLog;
} catch (err) {
throw new InternalServerErrorException(
`${err}: 채팅로그 불러오기(getPreviousChatLog): 알 수 없는 서버 에러입니다.`,
);
}
}
}
20 changes: 19 additions & 1 deletion main-project/src/chats/repository/chat-users.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InternalServerErrorException } from '@nestjs/common';
import { EntityRepository, InsertResult, Repository } from 'typeorm';
import { ChatUsers } from '../entity/chat-users.entity';
import { ChatRoomList } from '../interface/chat.interface';
import { ChatRoomList, ChatUserInfo } from '../interface/chat.interface';

@EntityRepository(ChatUsers)
export class ChatUsersRepository extends Repository<ChatUsers> {
Expand Down Expand Up @@ -38,4 +38,22 @@ export class ChatUsersRepository extends Repository<ChatUsers> {
);
}
}

async checkUserInChatRoom(chatUserInfo: ChatUserInfo): Promise<object> {
try {
const user = await this.createQueryBuilder('chat_users')
.select([
'chat_users.user_no AS userNo',
'chat_users.chat_room_no AS chatRoomNo',
])
.where('user_no = :userNo AND chat_room_no = :chatRoomNo', chatUserInfo)
.getRawOne();

return user;
} catch (err) {
throw new InternalServerErrorException(
`${err}: 유저 정보 조회(checkUserInChatRoom): 알 수 없는 서버 에러입니다`,
);
}
}
}

0 comments on commit 55619c3

Please sign in to comment.