Skip to content

Commit

Permalink
Merge pull request #99 from Team-INSERT/refactor/#92
Browse files Browse the repository at this point in the history
refactor(coin): coin 도메인 리팩토링 및 테스트 코드 작성
  • Loading branch information
hw9402 authored May 3, 2024
2 parents a6de4c0 + 01f5442 commit 6edce2f
Show file tree
Hide file tree
Showing 42 changed files with 1,457 additions and 502 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.time.LocalDateTime;

import com.project.bumawiki.domain.coin.exception.CoinNotEnoughException;
import com.project.bumawiki.domain.coin.exception.MoneyNotEnoughException;
import com.project.bumawiki.global.error.exception.BumawikiException;
import com.project.bumawiki.global.error.exception.ErrorCode;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -37,15 +37,15 @@ public CoinAccount(Long userId, Long money) {

public void buyCoin(Long coinPrice, Long coinCount) {
if (this.money < coinPrice * coinCount) {
throw new MoneyNotEnoughException();
throw new BumawikiException(ErrorCode.MONEY_NOT_ENOUGH);
}
this.money -= coinPrice * coinCount;
this.coin += coinCount;
}

public void sellCoin(Long coinPrice, Long coinCount) {
if (this.coin < coinCount) {
throw new CoinNotEnoughException();
throw new BumawikiException(ErrorCode.COIN_NOT_ENOUGH);
}
this.coin -= coinCount;
this.money += coinPrice * coinCount;
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/project/bumawiki/domain/coin/domain/TradeVo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.project.bumawiki.domain.coin.domain;

import com.project.bumawiki.domain.coin.domain.type.TradeStatus;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class TradeVo {
@NotNull
@Positive
private Long coinPrice;

@NotNull
@Positive
private Long coinCount;

public Trade toTrade(CoinAccount coinAccount) {
return new Trade(
coinPrice,
coinCount,
coinPrice * coinCount,
TradeStatus.NONE,
coinAccount.getId()
);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import org.springframework.data.jpa.repository.Query;

import com.project.bumawiki.domain.coin.domain.CoinAccount;
import com.project.bumawiki.domain.coin.exception.CoinAccountNotFoundException;
import com.project.bumawiki.global.error.exception.BumawikiException;
import com.project.bumawiki.global.error.exception.ErrorCode;

public interface CoinAccountRepository extends JpaRepository<CoinAccount, Long> {
Optional<CoinAccount> findByUserId(Long userId);
Expand All @@ -17,12 +18,12 @@ public interface CoinAccountRepository extends JpaRepository<CoinAccount, Long>

default CoinAccount getByUserId(Long userId) {
return findByUserId(userId)
.orElseThrow(CoinAccountNotFoundException::new);
.orElseThrow(() -> new BumawikiException(ErrorCode.COIN_ACCOUNT_NOT_FOUND_EXCEPTION));
}

default CoinAccount getById(Long id) {
return findById(id)
.orElseThrow(CoinAccountNotFoundException::new);
.orElseThrow(() -> new BumawikiException(ErrorCode.COIN_ACCOUNT_NOT_FOUND_EXCEPTION));
}

@Query(value = "select c from CoinAccount c where c.coin > 0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ public interface PriceRepository extends JpaRepository<Price, Long> {

default Price getRecentPrice() {
return findTopOrderByStartedTime()
.orElse(save(
new Price(1000000L)
));
// .orElseThrow(
// PriceNotFoundException::new
// );
.orElseGet(() ->
save(new Price(1000000L))
);
}

@Query("select p from Price p where p.startedTime >= :twoWeeksAgo order by p.startedTime asc")
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.project.bumawiki.domain.coin.implementation;

import com.project.bumawiki.domain.coin.domain.CoinAccount;
import com.project.bumawiki.domain.coin.domain.repository.CoinAccountRepository;
import com.project.bumawiki.global.annotation.Implementation;

import lombok.RequiredArgsConstructor;

@Implementation
@RequiredArgsConstructor
public class CoinAccountCreator {

private final CoinAccountRepository coinAccountRepository;

public CoinAccount create(CoinAccount coinAccount) {
return coinAccountRepository.save(coinAccount);
}
}
Loading

0 comments on commit 6edce2f

Please sign in to comment.