Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#184 - 주식을 한글로 검색할 수 있다. #191

Merged
merged 25 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fe5686f
✨ feat: 주식 소유여부 확인
xjfcnfw3 Nov 13, 2024
032c1cf
✨ feat: request에서 user를 가져오는 커스텀 데코레이터 구현
xjfcnfw3 Nov 13, 2024
e4935a3
✨ feat: http 세션 전용 가드 구현
xjfcnfw3 Nov 13, 2024
3d1f90a
✨ feat: 쿠키 이름 설정
xjfcnfw3 Nov 13, 2024
e019686
✨ feat: 유저 소유 주식 엔드포인트 가드 적용
xjfcnfw3 Nov 13, 2024
aa78d8b
✨ feat: getUser 데코레이터를 인증된 유저 객체만 가져올 수 있도록 변경
xjfcnfw3 Nov 13, 2024
843f686
✨ feat: 유저 주식 소유 여부 엔드포인트 구현
xjfcnfw3 Nov 13, 2024
0bf13f0
Merge remote-tracking branch 'origin/dev-be' into feature/#158
xjfcnfw3 Nov 16, 2024
d2567ce
Merge remote-tracking branch 'origin/dev-be' into feature/#158
xjfcnfw3 Nov 18, 2024
24b4229
🐛 fix: 비어있는 쿠키를 받을 때 발생하는 예외 수정
xjfcnfw3 Nov 13, 2024
7016782
✨ feat: 특정 종목방의 최신 메시지 리스트 조회 기능 구현
xjfcnfw3 Nov 13, 2024
368401c
✨ feat: 종목방에 입장 시 이전 채팅 메시지를 받는다
xjfcnfw3 Nov 13, 2024
8b33b3f
♻️ refactor: swagger url 변경
xjfcnfw3 Nov 14, 2024
6f19e72
✨ feat: 이전 채팅 스크롤 조회 기능 구현
xjfcnfw3 Nov 16, 2024
8c2796e
✨ feat: class dto 내부 타입 자동 변경 설정
xjfcnfw3 Nov 16, 2024
f6369d8
✨ feat: 스크롤 크기를 100을 넘지 않도록 설정
xjfcnfw3 Nov 17, 2024
e2e3938
✅ test: 100개 초과 메시지 조회에 대한 테스트 코드 작성
xjfcnfw3 Nov 17, 2024
d6afa45
Merge branch 'dev-be' of https://github.com/boostcampwm-2024/web17-ju…
xjfcnfw3 Nov 18, 2024
42bd4ad
♻️ refactor: swagger url 변경
xjfcnfw3 Nov 14, 2024
00a0c06
✨ feat: 이전 채팅 스크롤 조회 기능 구현
xjfcnfw3 Nov 16, 2024
d5040cc
✨ feat: class dto 내부 타입 자동 변경 설정
xjfcnfw3 Nov 16, 2024
4f6c09f
Merge branch 'feature/#99' into feature/#93
xjfcnfw3 Nov 18, 2024
b003c02
✨ feat: 주식 검색 기능 구현
xjfcnfw3 Nov 18, 2024
95af1ae
✨ feat: 주식 검색 엔드포인트 구현
xjfcnfw3 Nov 18, 2024
35a2afc
✨ feat: swagger 기본 경로 변경
xjfcnfw3 Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/backend/src/auth/session/cookieParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const websocketCookieParse = (socket: Socket) => {
};

const getSessionIdFromCookie = (cookieValue: string) => {
if (cookieValue.startsWith('s:')) {
if (cookieValue?.startsWith('s:')) {
const [id, signature] = cookieValue.slice(2).split('.');
const expectedSignature = crypto
.createHmac('sha256', sessionConfig.secret)
Expand Down
39 changes: 39 additions & 0 deletions packages/backend/src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Controller, Get, Query } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiOkResponse,
ApiOperation,
} from '@nestjs/swagger';
import { ChatService } from '@/chat/chat.service';
import { ChatScrollRequest } from '@/chat/dto/chat.request';
import { ChatScrollResponse } from '@/chat/dto/chat.response';

@Controller('chat')
export class ChatController {
constructor(private readonly chatService: ChatService) {}

@ApiOperation({
summary: '채팅 스크롤 조회 API',
description: '채팅을 스크롤하여 조회한다.',
})
@ApiOkResponse({
description: '스크롤 조회 성공',
type: ChatScrollResponse,
})
@ApiBadRequestResponse({
description: '스크롤 크기 100 초과',
example: {
message: 'pageSize should be less than 100',
error: 'Bad Request',
statusCode: 400,
},
})
@Get()
async findChatList(@Query() request: ChatScrollRequest) {
return await this.chatService.scrollNextChat(
request.stockId,
request.latestChatId,
request.pageSize,
);
}
}
21 changes: 14 additions & 7 deletions packages/backend/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
SessionSocket,
WebSocketSessionGuard,
} from '@/auth/session/webSocketSession.guard.';
import { WebSocketExceptionFilter } from '@/middlewares/filter/webSocketException.filter';
import { StockService } from '@/stock/stock.service';
import { ChatService } from '@/chat/chat.service';
import { Chat } from '@/chat/domain/chat.entity';
import { WebSocketExceptionFilter } from '@/middlewares/filter/webSocketException.filter';
import { StockService } from '@/stock/stock.service';

