-
Notifications
You must be signed in to change notification settings - Fork 1
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
[BE] 7.03 매수 API 구현 #49 #70
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
697eb50
✨ feat: jwt authGuard 추가
sieunie e1d5582
✨ feat: 주식 매수 API 구현 #49
sieunie 8c5dc07
Merge branch 'back/main' into feature/api/stockbuy-#49
sieunie cce5466
merge conflict resolve app.module.ts
sieunie 8ecc422
♻️ refactor: remove unused import
sieunie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') {} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsInt, IsNumber, IsPositive } from 'class-validator'; | ||
|
||
export class StockOrderRequestDto { | ||
@ApiProperty({ description: '주식 id' }) | ||
@IsInt() | ||
@IsPositive() | ||
stock_id: number; | ||
|
||
@ApiProperty({ description: '매수/매도 희망 가격' }) | ||
@IsNumber() | ||
@IsPositive() | ||
price: number; | ||
|
||
@ApiProperty({ description: '매수/매도 희망 수량' }) | ||
@IsInt() | ||
@IsPositive() | ||
amount: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum StatusType { | ||
PENDING = 'PENDING', | ||
COMPLETE = 'COMPLETE', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum TradeType { | ||
SELL = 'SELL', | ||
BUY = 'BUY', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface RequestInterface { | ||
user: { | ||
id: number; | ||
email: string; | ||
password: string; | ||
tutorial: boolean; | ||
kakaoId: number; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Post, | ||
Req, | ||
UseGuards, | ||
ValidationPipe, | ||
} from '@nestjs/common'; | ||
import { | ||
ApiBearerAuth, | ||
ApiOperation, | ||
ApiResponse, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { StockOrderService } from './stock-order.service'; | ||
import { StockOrderRequestDto } from './dto/stock-order-request.dto'; | ||
import { JwtAuthGuard } from '../../auth/jwt-auth-guard'; | ||
import { RequestInterface } from './interface/request.interface'; | ||
|
||
@Controller('/api/stocks/trade') | ||
@ApiTags('주식 매수/매도 API') | ||
export class StockOrderController { | ||
constructor(private readonly stockTradeService: StockOrderService) {} | ||
|
||
@Post('/buy') | ||
@ApiBearerAuth() | ||
@UseGuards(JwtAuthGuard) | ||
@ApiOperation({ | ||
summary: '주식 매수 API', | ||
description: '주식 id, 매수 가격, 수량으로 주식을 매수한다.', | ||
}) | ||
@ApiResponse({ | ||
status: 201, | ||
description: '주식 매수 성공', | ||
}) | ||
async buy( | ||
@Req() request: RequestInterface, | ||
@Body(ValidationPipe) stockOrderRequest: StockOrderRequestDto, | ||
) { | ||
await this.stockTradeService.buy(request.user.id, stockOrderRequest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
} from 'typeorm'; | ||
import { TradeType } from './enum/trade-type'; | ||
import { StatusType } from './enum/status-type'; | ||
|
||
@Entity('orders') | ||
export class Order { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ nullable: false }) | ||
user_id: number; | ||
|
||
@Column({ nullable: false }) | ||
stock_id: number; | ||
|
||
@Column({ | ||
type: 'enum', | ||
enum: TradeType, | ||
nullable: false, | ||
}) | ||
trade_type: TradeType; | ||
|
||
@Column({ nullable: false }) | ||
amount: number; | ||
|
||
@Column({ nullable: false }) | ||
price: number; | ||
|
||
@Column({ | ||
type: 'enum', | ||
enum: StatusType, | ||
nullable: false, | ||
}) | ||
status: StatusType; | ||
|
||
@CreateDateColumn() | ||
created_at: Date; | ||
|
||
@Column({ nullable: true }) | ||
completed_at?: Date; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 타입스크립트에서는 nullable한 값은 옵셔널로 두는게 관례인가요? 신기하네요 |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { StockOrderController } from './stock-order.controller'; | ||
import { StockOrderService } from './stock-order.service'; | ||
import { Order } from './stock-order.entity'; | ||
import { StockOrderRepository } from './stock-order.repository'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Order])], | ||
controllers: [StockOrderController], | ||
providers: [StockOrderService, StockOrderRepository], | ||
}) | ||
export class StockOrderModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { DataSource, Repository } from 'typeorm'; | ||
import { InjectDataSource } from '@nestjs/typeorm'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Order } from './stock-order.entity'; | ||
|
||
@Injectable() | ||
export class StockOrderRepository extends Repository<Order> { | ||
constructor(@InjectDataSource() dataSource: DataSource) { | ||
super(Order, dataSource.createEntityManager()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { StockOrderRequestDto } from './dto/stock-order-request.dto'; | ||
import { StockOrderRepository } from './stock-order.repository'; | ||
import { TradeType } from './enum/trade-type'; | ||
import { StatusType } from './enum/status-type'; | ||
|
||
@Injectable() | ||
export class StockOrderService { | ||
constructor(private readonly stockOrderRepository: StockOrderRepository) {} | ||
|
||
async buy(userId: number, stockOrderRequest: StockOrderRequestDto) { | ||
const order = this.stockOrderRepository.create({ | ||
user_id: userId, | ||
stock_id: stockOrderRequest.stock_id, | ||
trade_type: TradeType.BUY, | ||
amount: stockOrderRequest.amount, | ||
price: stockOrderRequest.price, | ||
status: StatusType.PENDING, | ||
}); | ||
|
||
await this.stockOrderRepository.save(order); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ export function setupSwagger(app: INestApplication): void { | |
.setTitle('Juga API') | ||
.setDescription('Juga API 문서입니다.') | ||
.setVersion('1.0.0') | ||
.addBearerAuth() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 처음으로 토큰이 필요한 무언가가 나왔네요.. 고생하셨습니다!! |
||
.build(); | ||
|
||
const document = SwaggerModule.createDocument(app, options); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟢 오우 JwtAuthGuard를 클래스로 만들어서 사용할 수 있었네요 ..? 저는 AuthGuard('strategy 이름') 이런 식으로 사용해봤는데 가독성이나 이후 검증 로직을 추가하거나 하는데 좋을것 같네요 canActivate 함수에 원하는 로직을 넣거나 해서요