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

stock detail 기본쿼리가 아닌 left join 으로 변경 #236

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions packages/backend/src/stock/dto/stockDetail.response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { StockDetail } from '../domain/stockDetail.entity';

export class StockDetailResponse {
@ApiProperty({
Expand Down Expand Up @@ -36,4 +37,13 @@ export class StockDetailResponse {
example: 53000,
})
low52w: number;

constructor(stockDetail: StockDetail) {
this.eps = stockDetail.eps;
this.per = stockDetail.per;
this.high52w = stockDetail.high52w;
this.low52w = stockDetail.low52w;
this.marketCap = Number(stockDetail.marketCap);
this.name = stockDetail.stock.name;
Copy link
Collaborator

Choose a reason for hiding this comment

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

굿굿~

}
}
23 changes: 12 additions & 11 deletions packages/backend/src/stock/stockDetail.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Injectable, Inject, NotFoundException } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { DataSource } from 'typeorm';
import { Logger } from 'winston';
import { Stock } from './domain/stock.entity';
import { StockDetail } from './domain/stockDetail.entity';
import { StockDetailResponse } from './dto/stockDetail.response';

Expand All @@ -26,17 +24,20 @@ export class StockDetailService {
);
}

const stockDetail = await manager.findBy(StockDetail, {
stock: { id: stockId },
});

const stockName = await manager.findBy(Stock, {
id: stockId,
});
const result = await this.datasource.manager
.getRepository(StockDetail)
.createQueryBuilder('stockDetail')
.leftJoinAndSelect('stockDetail.stock', 'stock')
.where('stockDetail.stock_id = :stockId', { stockId })
.getOne();

const result = { name: stockName[0].name, ...stockDetail[0] };
if (!result) {
throw new NotFoundException(
`stock detail not found (stockId: ${stockId}`,
);
}

return plainToInstance(StockDetailResponse, result);
return new StockDetailResponse(result);
});
}
}