Skip to content

Commit b2a77cf

Browse files
committed
feat: PlaceRepository에 api 호출 메서드를 구현
1 parent 505ac4e commit b2a77cf

5 files changed

Lines changed: 156 additions & 3 deletions

File tree

data/place/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ android {
1818
dependencies {
1919
implementation(projects.core.util)
2020
implementation(projects.data.domain)
21+
implementation(projects.data.remote)
2122
implementation(projects.domain.place)
2223

2324
testImplementation(libs.kotlinx.coroutines.test)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.ku_stacks.ku_ring.place.mapper
2+
3+
import com.ku_stacks.ku_ring.domain.Place
4+
import com.ku_stacks.ku_ring.domain.PlaceCategory
5+
import com.ku_stacks.ku_ring.domain.PlaceFacility
6+
import com.ku_stacks.ku_ring.domain.PlaceOperationHours
7+
import com.ku_stacks.ku_ring.remote.place.response.PlaceBuildingDetailResponse
8+
import com.ku_stacks.ku_ring.remote.place.response.PlaceBuildingResponse
9+
import com.ku_stacks.ku_ring.remote.place.response.PlaceCampusPlaceResponse
10+
import com.ku_stacks.ku_ring.remote.place.response.PlaceCategoryResponse
11+
import com.ku_stacks.ku_ring.remote.place.response.PlaceOperatingHoursResponse
12+
13+
internal fun PlaceBuildingResponse.toDomain() = Place(
14+
id = id.toString(),
15+
name = name,
16+
category = "",
17+
address = address,
18+
latitude = latitude,
19+
longitude = longitude,
20+
priority = Place.Priority.MIDDLE,
21+
)
22+
23+
internal fun PlaceBuildingDetailResponse.toDomain() = Place(
24+
id = id.toString(),
25+
name = name,
26+
category = "",
27+
address = address,
28+
latitude = latitude,
29+
longitude = longitude,
30+
priority = Place.Priority.MIDDLE,
31+
imageUrl = imageUrl,
32+
operationHours = currentOperatingHours?.toDomain(),
33+
facilities = campusPlaces.map { it.toDomain() },
34+
)
35+
36+
internal fun PlaceCampusPlaceResponse.toDomain() = PlaceFacility(
37+
name = name,
38+
category = categoryKorName,
39+
location = listOfNotNull(floor, locationDetail).joinToString(" ").ifBlank { null },
40+
operationHours = currentOperatingHours?.toDomain(),
41+
id = id,
42+
imageUrl = imageUrl,
43+
quantity = quantity,
44+
externalUrl = externalUrl,
45+
)
46+
47+
internal fun PlaceOperatingHoursResponse.toDomain() = PlaceOperationHours(
48+
current = toDisplayText(),
49+
)
50+
51+
private fun PlaceOperatingHoursResponse.toDisplayText(): String = when (status) {
52+
"OPEN_24_HOURS" -> "24시간 운영"
53+
"CLOSED" -> "휴무"
54+
else -> if (opensAt != null && closesAt != null) "$opensAt - $closesAt" else "-"
55+
}
56+
57+
internal fun PlaceCategoryResponse.toDomain() = PlaceCategory(
58+
name = name,
59+
korName = korName,
60+
displayOrder = displayOrder,
61+
)
Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,63 @@
11
package com.ku_stacks.ku_ring.place.repository
22

33
import com.ku_stacks.ku_ring.domain.Place
4+
import com.ku_stacks.ku_ring.domain.PlaceCategory
5+
import com.ku_stacks.ku_ring.domain.PlaceFacility
46
import com.ku_stacks.ku_ring.domain.place.repository.PlaceRepository
57
import com.ku_stacks.ku_ring.place.datasource.PlaceDataSource
68
import com.ku_stacks.ku_ring.place.mapper.toDomain
9+
import com.ku_stacks.ku_ring.remote.place.PlaceClient
10+
import com.ku_stacks.ku_ring.util.suspendRunCatching
711
import javax.inject.Inject
812

913
class PlaceRepositoryImpl @Inject constructor(
1014
private val placeDataSource: PlaceDataSource,
11-
): PlaceRepository {
15+
private val placeClient: PlaceClient,
16+
) : PlaceRepository {
1217
override suspend fun getPlaces(): List<Place> {
1318
return placeDataSource.getJsonPlaces().map { it.toDomain() }
1419
}
20+
21+
override suspend fun getPlaceBuildingDetail(buildingId: Long): Result<Place> =
22+
suspendRunCatching {
23+
val response = placeClient.fetchBuildingDetail(buildingId)
24+
when {
25+
response.isSuccessAndDataExists -> response.data!!.toDomain()
26+
else -> throw IllegalStateException(response.resultMsg)
27+
}
28+
}
29+
30+
override suspend fun searchPlaceBuildings(keyword: String): Result<List<Place>> =
31+
suspendRunCatching {
32+
val response = placeClient.searchBuildings(keyword)
33+
when {
34+
response.isSuccessAndDataExists -> response.data!!.buildings.map { it.toDomain() }
35+
else -> throw IllegalStateException(response.resultMsg)
36+
}
37+
}
38+
39+
override suspend fun getPlaceBuildings(): Result<List<Place>> = suspendRunCatching {
40+
val response = placeClient.fetchBuildings()
41+
when {
42+
response.isSuccessAndDataExists -> response.data!!.buildings.map { it.toDomain() }
43+
else -> throw IllegalStateException(response.resultMsg)
44+
}
45+
}
46+
47+
override suspend fun getPlaceCampusPlaces(category: String): Result<List<PlaceFacility>> =
48+
suspendRunCatching {
49+
val response = placeClient.fetchCampusPlaces(category)
50+
when {
51+
response.isSuccessAndDataExists -> response.data!!.campusPlaces.map { it.toDomain() }
52+
else -> throw IllegalStateException(response.resultMsg)
53+
}
54+
}
55+
56+
override suspend fun getPlaceCategories(): Result<List<PlaceCategory>> = suspendRunCatching {
57+
val response = placeClient.fetchCategories()
58+
when {
59+
response.isSuccessAndDataExists -> response.data!!.categories.map { it.toDomain() }
60+
else -> throw IllegalStateException(response.resultMsg)
61+
}
62+
}
1563
}

data/place/src/test/java/com/ku_stacks/ku_ring/place/PlaceRepositoryTest.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.ku_stacks.ku_ring.domain.Place
66
import com.ku_stacks.ku_ring.domain.place.repository.PlaceRepository
77
import com.ku_stacks.ku_ring.place.datasource.PlaceDataSource
88
import com.ku_stacks.ku_ring.place.repository.PlaceRepositoryImpl
9+
import com.ku_stacks.ku_ring.remote.place.PlaceClient
910
import kotlinx.coroutines.test.StandardTestDispatcher
1011
import kotlinx.coroutines.test.TestCoroutineScheduler
1112
import kotlinx.coroutines.test.TestDispatcher
@@ -14,13 +15,15 @@ import org.junit.Assert.assertTrue
1415
import org.junit.Before
1516
import org.junit.Test
1617
import org.junit.runner.RunWith
18+
import org.mockito.Mockito
1719
import org.robolectric.RobolectricTestRunner
1820

1921
@RunWith(RobolectricTestRunner::class)
2022
class PlaceRepositoryTest {
2123
private lateinit var dataSource: PlaceDataSource
2224
private lateinit var repository: PlaceRepository
2325
private lateinit var testDispatcher: TestDispatcher
26+
private val placeClient = Mockito.mock(PlaceClient::class.java)
2427

2528
@Before
2629
fun setUp() {
@@ -29,7 +32,7 @@ class PlaceRepositoryTest {
2932
testDispatcher = StandardTestDispatcher(testScheduler)
3033

3134
dataSource = PlaceDataSource(context, testDispatcher)
32-
repository = PlaceRepositoryImpl(dataSource)
35+
repository = PlaceRepositoryImpl(dataSource, placeClient)
3336
}
3437

3538
@Test
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
11
package com.ku_stacks.ku_ring.domain.place.repository
22

33
import com.ku_stacks.ku_ring.domain.Place
4+
import com.ku_stacks.ku_ring.domain.PlaceCategory
5+
import com.ku_stacks.ku_ring.domain.PlaceFacility
46

57
interface PlaceRepository {
68
suspend fun getPlaces(): List<Place>
7-
}
9+
10+
/**
11+
* 캠퍼스맵 건물의 상세 정보를 가져온다.
12+
*
13+
* @param buildingId 건물 ID
14+
* @return 정상 처리 시 건물 상세 정보를 [Result]에 담아 반환. 실패 시 [Result.Failure]
15+
*/
16+
suspend fun getPlaceBuildingDetail(buildingId: Long): Result<Place>
17+
18+
/**
19+
* 키워드로 캠퍼스맵 건물을 검색한다.
20+
*
21+
* @param keyword 검색 키워드
22+
* @return 정상 처리 시 검색된 건물 목록을 [Result]에 담아 반환. 실패 시 [Result.Failure]
23+
*/
24+
suspend fun searchPlaceBuildings(keyword: String): Result<List<Place>>
25+
26+
/**
27+
* 캠퍼스맵 전체 건물 목록을 가져온다.
28+
*
29+
* @return 정상 처리 시 건물 목록을 [Result]에 담아 반환. 실패 시 [Result.Failure]
30+
*/
31+
suspend fun getPlaceBuildings(): Result<List<Place>>
32+
33+
/**
34+
* 카테고리에 해당하는 캠퍼스맵 시설 목록을 가져온다.
35+
*
36+
* @param category 시설 카테고리
37+
* @return 정상 처리 시 시설 목록을 [Result]에 담아 반환. 실패 시 [Result.Failure]
38+
*/
39+
suspend fun getPlaceCampusPlaces(category: String): Result<List<PlaceFacility>>
40+
41+
/**
42+
* 캠퍼스맵 카테고리 목록을 가져온다.
43+
*
44+
* @return 정상 처리 시 카테고리 목록을 [Result]에 담아 반환. 실패 시 [Result.Failure]
45+
*/
46+
suspend fun getPlaceCategories(): Result<List<PlaceCategory>>
47+
}

0 commit comments

Comments
 (0)