Skip to content

Commit

Permalink
refactor: upbit controller swagger 설명 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungGwan123 committed Nov 27, 2024
1 parent 958f005 commit 2a033c5
Show file tree
Hide file tree
Showing 31 changed files with 1,248 additions and 938 deletions.
2 changes: 1 addition & 1 deletion packages/server/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
'error',
{
endOfLine: 'auto',
useTabs: 'false',
useTabs: false,
},
],
'no-console': ['warn', { allow: ['warn', 'error'] }],
Expand Down
7 changes: 5 additions & 2 deletions packages/server/src/account/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export class AccountController {

constructor(private readonly accountService: AccountService) {}

@ApiOperation({ summary: '내 계정 정보 조회', description: '현재 로그인한 사용자의 계정 정보를 조회합니다.' })
@ApiOperation({
summary: '내 계정 정보 조회',
description: '현재 로그인한 사용자의 계정 정보를 조회합니다.',
})
@ApiResponse({
status: HttpStatus.OK,
description: '계정 정보 조회 성공',
Expand Down Expand Up @@ -56,4 +59,4 @@ export class AccountController {
throw error;
}
}
}
}
32 changes: 22 additions & 10 deletions packages/server/src/account/account.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { DataSource, Repository, QueryRunner } from 'typeorm';
import { Injectable, UnprocessableEntityException, Logger } from '@nestjs/common';
import {
Injectable,
UnprocessableEntityException,
Logger,
} from '@nestjs/common';
import { Account } from './account.entity';
import { User } from '@src/auth/user.entity';
import { CURRENCY_CONSTANTS } from './constants/currency.constants';
Expand All @@ -21,7 +25,7 @@ export class AccountRepository extends Repository<Account> {
account.USDT = CURRENCY_CONSTANTS.ADMIN_INITIAL_USDT;
account.BTC = CURRENCY_CONSTANTS.ADMIN_INITIAL_BTC;
account.user = adminUser;

await this.save(account);
this.logger.log(`관리자 계정 생성 완료: ${adminUser.id}`);
} catch (error) {
Expand Down Expand Up @@ -49,26 +53,31 @@ export class AccountRepository extends Repository<Account> {
accountId: number,
queryRunner: QueryRunner,
): Promise<void> {
this.logger.log(`계정 통화 업데이트 시작: accountId=${accountId}, type=${typeGiven}`);
this.logger.log(
`계정 통화 업데이트 시작: accountId=${accountId}, type=${typeGiven}`,
);
try {
await queryRunner.manager
.createQueryBuilder()
.update(Account)
.set({ [typeGiven]: accountBalance })
.where('id = :id', { id: accountId })
.execute();

this.logger.log(`계정 통화 업데이트 완료: accountId=${accountId}`);
} catch (error) {
this.logger.error(`계정 통화 업데이트 실패: ${error.message}`, error.stack);
this.logger.error(
`계정 통화 업데이트 실패: ${error.message}`,
error.stack,
);
throw error;
}
}

async updateAccountBTC(
id: number,
quantity: number,
queryRunner: QueryRunner
queryRunner: QueryRunner,
): Promise<void> {
this.logger.log(`BTC 잔액 업데이트 시작: accountId=${id}`);
try {
Expand All @@ -78,10 +87,13 @@ export class AccountRepository extends Repository<Account> {
.set({ BTC: quantity })
.where('id = :id', { id })
.execute();

this.logger.log(`BTC 잔액 업데이트 완료: accountId=${id}`);
} catch (error) {
this.logger.error(`BTC 잔액 업데이트 실패: ${error.message}`, error.stack);
this.logger.error(
`BTC 잔액 업데이트 실패: ${error.message}`,
error.stack,
);
throw error;
}
}
Expand All @@ -106,12 +118,12 @@ export class AccountRepository extends Repository<Account> {
const account = await queryRunner.manager.findOne(Account, {
where: { user: { id } },
});

this.logger.log(`계정 조회 완료: userId=${id}`);
return account;
} catch (error) {
this.logger.error(`계정 조회 실패: ${error.message}`, error.stack);
throw error;
}
}
}
}
21 changes: 16 additions & 5 deletions packages/server/src/account/account.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { HttpStatus, Injectable, Logger, UnauthorizedException } from '@nestjs/common';
import {
HttpStatus,
Injectable,
Logger,
UnauthorizedException,
} from '@nestjs/common';
import { AssetRepository } from '@src/asset/asset.repository';
import { CoinDto, MyAccountDto } from './dtos/my-account.dto';
import { CoinDataUpdaterService } from '@src/upbit/coin-data-updater.service';
Expand All @@ -19,7 +24,7 @@ export class AccountService {

async getMyAccountData(user: UserDto): Promise<MyAccountResponseDto> {
this.logger.log(`계정 데이터 조회 시작: ${user.userId}`);

const account = await this.accountRepository.findOne({
where: { user: { id: user.userId } },
});
Expand Down Expand Up @@ -57,7 +62,10 @@ export class AccountService {
}
}

private mapCoinsData(myCoins: Asset[], coinNameData: Map<string, string>): CoinDto[] {
private mapCoinsData(
myCoins: Asset[],
coinNameData: Map<string, string>,
): CoinDto[] {
return myCoins.map((myCoin) => {
const name = myCoin.assetName;
return {
Expand All @@ -72,7 +80,10 @@ export class AccountService {
});
}

private getKoreanName(name: string, coinNameData: Map<string, string>): string {
private getKoreanName(
name: string,
coinNameData: Map<string, string>,
): string {
const markets = ['KRW', 'BTC', 'USDT'];
for (const market of markets) {
const koreanName = coinNameData.get(`${market}-${name}`);
Expand All @@ -84,4 +95,4 @@ export class AccountService {
private calculateTotalPrice(coins: CoinDto[]): number {
return coins.reduce((total, coin) => total + coin.price, 0);
}
}
}
10 changes: 5 additions & 5 deletions packages/server/src/account/constants/currency.constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const CURRENCY_CONSTANTS = {
ADMIN_INITIAL_KRW: 300000000,
ADMIN_INITIAL_USDT: 300000,
ADMIN_INITIAL_BTC: 0,
UPBIT_IMAGE_URL: 'https://static.upbit.com/logos/',
} as const;
ADMIN_INITIAL_KRW: 300000000,
ADMIN_INITIAL_USDT: 300000,
ADMIN_INITIAL_BTC: 0,
UPBIT_IMAGE_URL: 'https://static.upbit.com/logos/',
} as const;
2 changes: 1 addition & 1 deletion packages/server/src/account/dtos/my-account.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export class MyAccountDto {

@IsArray()
@ValidateNested({ each: true })
@Type(() => CoinDto)
@Type(() => CoinDto)
coins: CoinDto[];
}
29 changes: 14 additions & 15 deletions packages/server/src/account/dtos/my-account.response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ export class MyAccountResponseDto {
message: MyAccountDto;
}


export class UserDto {
@ApiProperty({
example: 1,
description: '사용자 고유 ID',
})
@IsNumber()
readonly userId: number;
@ApiProperty({
example: 'john_doe',
description: '사용자 이름',
})
@IsString()
readonly userName: string;
}
@ApiProperty({
example: 1,
description: '사용자 고유 ID',
})
@IsNumber()
readonly userId: number;

@ApiProperty({
example: 'john_doe',
description: '사용자 이름',
})
@IsString()
readonly userName: string;
}
82 changes: 63 additions & 19 deletions packages/server/src/asset/asset.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { DataSource, Repository, QueryRunner } from 'typeorm';
import { Injectable, Logger, InternalServerErrorException } from '@nestjs/common';
import {
Injectable,
Logger,
InternalServerErrorException,
} from '@nestjs/common';
import { Account } from '@src/account/account.entity';
import { Asset } from './asset.entity';

Expand All @@ -18,7 +22,9 @@ export class AssetRepository extends Repository<Asset> {
quantity: number,
queryRunner: QueryRunner,
): Promise<Asset> {
this.logger.log(`자산 생성 시작: type=${typeReceived}, accountId=${account.id}`);
this.logger.log(
`자산 생성 시작: type=${typeReceived}, accountId=${account.id}`,
);
try {
const asset = new Asset();
asset.assetName = typeReceived;
Expand All @@ -29,15 +35,20 @@ export class AssetRepository extends Repository<Asset> {

const savedAsset = await queryRunner.manager.save(Asset, asset);
this.logger.log(`자산 생성 완료: assetId=${savedAsset.assetId}`);

return savedAsset;
} catch (error) {
this.logger.error(`자산 생성 실패: ${error.message}`, error.stack);
throw new InternalServerErrorException('자산 생성 중 오류가 발생했습니다.');
throw new InternalServerErrorException(
'자산 생성 중 오류가 발생했습니다.',
);
}
}

async updateAssetQuantityPrice(asset: Asset, queryRunner: QueryRunner): Promise<void> {
async updateAssetQuantityPrice(
asset: Asset,
queryRunner: QueryRunner,
): Promise<void> {
this.logger.log(`자산 수량/가격 업데이트 시작: assetId=${asset.assetId}`);
try {
await queryRunner.manager
Expand All @@ -53,12 +64,20 @@ export class AssetRepository extends Repository<Asset> {

this.logger.log(`자산 수량/가격 업데이트 완료: assetId=${asset.assetId}`);
} catch (error) {
this.logger.error(`자산 수량/가격 업데이트 실패: ${error.message}`, error.stack);
throw new InternalServerErrorException('자산 업데이트 중 오류가 발생했습니다.');
this.logger.error(
`자산 수량/가격 업데이트 실패: ${error.message}`,
error.stack,
);
throw new InternalServerErrorException(
'자산 업데이트 중 오류가 발생했습니다.',
);
}
}

async updateAssetQuantity(asset: Asset, queryRunner: QueryRunner): Promise<void> {
async updateAssetQuantity(
asset: Asset,
queryRunner: QueryRunner,
): Promise<void> {
this.logger.log(`자산 수량 업데이트 시작: assetId=${asset.assetId}`);
try {
await queryRunner.manager
Expand All @@ -70,12 +89,20 @@ export class AssetRepository extends Repository<Asset> {

this.logger.log(`자산 수량 업데이트 완료: assetId=${asset.assetId}`);
} catch (error) {
this.logger.error(`자산 수량 업데이트 실패: ${error.message}`, error.stack);
throw new InternalServerErrorException('자산 수량 업데이트 중 오류가 발생했습니다.');
this.logger.error(
`자산 수량 업데이트 실패: ${error.message}`,
error.stack,
);
throw new InternalServerErrorException(
'자산 수량 업데이트 중 오류가 발생했습니다.',
);
}
}

async updateAssetAvailableQuantity(asset: Asset, queryRunner: QueryRunner): Promise<void> {
async updateAssetAvailableQuantity(
asset: Asset,
queryRunner: QueryRunner,
): Promise<void> {
this.logger.log(`거래가능 수량 업데이트 시작: assetId=${asset.assetId}`);
try {
await queryRunner.manager
Expand All @@ -87,12 +114,20 @@ export class AssetRepository extends Repository<Asset> {

this.logger.log(`거래가능 수량 업데이트 완료: assetId=${asset.assetId}`);
} catch (error) {
this.logger.error(`거래가능 수량 업데이트 실패: ${error.message}`, error.stack);
throw new InternalServerErrorException('거래가능 수량 업데이트 중 오류가 발생했습니다.');
this.logger.error(
`거래가능 수량 업데이트 실패: ${error.message}`,
error.stack,
);
throw new InternalServerErrorException(
'거래가능 수량 업데이트 중 오류가 발생했습니다.',
);
}
}

async updateAssetPrice(asset: Asset, queryRunner: QueryRunner): Promise<void> {
async updateAssetPrice(
asset: Asset,
queryRunner: QueryRunner,
): Promise<void> {
this.logger.log(`자산 가격 업데이트 시작: assetId=${asset.assetId}`);
try {
await queryRunner.manager
Expand All @@ -107,8 +142,13 @@ export class AssetRepository extends Repository<Asset> {

this.logger.log(`자산 가격 업데이트 완료: assetId=${asset.assetId}`);
} catch (error) {
this.logger.error(`자산 가격 업데이트 실패: ${error.message}`, error.stack);
throw new InternalServerErrorException('자산 가격 업데이트 중 오류가 발생했습니다.');
this.logger.error(
`자산 가격 업데이트 실패: ${error.message}`,
error.stack,
);
throw new InternalServerErrorException(
'자산 가격 업데이트 중 오류가 발생했습니다.',
);
}
}

Expand All @@ -126,11 +166,15 @@ export class AssetRepository extends Repository<Asset> {
},
});

this.logger.log(`자산 조회 완료: ${asset ? `assetId=${asset.assetId}` : '자산 없음'}`);
this.logger.log(
`자산 조회 완료: ${asset ? `assetId=${asset.assetId}` : '자산 없음'}`,
);
return asset;
} catch (error) {
this.logger.error(`자산 조회 실패: ${error.message}`, error.stack);
throw new InternalServerErrorException('자산 조회 중 오류가 발생했습니다.');
throw new InternalServerErrorException(
'자산 조회 중 오류가 발생했습니다.',
);
}
}
}
}
Loading

0 comments on commit 2a033c5

Please sign in to comment.