-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from boostcampwm-2024/feature/api/stockbuy-#49
[BE] 7.03 매수 API 구현 #49
- Loading branch information
Showing
12 changed files
with
179 additions
and
0 deletions.
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; | ||
} |
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