interface chatMessage {
room: string;
Expand Down Expand Up @@ -67,16 +67,23 @@ export class ChatGateway implements OnGatewayConnection {

async handleConnection(client: Socket) {
const room = client.handshake.query.stockId;
if (!room || !(await this.stockService.checkStockExist(room as string))) {
if (
!this.isString(room) ||
!(await this.stockService.checkStockExist(room))
) {
client.emit('error', 'Invalid stockId');
this.logger.warn(`client connected with invalid stockId: ${room}`);
client.disconnect();
return;
}
if (room) {
client.join(room);
this.logger.info(`client joined room ${room}`);
}
client.join(room);
const messages = await this.chatService.scrollFirstChat(room);
this.logger.info(`client joined room ${room}`);
client.emit('chat', messages);
}

private isString(value: string | string[] | undefined): value is string {
return typeof value === 'string';
}

private toResponse(chat: Chat): chatResponse {
Expand Down
4 changes: 3 additions & 1 deletion packages/backend/src/chat/chat.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SessionModule } from '@/auth/session.module';
import { ChatController } from '@/chat/chat.controller';
import { ChatGateway } from '@/chat/chat.gateway';
import { ChatService } from '@/chat/chat.service';
import { Chat } from '@/chat/domain/chat.entity';
import { StockModule } from '@/stock/stock.module';
import { ChatService } from '@/chat/chat.service';

@Module({
imports: [TypeOrmModule.forFeature([Chat]), StockModule, SessionModule],
controllers: [ChatController],
providers: [ChatGateway, ChatService],
})
export class ChatModule {}
23 changes: 23 additions & 0 deletions packages/backend/src/chat/chat.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DataSource } from 'typeorm';
import { ChatService } from '@/chat/chat.service';
import { createDataSourceMock } from '@/user/user.service.spec';

describe('ChatService 테스트', () => {
test('첫 스크롤을 조회시 100개 이상 조회하면 예외가 발생한다.', async () => {
const dataSource = createDataSourceMock({});
const chatService = new ChatService(dataSource as DataSource);

await expect(() =>
chatService.scrollNextChat('A005930', 1, 101),
).rejects.toThrow('pageSize should be less than 100');
});

test('100개 이상의 채팅을 조회하려 하면 예외가 발생한다.', async () => {
const dataSource = createDataSourceMock({});
const chatService = new ChatService(dataSource as DataSource);

await expect(() =>
chatService.scrollFirstChat('A005930', 101),
).rejects.toThrow('pageSize should be less than 100');
});
});
83 changes: 80 additions & 3 deletions packages/backend/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { Chat } from '@/chat/domain/chat.entity';
import { ChatScrollResponse } from '@/chat/dto/chat.response';

interface ChatMessage {
export interface ChatMessage {
message: string;
stockId: string;
}
Comment on lines +6 to 9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만약 외부에서도 사용한다면 다른 파일로 분리하는 게 좋을 거 같아요


const DEFAULT_PAGE_SIZE = 20;

@Injectable()
export class ChatService {
constructor(private readonly dataSource: DataSource) {}
Expand All @@ -18,4 +21,78 @@ export class ChatService {
message: chatMessage.message,
});
}
}

async scrollFirstChat(stockId: string, scrollSize?: number) {
this.validatePageSize(scrollSize);
const result = await this.findFirstChatScroll(stockId, scrollSize);
return await this.toScrollResponse(result, scrollSize);
}

async scrollNextChat(
stockId: string,
latestChatId?: number,
pageSize?: number,
) {
this.validatePageSize(pageSize);
const result = await this.findChatScroll(stockId, latestChatId, pageSize);
return await this.toScrollResponse(result, pageSize);
}

private validatePageSize(scrollSize?: number) {
if (scrollSize && scrollSize > 100) {
throw new BadRequestException('pageSize should be less than 100');
}
}

private async toScrollResponse(result: Chat[], pageSize: number | undefined) {
const hasMore =
!!result && result.length > (pageSize ? pageSize : DEFAULT_PAGE_SIZE);
if (hasMore) {
result.pop();
}
return new ChatScrollResponse(result, hasMore);
}

private async findChatScroll(
stockId: string,
latestChatId?: number,
pageSize?: number,
) {
if (!latestChatId) {
return await this.findFirstChatScroll(stockId, pageSize);
} else {
return await this.findNextChatScroll(stockId, latestChatId, pageSize);
}
}

private async findFirstChatScroll(stockId: string, pageSize?: number) {
const queryBuilder = this.dataSource.createQueryBuilder(Chat, 'chat');
if (!pageSize) {
pageSize = DEFAULT_PAGE_SIZE;
}
return queryBuilder
.where('chat.stock_id = :stockId', { stockId })
.orderBy('chat.id', 'DESC')
.limit(pageSize + 1)
.getMany();
}

private async findNextChatScroll(
stockId: string,
latestChatId: number,
pageSize?: number,
) {
const queryBuilder = this.dataSource.createQueryBuilder(Chat, 'chat');
if (!pageSize) {
pageSize = DEFAULT_PAGE_SIZE;
}
return queryBuilder
.where('chat.stock_id = :stockId and chat.id < :latestChatId', {
stockId,
latestChatId,
})
.orderBy('chat.id', 'DESC')
.limit(pageSize + 1)
.getMany();
}
}
12 changes: 10 additions & 2 deletions packages/backend/src/chat/domain/chat.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import {
Column,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { ChatType } from '@/chat/domain/chatType.enum';
import { DateEmbedded } from '@/common/dateEmbedded.entity';
import { Stock } from '@/stock/domain/stock.entity';
Expand All @@ -10,9 +16,11 @@ export class Chat {
id: number;

@ManyToOne(() => User, (user) => user.id)
@JoinColumn({ name: 'user_id' })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 이거 저도 추가해야하는 데.. 깜박했네요

user: User;

@ManyToOne(() => Stock, (stock) => stock.id)
@JoinColumn({ name: 'stock_id' })
stock: Stock;

@Column()
Expand All @@ -25,5 +33,5 @@ export class Chat {
likeCount: number = 0;

@Column(() => DateEmbedded, { prefix: '' })
date?: DateEmbedded;
date: DateEmbedded;
}
30 changes: 30 additions & 0 deletions packages/backend/src/chat/dto/chat.request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNumber, IsOptional, IsString } from 'class-validator';

export class ChatScrollRequest {
@ApiProperty({
description: '종목 주식 id(종목방 id)',
example: 'A005930',
})
@IsString()
readonly stockId: string;

@ApiProperty({
description: '최신 채팅 id',
example: 99999,
required: false,
})
@IsOptional()
@IsNumber()
readonly latestChatId?: number;

@ApiProperty({
description: '페이지 크기',
example: 20,
default: 20,
required: false,
})
@IsOptional()
@IsNumber()
readonly pageSize?: number;
}
44 changes: 44 additions & 0 deletions packages/backend/src/chat/dto/chat.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ApiProperty } from '@nestjs/swagger';
import { Chat } from '@/chat/domain/chat.entity';
import { ChatType } from '@/chat/domain/chatType.enum';

