Skip to content

Commit

Permalink
Revert "[BE] 7.07 거래 체결 기능 구현 (WebSocket) #53"
Browse files Browse the repository at this point in the history
  • Loading branch information
sieunie authored Nov 14, 2024
1 parent ad6afbc commit 857334b
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 119 deletions.
4 changes: 2 additions & 2 deletions BE/src/stock/order/stock-order-socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class StockOrderSocketService {

const totalPrice = order.price * order.amount;
const fee = this.calculateFee(totalPrice);
await this.stockOrderRepository.updateOrderAndAssetAndUserStockWhenBuy(
await this.stockOrderRepository.updateOrderAndAssetWhenBuy(
order,
totalPrice + fee,
);
Expand All @@ -100,7 +100,7 @@ export class StockOrderSocketService {

const totalPrice = order.price * order.amount;
const fee = this.calculateFee(totalPrice);
await this.stockOrderRepository.updateOrderAndAssetAndUserStockWhenSell(
await this.stockOrderRepository.updateOrderAndAssetWhenSell(
order,
totalPrice - fee,
);
Expand Down
8 changes: 1 addition & 7 deletions BE/src/stock/order/stock-order.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ import { StockOrderRepository } from './stock-order.repository';
import { SocketModule } from '../../websocket/socket.module';
import { AssetModule } from '../../asset/asset.module';
import { StockOrderSocketService } from './stock-order-socket.service';
import { UserStockModule } from '../../userStock/user-stock.module';

@Module({
imports: [
TypeOrmModule.forFeature([Order]),
SocketModule,
AssetModule,
UserStockModule,
],
imports: [TypeOrmModule.forFeature([Order]), SocketModule, AssetModule],
controllers: [StockOrderController],
providers: [StockOrderService, StockOrderRepository, StockOrderSocketService],
})
Expand Down
47 changes: 10 additions & 37 deletions BE/src/stock/order/stock-order.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { Order } from './stock-order.entity';
import { StatusType } from './enum/status-type';
import { Asset } from '../../asset/asset.entity';
import { UserStock } from '../../userStock/user-stock.entity';

@Injectable()
export class StockOrderRepository extends Repository<Order> {
Expand All @@ -18,7 +17,7 @@ export class StockOrderRepository extends Repository<Order> {
.getRawMany();
}

async updateOrderAndAssetAndUserStockWhenBuy(order, realPrice) {
async updateOrderAndAssetWhenBuy(order, realPrice) {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.startTransaction();

Expand All @@ -33,32 +32,14 @@ export class StockOrderRepository extends Repository<Order> {
.createQueryBuilder()
.update(Asset)
.set({
cash_balance: () => `cash_balance - ${realPrice}`,
total_asset: () => `total_asset - ${realPrice}`,
total_profit: () => `total_profit - ${realPrice}`,
cash_balance: () => 'cash_balance - :realPrice',
total_asset: () => 'total_asset - :realPrice',
total_profit: () => 'total_profit - :realPrice',
total_profit_rate: () => `total_profit / 10000000`,
last_updated: new Date(),
})
.where({ user_id: order.user_id })
.execute();

await queryRunner.manager
.createQueryBuilder()
.insert()
.into(UserStock)
.values({
user_id: order.user_id,
stock_code: order.stock_code,
quantity: order.amount,
avg_price: order.price,
})
.orUpdate(
[
`quantity = quantity + ${order.amount}`,
`avg_price = ((avg_price * quantity + ${order.price} * ${order.amount}) / (quantity + ${order.amount}))`,
],
['user_id', 'stock_code'],
)
.setParameter('realPrice', realPrice)
.execute();

await queryRunner.commitTransaction();
Expand All @@ -70,7 +51,7 @@ export class StockOrderRepository extends Repository<Order> {
}
}

async updateOrderAndAssetAndUserStockWhenSell(order, realPrice) {
async updateOrderAndAssetWhenSell(order, realPrice) {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.startTransaction();

Expand All @@ -85,22 +66,14 @@ export class StockOrderRepository extends Repository<Order> {
.createQueryBuilder()
.update(Asset)
.set({
cash_balance: () => `cash_balance + ${realPrice}`,
total_asset: () => `total_asset + ${realPrice}`,
total_profit: () => `total_profit + ${realPrice}`,
cash_balance: () => 'cash_balance + :realPrice',
total_asset: () => 'total_asset + :realPrice',
total_profit: () => 'total_profit + :realPrice',
total_profit_rate: () => `total_profit / 10000000`,
last_updated: new Date(),
})
.where({ user_id: order.user_id })
.execute();

await queryRunner.manager
.createQueryBuilder()
.update(UserStock)
.set({
quantity: () => `quantity - ${order.amount}`,
})
.where({ user_id: order.user_id, stock_code: order.stock_code })
.setParameter('realPrice', realPrice)
.execute();

await queryRunner.commitTransaction();
Expand Down
11 changes: 0 additions & 11 deletions BE/src/stock/order/stock-order.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
BadRequestException,
ConflictException,
ForbiddenException,
Injectable,
Expand All @@ -10,14 +9,12 @@ import { StockOrderRepository } from './stock-order.repository';
import { TradeType } from './enum/trade-type';
import { StatusType } from './enum/status-type';
import { StockOrderSocketService } from './stock-order-socket.service';
import { UserStockRepository } from '../../userStock/user-stock.repository';

@Injectable()
export class StockOrderService {
constructor(
private readonly stockOrderRepository: StockOrderRepository,
private readonly stockOrderSocketService: StockOrderSocketService,
private readonly userStockRepository: UserStockRepository,
) {}

async buy(userId: number, stockOrderRequest: StockOrderRequestDto) {
Expand All @@ -35,14 +32,6 @@ export class StockOrderService {
}

async sell(userId: number, stockOrderRequest: StockOrderRequestDto) {
const userStock = await this.userStockRepository.findOneBy({
user_id: userId,
stock_code: stockOrderRequest.stock_code,
});

if (!userStock || userStock.quantity === 0)
throw new BadRequestException('주식을 매도 수만큼 가지고 있지 않습니다.');

const order = this.stockOrderRepository.create({
user_id: userId,
stock_code: stockOrderRequest.stock_code,
Expand Down
6 changes: 0 additions & 6 deletions BE/src/userStock/user-stock.controller.ts

This file was deleted.

27 changes: 0 additions & 27 deletions BE/src/userStock/user-stock.entity.ts

This file was deleted.

14 changes: 0 additions & 14 deletions BE/src/userStock/user-stock.module.ts

This file was deleted.

11 changes: 0 additions & 11 deletions BE/src/userStock/user-stock.repository.ts

This file was deleted.

4 changes: 0 additions & 4 deletions BE/src/userStock/user-stock.service.ts

This file was deleted.

0 comments on commit 857334b

Please sign in to comment.