-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change dependency direction (#87)
* chore: add auto generated qclass to .gitignore * fix: failed test by current time * refactor: apply dip for package dependency * chore: update interface name * test: modify test data * fix: test code
- Loading branch information
Showing
8 changed files
with
109 additions
and
167 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
86 changes: 86 additions & 0 deletions
86
...ain/java/nexters/payout/apiserver/dividend/application/StockDividendQueryServiceImpl.java
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,86 @@ | ||
package nexters.payout.apiserver.dividend.application; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import nexters.payout.apiserver.stock.application.StockDividendQueryService; | ||
import nexters.payout.apiserver.stock.application.dto.response.DividendResponse; | ||
import nexters.payout.apiserver.stock.application.dto.response.StockDetailResponse; | ||
import nexters.payout.core.time.InstantProvider; | ||
import nexters.payout.domain.dividend.domain.Dividend; | ||
import nexters.payout.domain.dividend.domain.repository.DividendRepository; | ||
import nexters.payout.domain.stock.domain.Stock; | ||
import nexters.payout.domain.stock.domain.exception.TickerNotFoundException; | ||
import nexters.payout.domain.stock.domain.repository.StockRepository; | ||
import nexters.payout.domain.stock.domain.service.StockDividendAnalysisService; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.Month; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class StockDividendQueryServiceImpl implements StockDividendQueryService { | ||
|
||
private final StockDividendAnalysisService dividendAnalysisService; | ||
private final StockRepository stockRepository; | ||
private final DividendRepository dividendRepository; | ||
|
||
public StockDetailResponse getStockByTicker(final String ticker) { | ||
Stock stock = getStock(ticker); | ||
|
||
List<Dividend> lastYearDividends = getLastYearDividends(stock); | ||
List<Dividend> thisYearDividends = getThisYearDividends(stock); | ||
|
||
if (lastYearDividends.isEmpty() && thisYearDividends.isEmpty()) { | ||
return StockDetailResponse.of(stock, DividendResponse.noDividend()); | ||
} | ||
|
||
List<Month> dividendMonths = dividendAnalysisService.calculateDividendMonths(stock, lastYearDividends); | ||
Double dividendYield = dividendAnalysisService.calculateDividendYield(stock, lastYearDividends); | ||
Double dividendPerShare = dividendAnalysisService.calculateAverageDividend( | ||
combinedDividends(lastYearDividends, thisYearDividends) | ||
); | ||
|
||
return dividendAnalysisService.findUpcomingDividend(lastYearDividends, thisYearDividends) | ||
.map(upcomingDividend -> StockDetailResponse.of( | ||
stock, | ||
DividendResponse.fullDividendInfo(upcomingDividend, dividendYield, dividendMonths) | ||
)) | ||
.orElse(StockDetailResponse.of( | ||
stock, | ||
DividendResponse.withoutDividendDates(dividendPerShare, dividendYield, dividendMonths) | ||
)); | ||
} | ||
|
||
private List<Dividend> combinedDividends(final List<Dividend> lastYearDividends, final List<Dividend> thisYearDividends) { | ||
return Stream.of(lastYearDividends, thisYearDividends) | ||
.flatMap(List::stream) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Stock getStock(final String ticker) { | ||
return stockRepository.findByTicker(ticker) | ||
.orElseThrow(() -> new TickerNotFoundException(ticker)); | ||
} | ||
|
||
private List<Dividend> getLastYearDividends(final Stock stock) { | ||
int lastYear = InstantProvider.getLastYear(); | ||
|
||
return dividendRepository.findAllByStockId(stock.getId()) | ||
.stream() | ||
.filter(dividend -> InstantProvider.toLocalDate(dividend.getExDividendDate()).getYear() == lastYear) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<Dividend> getThisYearDividends(final Stock stock) { | ||
int thisYear = InstantProvider.getThisYear(); | ||
|
||
return dividendRepository.findAllByStockId(stock.getId()) | ||
.stream() | ||
.filter(dividend -> InstantProvider.toLocalDate(dividend.getExDividendDate()).getYear() == thisYear) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...r/src/main/java/nexters/payout/apiserver/stock/application/StockDividendQueryService.java
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,7 @@ | ||
package nexters.payout.apiserver.stock.application; | ||
|
||
import nexters.payout.apiserver.stock.application.dto.response.StockDetailResponse; | ||
|
||
public interface StockDividendQueryService { | ||
StockDetailResponse getStockByTicker(final String ticker); | ||
} |
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
41 changes: 0 additions & 41 deletions
41
domain/src/main/generated/nexters/payout/domain/QBaseEntity.java
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
domain/src/main/generated/nexters/payout/domain/dividend/domain/QDividend.java
This file was deleted.
Oops, something went wrong.
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