-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] 공휴일 API를 받아와서 공휴일 점심에 스낵코너가 안뜨게 처리해요 #501
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 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
159a7fe
[Feat] 공휴일 API를 연동하고 메뉴 화면에 공휴일 배너를 표시합니다
HI-JIN2 c1b4129
[Refactor] 메뉴 로딩(공휴일 포함) 로직을 UseCase로 이동합니다
HI-JIN2 5aab37d
[Chore] 메뉴 공휴일 배너 노출 제거
HI-JIN2 4e51d3d
Merge remote-tracking branch 'origin/develop' into feat/public-holiday
HI-JIN2 e6d7bcf
test: update MenuViewModelBehaviorSpec for LoadMenusUseCase
HI-JIN2 6bb8a2e
[Refactor] 공휴일 캐시 제한 및 repository API 정리
HI-JIN2 874c580
chore: 공휴일 API 관련 주석 추가
HI-JIN2 cfea83a
Merge branch 'develop' into feat/public-holiday
HI-JIN2 7ba9941
refactor: simplify public holiday fetch
HI-JIN2 548eeee
chore: early return으로 수정
HI-JIN2 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
64 changes: 64 additions & 0 deletions
64
app/src/main/java/com/eatssu/android/data/remote/dto/response/PublicHolidayApiResponse.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,64 @@ | ||
| package com.eatssu.android.data.remote.dto.response | ||
|
|
||
| import kotlinx.serialization.ExperimentalSerializationApi | ||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.builtins.ListSerializer | ||
| import kotlinx.serialization.json.JsonArray | ||
| import kotlinx.serialization.json.JsonElement | ||
| import kotlinx.serialization.json.JsonObject | ||
| import kotlinx.serialization.json.JsonTransformingSerializer | ||
|
|
||
| @Serializable | ||
| data class PublicHolidayApiResponse( | ||
| @SerialName("response") val response: Response? = null, | ||
| ) { | ||
| @Serializable | ||
| data class Response( | ||
| @SerialName("header") val header: Header? = null, | ||
| @SerialName("body") val body: Body? = null, | ||
| ) | ||
|
|
||
| @Serializable | ||
| data class Header( | ||
| @SerialName("resultCode") val resultCode: String? = null, | ||
| @SerialName("resultMsg") val resultMsg: String? = null, | ||
| ) | ||
|
|
||
| @Serializable | ||
| data class Body( | ||
| @SerialName("items") val items: Items? = null, | ||
| @SerialName("numOfRows") val numOfRows: Int? = null, | ||
| @SerialName("pageNo") val pageNo: Int? = null, | ||
| @SerialName("totalCount") val totalCount: Int? = null, | ||
| ) | ||
|
|
||
| @Serializable | ||
| data class Items( | ||
| @Serializable(with = PublicHolidayItemListSerializer::class) | ||
| @SerialName("item") val item: List<Item> = emptyList(), | ||
| ) | ||
|
|
||
| @OptIn(ExperimentalSerializationApi::class) | ||
| /** | ||
| * 공휴일 API는 item이 1개일 때는 Object, 여러 개일 때는 Array로 내려주는 케이스가 있어 | ||
| * 역직렬화 시 항상 List 형태로 정규화한다. | ||
| */ | ||
| object PublicHolidayItemListSerializer : JsonTransformingSerializer<List<Item>>( | ||
| ListSerializer(Item.serializer()) | ||
| ) { | ||
| override fun transformDeserialize(element: JsonElement): JsonElement { | ||
| return when (element) { | ||
| is JsonObject -> JsonArray(listOf(element)) | ||
| else -> element | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Serializable | ||
| data class Item( | ||
| @SerialName("locdate") val locdate: Long? = null, | ||
| @SerialName("isHoliday") val isHoliday: String? = null, | ||
| @SerialName("dateName") val dateName: String? = null, | ||
| ) | ||
| } |
106 changes: 106 additions & 0 deletions
106
app/src/main/java/com/eatssu/android/data/remote/repository/PublicHolidayRepositoryImpl.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,106 @@ | ||
| package com.eatssu.android.data.remote.repository | ||
|
|
||
| import com.eatssu.android.data.remote.service.PublicHolidayService | ||
| import com.eatssu.android.domain.model.PublicHoliday | ||
| import com.eatssu.android.domain.repository.PublicHolidayRepository | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import timber.log.Timber | ||
| import java.net.URLEncoder | ||
| import java.time.LocalDate | ||
| import java.time.YearMonth | ||
| import javax.inject.Named | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class PublicHolidayRepositoryImpl @Inject constructor( | ||
| private val publicHolidayService: PublicHolidayService, | ||
| @Named(PUBLIC_HOLIDAY_SERVICE_KEY_NAME) private val serviceKey: String, | ||
| ) : PublicHolidayRepository { | ||
|
|
||
| companion object { | ||
| const val PUBLIC_HOLIDAY_SERVICE_KEY_NAME: String = "PublicHolidayServiceKey" | ||
| } | ||
|
|
||
| /** | ||
| * 외부 공휴일 API를 호출해 해당 [YearMonth]의 공휴일 목록을 조회한다. | ||
| * | ||
| * | ||
| * - `HOLIDAY_API_KEY`가 비어있으면 네트워크 호출 없이 빈 리스트를 반환한다. | ||
| * - 네트워크/파싱 실패 또는 비정상 resultCode인 경우 빈 리스트를 반환한다. | ||
| * - `isHoliday == "Y"`만 필터링하고, 날짜 기준으로 중복 제거 후 오름차순 정렬한다. | ||
| */ | ||
| override suspend fun getHolidays(yearMonth: YearMonth): List<PublicHoliday> { | ||
| if (serviceKey.isBlank()) { | ||
| Timber.w("HOLIDAY_API_KEY is blank; skipping public holiday fetch") | ||
| return emptyList() | ||
| } | ||
|
|
||
| val normalizedKey = normalizeServiceKey(serviceKey) | ||
|
|
||
| val responseResult = withContext(Dispatchers.IO) { | ||
| runCatching { | ||
| publicHolidayService.getRestDeInfo( | ||
| serviceKey = normalizedKey, | ||
| solYear = yearMonth.year.toString(), | ||
| solMonth = yearMonth.monthValue.toString().padStart(2, '0'), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| val response = responseResult.getOrNull() ?: run { | ||
| Timber.w(responseResult.exceptionOrNull(), "Failed to fetch public holidays") | ||
| return emptyList() | ||
| } | ||
|
|
||
| val resultCode = response.response?.header?.resultCode | ||
| if (resultCode != null && resultCode != "00") { | ||
| Timber.w( | ||
| "PublicHoliday API returned non-normal resultCode=%s msg=%s", | ||
| resultCode, | ||
| response.response?.header?.resultMsg, | ||
| ) | ||
| return emptyList() | ||
| } | ||
|
|
||
| val holidays = response.response | ||
| ?.body | ||
| ?.items | ||
| ?.item | ||
| .orEmpty() | ||
| .asSequence() | ||
| .filter { it.isHoliday.equals("Y", ignoreCase = true) } | ||
| .mapNotNull { item -> | ||
| val date = item.locdate?.let(::parseLocdate) | ||
| val name = item.dateName?.trim().orEmpty() | ||
|
|
||
| if (date == null || name.isBlank()) return@mapNotNull null | ||
| PublicHoliday(date = date, name = name) | ||
| } | ||
| .distinctBy { it.date } | ||
| .sortedBy { it.date } | ||
| .toList() | ||
|
|
||
| return holidays | ||
| } | ||
|
|
||
| private fun parseLocdate(locdate: Long): LocalDate? { | ||
| val s = locdate.toString() | ||
| if (s.length != 8) return null | ||
|
|
||
| return runCatching { | ||
| val year = s.substring(0, 4).toInt() | ||
| val month = s.substring(4, 6).toInt() | ||
| val day = s.substring(6, 8).toInt() | ||
| LocalDate.of(year, month, day) | ||
| }.getOrNull() | ||
| } | ||
|
|
||
| private fun normalizeServiceKey(value: String): String { | ||
| val trimmed = value.trim() | ||
| if (trimmed.isEmpty()) return "" | ||
|
|
||
| return if ('%' in trimmed) trimmed else URLEncoder.encode(trimmed, Charsets.UTF_8.name()) | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/eatssu/android/data/remote/service/PublicHolidayService.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,27 @@ | ||
| package com.eatssu.android.data.remote.service | ||
|
|
||
| import com.eatssu.android.data.remote.dto.response.PublicHolidayApiResponse | ||
| import retrofit2.http.GET | ||
| import retrofit2.http.Query | ||
|
|
||
| interface PublicHolidayService { | ||
|
|
||
| /** | ||
| * data.go.kr 공휴일(OpenAPI) 호출용 API. | ||
| * | ||
| * - 엔드포인트: SpcdeInfoService/getRestDeInfo | ||
| * - ServiceKey는 이미 URL 인코딩된 값으로 전달한다(`encoded = true`). | ||
| * - solYear/solMonth는 양력 기준 연/월이다. | ||
| * - `_type=json`으로 JSON 응답을 받는다. | ||
| */ | ||
|
|
||
| @GET("B090041/openapi/service/SpcdeInfoService/getRestDeInfo") | ||
| suspend fun getRestDeInfo( | ||
| @Query(value = "ServiceKey", encoded = true) serviceKey: String, | ||
| @Query("solYear") solYear: String, | ||
| @Query("solMonth") solMonth: String, | ||
| @Query("numOfRows") numOfRows: Int = 50, | ||
| @Query("pageNo") pageNo: Int = 1, | ||
| @Query("_type") type: String = "json", | ||
| ): PublicHolidayApiResponse | ||
| } |
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
64 changes: 64 additions & 0 deletions
64
app/src/main/java/com/eatssu/android/di/PublicHolidayModule.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,64 @@ | ||
| package com.eatssu.android.di | ||
|
|
||
| import com.eatssu.android.BuildConfig | ||
| import com.eatssu.android.data.remote.repository.PublicHolidayRepositoryImpl | ||
| import com.eatssu.android.data.remote.service.PublicHolidayService | ||
| import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import kotlinx.serialization.json.Json | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import okhttp3.OkHttpClient | ||
| import retrofit2.Retrofit | ||
| import javax.inject.Named | ||
| import javax.inject.Qualifier | ||
| import javax.inject.Singleton | ||
|
|
||
| @Qualifier | ||
| @Retention(AnnotationRetention.BINARY) | ||
| /** 공휴일 API 전용 Retrofit 구분자. */ | ||
| annotation class PublicHolidayApi | ||
|
|
||
| /** | ||
| * 공휴일 API 전용 Retrofit/Service 제공 모듈. | ||
| * | ||
| * - 인증 토큰이 필요 없는 외부 API이므로 `@NoToken` OkHttpClient를 사용한다. | ||
| * - 키는 `BuildConfig.HOLIDAY_API_KEY`로 주입되며, 비어있을 수 있다(로컬 환경 등). | ||
| */ | ||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object PublicHolidayModule { | ||
|
|
||
| private const val PUBLIC_HOLIDAY_BASE_URL = "https://apis.data.go.kr/" | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| @Named(PublicHolidayRepositoryImpl.PUBLIC_HOLIDAY_SERVICE_KEY_NAME) | ||
| fun providePublicHolidayServiceKey(): String { | ||
| return BuildConfig.HOLIDAY_API_KEY | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| @PublicHolidayApi | ||
| fun providePublicHolidayRetrofit( | ||
| @NoToken okHttpClient: OkHttpClient, | ||
| json: Json, | ||
| ): Retrofit { | ||
| return Retrofit.Builder() | ||
| .baseUrl(PUBLIC_HOLIDAY_BASE_URL) | ||
| .client(okHttpClient) | ||
| .addConverterFactory(json.asConverterFactory("application/json".toMediaType())) | ||
| .build() | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun providePublicHolidayService( | ||
| @PublicHolidayApi retrofit: Retrofit, | ||
| ): PublicHolidayService { | ||
| return retrofit.create(PublicHolidayService::class.java) | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/eatssu/android/domain/model/MenuLoadResult.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 com.eatssu.android.domain.model | ||
|
|
||
| import com.eatssu.common.enums.Restaurant | ||
|
|
||
| data class MenuLoadResult( | ||
| val menuMap: Map<Restaurant, List<Menu>>, | ||
| ) |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/eatssu/android/domain/model/PublicHoliday.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,8 @@ | ||
| package com.eatssu.android.domain.model | ||
|
|
||
| import java.time.LocalDate | ||
|
|
||
| data class PublicHoliday( | ||
| val date: LocalDate, | ||
| val name: String, | ||
| ) |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/eatssu/android/domain/repository/PublicHolidayRepository.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,9 @@ | ||
| package com.eatssu.android.domain.repository | ||
|
|
||
| import com.eatssu.android.domain.model.PublicHoliday | ||
| import java.time.YearMonth | ||
|
|
||
| interface PublicHolidayRepository { | ||
|
|
||
| suspend fun getHolidays(yearMonth: YearMonth): List<PublicHoliday> | ||
| } |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/eatssu/android/domain/usecase/holiday/GetPublicHolidayOfDateUseCase.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 com.eatssu.android.domain.usecase.holiday | ||
|
|
||
| import com.eatssu.android.domain.model.PublicHoliday | ||
| import java.time.LocalDate | ||
| import java.time.YearMonth | ||
| import javax.inject.Inject | ||
|
|
||
| /** | ||
| * 특정 날짜가 공휴일이면 해당 공휴일 정보를 반환한다. | ||
| */ | ||
| class GetPublicHolidayOfDateUseCase @Inject constructor( | ||
| private val getPublicHolidaysOfMonthUseCase: GetPublicHolidaysOfMonthUseCase, | ||
| ) { | ||
| suspend operator fun invoke(date: LocalDate): PublicHoliday? { | ||
| val holidays = getPublicHolidaysOfMonthUseCase(YearMonth.from(date)) | ||
| return holidays.firstOrNull { it.date == date } | ||
| } | ||
| } |
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.