interface ChatResponse {
id: number;
likeCount: number;
message: string;
type: string;
createdAt: Date;
}

export class ChatScrollResponse {
@ApiProperty({
description: '다음 페이지가 있는지 여부',
example: true,
})
readonly hasMore: boolean;

@ApiProperty({
description: '채팅 목록',
example: [
{
id: 1,
likeCount: 0,
message: '안녕하세요',
type: ChatType.NORMAL,
createdAt: new Date(),
},
],
})
readonly chats: ChatResponse[];

constructor(chats: Chat[], hasMore: boolean) {
this.chats = chats.map((chat) => ({
id: chat.id,
likeCount: chat.likeCount,
message: chat.message,
type: chat.type,
createdAt: chat.date!.createdAt,
}));
this.hasMore = hasMore;
}
}
4 changes: 2 additions & 2 deletions packages/backend/src/common/dateEmbedded.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { CreateDateColumn, UpdateDateColumn } from 'typeorm';

export class DateEmbedded {
@CreateDateColumn({ type: 'timestamp', name: 'created_at' })
createdAt?: Date;
createdAt: Date;

@UpdateDateColumn({ type: 'timestamp', name: 'updated_at' })
updatedAt?: Date;
updatedAt: Date;
}
7 changes: 6 additions & 1 deletion packages/backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ async function bootstrap() {

app.setGlobalPrefix('api');
app.use(session({ ...sessionConfig, store }));
app.useGlobalPipes(new ValidationPipe({ transform: true }));
app.useGlobalPipes(
new ValidationPipe({
transform: true,
transformOptions: { enableImplicitConversion: true },
}),
);
useSwagger(app);
app.use(passport.initialize());
app.use(passport.session());
Expand Down
11 changes: 3 additions & 8 deletions packages/backend/src/stock/decorator/stock.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {
Query,
ParseIntPipe,
DefaultValuePipe,
applyDecorators,
} from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiResponse } from '@nestjs/swagger';
import { StocksResponse } from '../dto/stock.Response';
import { applyDecorators, DefaultValuePipe, ParseIntPipe, Query } from "@nestjs/common";
import { ApiOperation, ApiQuery, ApiResponse } from "@nestjs/swagger";
import { StocksResponse } from "../dto/stock.response";

export function LimitQuery(defaultValue = 5): ParameterDecorator {
return Query('limit', new DefaultValuePipe(defaultValue), ParseIntPipe);
Expand Down
12 changes: 12 additions & 0 deletions packages/backend/src/stock/dto/stock.request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';

export class StockSearchRequest {
@ApiProperty({
description: '검색할 단어',
example: '삼성',
})
@IsNotEmpty()
@IsString()
name: string;
}
Loading