-
Notifications
You must be signed in to change notification settings - Fork 0
종목 조회 및 시세 API 구현 #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/team/cklob/arena/domain/market/MarketErrorCode.kt
This file contains hidden or 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,16 @@ | ||
| package team.cklob.arena.domain.market | ||
|
|
||
| import org.springframework.http.HttpStatus | ||
| import team.cklob.arena.global.exception.ErrorCode | ||
|
|
||
| enum class MarketErrorCode( | ||
| override val status: HttpStatus, | ||
| override val message: String, | ||
| ) : ErrorCode { | ||
| MARKET_NOT_ACTIVE(HttpStatus.CONFLICT, "현재 이용할 수 없는 시장입니다."), | ||
| SYMBOL_NOT_FOUND(HttpStatus.NOT_FOUND, "종목을 찾을 수 없습니다."), | ||
| SYMBOL_NOT_ACTIVE(HttpStatus.CONFLICT, "현재 이용할 수 없는 종목입니다."), | ||
| MARKET_DATA_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE, "시세 정보를 일시적으로 조회할 수 없습니다."), | ||
| SYMBOL_SYNC_REJECTED(HttpStatus.SERVICE_UNAVAILABLE, "종목 동기화 응답이 불완전하여 기존 데이터를 유지합니다."), | ||
| INVALID_PRICE_HISTORY_RANGE(HttpStatus.BAD_REQUEST, "가격 조회 기간은 최대 7일이며 시작일이 종료일보다 늦을 수 없습니다."), | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/team/cklob/arena/domain/market/application/GetCurrentPriceService.kt
This file contains hidden or 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 team.cklob.arena.domain.market.application | ||
|
|
||
| import team.cklob.arena.domain.market.application.result.CurrentPriceResult | ||
|
|
||
| interface GetCurrentPriceService { | ||
| fun execute(symbolId: Long): CurrentPriceResult | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/team/cklob/arena/domain/market/application/GetPriceHistoryService.kt
This file contains hidden or 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,12 @@ | ||
| package team.cklob.arena.domain.market.application | ||
|
|
||
| import team.cklob.arena.domain.market.application.result.PriceHistoryResult | ||
| import java.time.LocalDate | ||
|
|
||
| interface GetPriceHistoryService { | ||
| fun execute( | ||
| symbolId: Long, | ||
| from: LocalDate, | ||
| to: LocalDate, | ||
| ): PriceHistoryResult | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/team/cklob/arena/domain/market/application/GetSymbolsService.kt
This file contains hidden or 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,12 @@ | ||
| package team.cklob.arena.domain.market.application | ||
|
|
||
| import team.cklob.arena.domain.market.application.result.SymbolPageResult | ||
| import team.cklob.arena.domain.market.domain.type.MarketType | ||
|
|
||
| interface GetSymbolsService { | ||
| fun execute( | ||
| market: MarketType, | ||
| page: Int, | ||
| size: Int, | ||
| ): SymbolPageResult | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/team/cklob/arena/domain/market/application/MarketDataCache.kt
This file contains hidden or 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,22 @@ | ||
| package team.cklob.arena.domain.market.application | ||
|
|
||
| import team.cklob.arena.domain.market.application.result.CurrentPriceResult | ||
| import team.cklob.arena.domain.market.application.result.PriceHistoryResult | ||
| import team.cklob.arena.domain.market.domain.type.MarketType | ||
|
|
||
| interface MarketDataCache { | ||
| fun findCurrentPrice(symbolId: Long): CurrentPriceResult? | ||
|
|
||
| fun saveCurrentPrice( | ||
| market: MarketType, | ||
| result: CurrentPriceResult, | ||
| ) | ||
|
|
||
| fun findPriceHistory( | ||
| symbolId: Long, | ||
| from: String, | ||
| to: String, | ||
| ): PriceHistoryResult? | ||
|
|
||
| fun savePriceHistory(result: PriceHistoryResult) | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/team/cklob/arena/domain/market/application/MarketDataClient.kt
This file contains hidden or 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,18 @@ | ||
| package team.cklob.arena.domain.market.application | ||
|
|
||
| import team.cklob.arena.domain.market.application.result.ExternalSymbolResult | ||
| import team.cklob.arena.domain.market.application.result.PricePointResult | ||
| import team.cklob.arena.domain.market.application.result.PriceQuoteResult | ||
| import java.time.LocalDate | ||
|
|
||
| interface MarketDataClient { | ||
| fun fetchSymbols(): List<ExternalSymbolResult> | ||
|
|
||
| fun fetchCurrentPrice(code: String): PriceQuoteResult | ||
|
|
||
| fun fetchPriceHistory( | ||
| code: String, | ||
| from: LocalDate, | ||
| to: LocalDate, | ||
| ): List<PricePointResult> | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/team/cklob/arena/domain/market/application/MarketValidator.kt
This file contains hidden or 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,26 @@ | ||
| package team.cklob.arena.domain.market.application | ||
|
|
||
| import org.springframework.stereotype.Component | ||
| import team.cklob.arena.domain.market.MarketErrorCode | ||
| import team.cklob.arena.domain.market.domain.entity.Symbol | ||
| import team.cklob.arena.domain.market.domain.repository.SymbolRepository | ||
| import team.cklob.arena.domain.market.domain.type.MarketType | ||
| import team.cklob.arena.domain.market.infrastructure.property.MarketProperties | ||
| import team.cklob.arena.global.exception.ExpectedException | ||
|
|
||
| @Component | ||
| class MarketValidator( | ||
| private val symbolRepository: SymbolRepository, | ||
| private val marketProperties: MarketProperties, | ||
| ) { | ||
| fun requireActiveMarket(market: MarketType) { | ||
| if (market !in marketProperties.activeMarkets) throw ExpectedException(MarketErrorCode.MARKET_NOT_ACTIVE) | ||
| } | ||
|
|
||
| fun findActiveSymbol(symbolId: Long): Symbol { | ||
| val symbol = symbolRepository.findById(symbolId).orElseThrow { ExpectedException(MarketErrorCode.SYMBOL_NOT_FOUND) } | ||
| requireActiveMarket(symbol.market) | ||
| if (!symbol.isActive) throw ExpectedException(MarketErrorCode.SYMBOL_NOT_ACTIVE) | ||
| return symbol | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/team/cklob/arena/domain/market/application/SearchSymbolsService.kt
This file contains hidden or 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 @@ | ||
| package team.cklob.arena.domain.market.application | ||
|
|
||
| import team.cklob.arena.domain.market.application.result.SymbolPageResult | ||
| import team.cklob.arena.domain.market.domain.type.MarketType | ||
|
|
||
| interface SearchSymbolsService { | ||
| fun execute( | ||
| market: MarketType, | ||
| keyword: String, | ||
| page: Int, | ||
| size: Int, | ||
| ): SymbolPageResult | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/team/cklob/arena/domain/market/application/SyncSymbolsService.kt
This file contains hidden or 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 @@ | ||
| package team.cklob.arena.domain.market.application | ||
|
|
||
| interface SyncSymbolsService { | ||
| fun execute() | ||
| } |
25 changes: 25 additions & 0 deletions
25
...main/kotlin/team/cklob/arena/domain/market/application/impl/GetCurrentPriceServiceImpl.kt
This file contains hidden or 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,25 @@ | ||
| package team.cklob.arena.domain.market.application.impl | ||
|
|
||
| import org.springframework.stereotype.Service | ||
| import team.cklob.arena.domain.market.application.GetCurrentPriceService | ||
| import team.cklob.arena.domain.market.application.MarketDataCache | ||
| import team.cklob.arena.domain.market.application.MarketDataClient | ||
| import team.cklob.arena.domain.market.application.MarketValidator | ||
| import team.cklob.arena.domain.market.application.result.CurrentPriceResult | ||
|
|
||
| @Service | ||
| class GetCurrentPriceServiceImpl( | ||
| private val marketValidator: MarketValidator, | ||
| private val marketDataCache: MarketDataCache, | ||
| private val marketDataClient: MarketDataClient, | ||
| ) : GetCurrentPriceService { | ||
| override fun execute(symbolId: Long): CurrentPriceResult { | ||
| val symbol = marketValidator.findActiveSymbol(symbolId) | ||
|
|
||
| return marketDataCache.findCurrentPrice(symbolId) ?: marketDataClient.fetchCurrentPrice(symbol.code).let { | ||
| CurrentPriceResult(symbolId, it.price, it.snapshotAt).also { result -> | ||
| marketDataCache.saveCurrentPrice(symbol.market, result) | ||
| } | ||
| } | ||
| } | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
...main/kotlin/team/cklob/arena/domain/market/application/impl/GetPriceHistoryServiceImpl.kt
This file contains hidden or 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,41 @@ | ||
| package team.cklob.arena.domain.market.application.impl | ||
|
|
||
| import org.springframework.stereotype.Service | ||
| import team.cklob.arena.domain.market.MarketErrorCode | ||
| import team.cklob.arena.domain.market.application.GetPriceHistoryService | ||
| import team.cklob.arena.domain.market.application.MarketDataCache | ||
| import team.cklob.arena.domain.market.application.MarketDataClient | ||
| import team.cklob.arena.domain.market.application.MarketValidator | ||
| import team.cklob.arena.domain.market.application.result.PriceHistoryResult | ||
| import team.cklob.arena.domain.market.infrastructure.property.MarketProperties | ||
| import team.cklob.arena.global.exception.ExpectedException | ||
| import java.time.LocalDate | ||
| import java.time.temporal.ChronoUnit | ||
|
|
||
| @Service | ||
| class GetPriceHistoryServiceImpl( | ||
| private val marketValidator: MarketValidator, | ||
| private val marketDataCache: MarketDataCache, | ||
| private val marketDataClient: MarketDataClient, | ||
| private val marketProperties: MarketProperties, | ||
| ) : GetPriceHistoryService { | ||
| override fun execute( | ||
| symbolId: Long, | ||
| from: LocalDate, | ||
| to: LocalDate, | ||
| ): PriceHistoryResult { | ||
| val days = ChronoUnit.DAYS.between(from, to) + 1 | ||
| if (days !in 1..marketProperties.historyMaxDays) { | ||
| throw ExpectedException(MarketErrorCode.INVALID_PRICE_HISTORY_RANGE) | ||
| } | ||
| val symbol = marketValidator.findActiveSymbol(symbolId) | ||
|
|
||
| return marketDataCache.findPriceHistory(symbolId, from.toString(), to.toString()) | ||
| ?: PriceHistoryResult( | ||
| symbolId = symbolId, | ||
| from = from.toString(), | ||
| to = to.toString(), | ||
| prices = marketDataClient.fetchPriceHistory(symbol.code, from, to).sortedBy { it.snapshotAt }, | ||
|
exijn marked this conversation as resolved.
|
||
| ).also(marketDataCache::savePriceHistory) | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/team/cklob/arena/domain/market/application/impl/GetSymbolsServiceImpl.kt
This file contains hidden or 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,28 @@ | ||
| package team.cklob.arena.domain.market.application.impl | ||
|
|
||
| import org.springframework.data.domain.PageRequest | ||
| import org.springframework.data.domain.Sort | ||
| import org.springframework.stereotype.Service | ||
| import org.springframework.transaction.annotation.Transactional | ||
| import team.cklob.arena.domain.market.application.GetSymbolsService | ||
| import team.cklob.arena.domain.market.application.MarketValidator | ||
| import team.cklob.arena.domain.market.application.result.SymbolPageResult | ||
| import team.cklob.arena.domain.market.domain.repository.SymbolRepository | ||
| import team.cklob.arena.domain.market.domain.type.MarketType | ||
|
|
||
| @Service | ||
| class GetSymbolsServiceImpl( | ||
| private val symbolRepository: SymbolRepository, | ||
| private val marketValidator: MarketValidator, | ||
| ) : GetSymbolsService { | ||
| @Transactional(readOnly = true) | ||
| override fun execute( | ||
| market: MarketType, | ||
| page: Int, | ||
| size: Int, | ||
| ): SymbolPageResult { | ||
| marketValidator.requireActiveMarket(market) | ||
| val pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.ASC, "code")) | ||
| return SymbolPageResult.from(symbolRepository.findAllByMarketAndIsActiveTrue(market, pageable)) | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/team/cklob/arena/domain/market/application/impl/SearchSymbolsServiceImpl.kt
This file contains hidden or 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,39 @@ | ||
| package team.cklob.arena.domain.market.application.impl | ||
|
|
||
| import org.springframework.data.domain.PageRequest | ||
| import org.springframework.data.domain.Sort | ||
| import org.springframework.stereotype.Service | ||
| import org.springframework.transaction.annotation.Transactional | ||
| import team.cklob.arena.domain.market.application.MarketValidator | ||
| import team.cklob.arena.domain.market.application.SearchSymbolsService | ||
| import team.cklob.arena.domain.market.application.result.SymbolPageResult | ||
| import team.cklob.arena.domain.market.domain.repository.SymbolRepository | ||
| import team.cklob.arena.domain.market.domain.type.MarketType | ||
| import team.cklob.arena.global.exception.CommonErrorCode | ||
| import team.cklob.arena.global.exception.ExpectedException | ||
|
|
||
| @Service | ||
| class SearchSymbolsServiceImpl( | ||
| private val symbolRepository: SymbolRepository, | ||
| private val marketValidator: MarketValidator, | ||
| ) : SearchSymbolsService { | ||
| @Transactional(readOnly = true) | ||
| override fun execute( | ||
| market: MarketType, | ||
| keyword: String, | ||
| page: Int, | ||
| size: Int, | ||
| ): SymbolPageResult { | ||
| marketValidator.requireActiveMarket(market) | ||
| val normalizedKeyword = | ||
| keyword.trim().takeIf { it.isNotEmpty() && it.length <= 100 } | ||
| ?: throw ExpectedException(CommonErrorCode.INVALID_REQUEST) | ||
| val pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.ASC, "code")) | ||
| return SymbolPageResult.from(symbolRepository.searchActive(market, normalizedKeyword.escapeLikePattern(), pageable)) | ||
| } | ||
|
|
||
| private fun String.escapeLikePattern(): String = | ||
| replace("\\", "\\\\") | ||
| .replace("%", "\\%") | ||
| .replace("_", "\\_") | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.