-
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.
Merge pull request #90 from everymeals/feature/get_restaurant
[feature/get_restaurant] 대학 주변 식당 API 연결 및 코드 리팩터링
- Loading branch information
Showing
21 changed files
with
651 additions
and
138 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
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
14 changes: 14 additions & 0 deletions
14
data/src/main/java/com/everymeal/data/datasource/restaurant/RestaurantDataSource.kt
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,14 @@ | ||
package com.everymeal.data.datasource.restaurant | ||
|
||
import androidx.paging.PagingData | ||
import com.everymeal.data.model.restaruant.RestaurantResponse | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface RestaurantDataSource { | ||
suspend fun getUnivRestaurant( | ||
campusIdx: Int, | ||
order: String, | ||
group: String? = null, | ||
grade: String? = null, | ||
): Flow<PagingData<RestaurantResponse>> | ||
} |
33 changes: 33 additions & 0 deletions
33
data/src/main/java/com/everymeal/data/datasource/restaurant/RestaurantDataSourceImpl.kt
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,33 @@ | ||
package com.everymeal.data.datasource.restaurant | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import com.everymeal.data.model.restaruant.RestaurantResponse | ||
import com.everymeal.data.service.restaurant.RestaurantApi | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
const val PAGING_SIZE = 20 | ||
const val STARTING_PAGE_INDEX = 0 | ||
|
||
class RestaurantDataSourceImpl @Inject constructor( | ||
private val restaurantApi: RestaurantApi | ||
) : RestaurantDataSource { | ||
override suspend fun getUnivRestaurant( | ||
campusIdx: Int, | ||
order: String, | ||
group: String?, | ||
grade: String? | ||
) : Flow<PagingData<RestaurantResponse>> { | ||
val pagingSourceFactory = { RestaurantPagingSource(restaurantApi, campusIdx, order, group, grade) } | ||
return Pager( | ||
config = PagingConfig( | ||
initialLoadSize = PAGING_SIZE, | ||
pageSize = PAGING_SIZE, | ||
enablePlaceholders = false, | ||
), | ||
pagingSourceFactory = pagingSourceFactory | ||
).flow | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
data/src/main/java/com/everymeal/data/datasource/restaurant/RestaurantPagingSource.kt
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,50 @@ | ||
package com.everymeal.data.datasource.restaurant | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.everymeal.data.model.restaruant.RestaurantResponse | ||
import com.everymeal.data.service.restaurant.RestaurantApi | ||
import retrofit2.HttpException | ||
import java.io.IOException | ||
|
||
class RestaurantPagingSource ( | ||
private val restaurantApi: RestaurantApi, | ||
private val campusIdx: Int, | ||
private val order: String, | ||
private val group: String?, | ||
private val grade: String? | ||
) : PagingSource<Int, RestaurantResponse>() { | ||
|
||
override fun getRefreshKey(state: PagingState<Int, RestaurantResponse>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1) | ||
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1) | ||
} | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, RestaurantResponse> { | ||
val position = params.key ?: STARTING_PAGE_INDEX | ||
|
||
return try { | ||
val response = restaurantApi.getUnivRestaurant( | ||
campusIdx = campusIdx, | ||
order = order, | ||
group = group, | ||
grade = grade, | ||
offset = position, | ||
limit = PAGING_SIZE | ||
) | ||
val restaurants = response.data.content | ||
LoadResult.Page( | ||
data = restaurants, | ||
prevKey = if (position == STARTING_PAGE_INDEX) null else position - 1, | ||
nextKey = if (restaurants.isEmpty()) null else position + 1 | ||
) | ||
} catch (exception: IOException) { | ||
LoadResult.Error(exception) | ||
} catch (exception: HttpException) { | ||
LoadResult.Error(exception) | ||
} | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
data/src/main/java/com/everymeal/data/model/restaruant/GetUnivRestaurantResponse.kt
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,67 @@ | ||
package com.everymeal.data.model.restaruant | ||
|
||
import com.everymeal.domain.model.restaurant.RestaurantDataEntity | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class GetUnivRestaurantResponse( | ||
val content: List<RestaurantResponse>, | ||
val pageable: Pageable, | ||
val totalPages: Int, | ||
val totalElements: Int, | ||
val last: Boolean, | ||
val size: Int, | ||
val number: Int, | ||
val sort: Sort, | ||
val numberOfElements: Int, | ||
val first: Boolean, | ||
val empty: Boolean | ||
) | ||
|
||
@Serializable | ||
data class RestaurantResponse( | ||
val idx: Int, | ||
val name: String, | ||
val address: String, | ||
val phoneNumber: String, | ||
val categoryDetail: String, | ||
val distance: Int, | ||
val grade: Float, | ||
val reviewCount: Int, | ||
val recommendedCount: Int, | ||
val images: List<String>?, | ||
val isLiked: Boolean | ||
) | ||
|
||
@Serializable | ||
data class Pageable( | ||
val sort: Sort, | ||
val offset: Int, | ||
val pageNumber: Int, | ||
val pageSize: Int, | ||
val paged: Boolean, | ||
val unpaged: Boolean | ||
) | ||
|
||
@Serializable | ||
data class Sort( | ||
val empty: Boolean, | ||
val sorted: Boolean, | ||
val unsorted: Boolean | ||
) | ||
|
||
fun RestaurantResponse.toEntity(): RestaurantDataEntity { | ||
return RestaurantDataEntity( | ||
idx = this.idx, | ||
name = this.name, | ||
address = this.address, | ||
phoneNumber = this.phoneNumber, | ||
categoryDetail = this.categoryDetail, | ||
distance = this.distance, | ||
grade = this.grade, | ||
reviewCount = this.reviewCount, | ||
recommendedCount = this.recommendedCount, | ||
images = this.images, | ||
isLiked = this.isLiked | ||
) | ||
} |
27 changes: 27 additions & 0 deletions
27
data/src/main/java/com/everymeal/data/repository/restaurant/RestaurantRepositoryImpl.kt
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,27 @@ | ||
package com.everymeal.data.repository.restaurant | ||
|
||
import androidx.paging.PagingData | ||
import com.everymeal.data.datasource.restaurant.RestaurantDataSource | ||
import com.everymeal.domain.model.restaurant.RestaurantDataEntity | ||
import com.everymeal.domain.repository.restaurant.RestaurantRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import androidx.paging.map | ||
import com.everymeal.data.model.restaruant.toEntity | ||
import javax.inject.Inject | ||
|
||
class RestaurantRepositoryImpl @Inject constructor( | ||
private val restaurantDataSource: RestaurantDataSource | ||
) : RestaurantRepository { | ||
override suspend fun getUnivRestaurant( | ||
campusIdx: Int, | ||
order: String, | ||
group: String?, | ||
grade: String? | ||
) : Flow<PagingData<RestaurantDataEntity>> { | ||
return restaurantDataSource.getUnivRestaurant(campusIdx, order, group, grade) | ||
.map { pagingData -> | ||
pagingData.map { it.toEntity() } | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
data/src/main/java/com/everymeal/data/service/restaurant/RestaurantApi.kt
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,20 @@ | ||
package com.everymeal.data.service.restaurant | ||
|
||
import com.everymeal.data.model.BaseResponse | ||
import com.everymeal.data.model.restaruant.GetUnivRestaurantResponse | ||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
import retrofit2.http.Query | ||
|
||
interface RestaurantApi { | ||
|
||
@GET("/api/v1/stores/campus/{campusIdx}") | ||
suspend fun getUnivRestaurant( | ||
@Path("campusIdx") campusIdx: Int, | ||
@Query("offset") offset: Int, | ||
@Query("limit") limit: Int, | ||
@Query("order") order: String, | ||
@Query("group") group: String? = null, | ||
@Query("grade") grade: String? = null, | ||
): BaseResponse<GetUnivRestaurantResponse> | ||
} |
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
20 changes: 20 additions & 0 deletions
20
domain/src/main/java/com/everymeal/domain/model/restaurant/GetUnivRestaurantEntity.kt
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,20 @@ | ||
package com.everymeal.domain.model.restaurant | ||
|
||
data class GetUnivRestaurantEntity( | ||
val data: List<RestaurantDataEntity> | ||
) | ||
|
||
data class RestaurantDataEntity( | ||
val idx: Int, | ||
val name: String, | ||
val address: String, | ||
val phoneNumber: String, | ||
val categoryDetail: String, | ||
val distance: Int, | ||
val grade: Float, | ||
val reviewCount: Int, | ||
val recommendedCount: Int, | ||
val images: List<String>?, | ||
val isLiked: Boolean | ||
) | ||
|
15 changes: 15 additions & 0 deletions
15
domain/src/main/java/com/everymeal/domain/repository/restaurant/RestaurantRepository.kt
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,15 @@ | ||
package com.everymeal.domain.repository.restaurant | ||
|
||
import androidx.paging.PagingData | ||
import com.everymeal.domain.model.restaurant.RestaurantDataEntity | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface RestaurantRepository { | ||
|
||
suspend fun getUnivRestaurant( | ||
campusIdx: Int, | ||
order: String, | ||
group: String? = null, | ||
grade: String? = null, | ||
) : Flow<PagingData<RestaurantDataEntity>> | ||
} |
20 changes: 20 additions & 0 deletions
20
domain/src/main/java/com/everymeal/domain/usecase/restaurant/GetUnivRestaurantUseCase.kt
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,20 @@ | ||
package com.everymeal.domain.usecase.restaurant | ||
|
||
import androidx.paging.PagingData | ||
import com.everymeal.domain.model.restaurant.RestaurantDataEntity | ||
import com.everymeal.domain.repository.restaurant.RestaurantRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
class GetUnivRestaurantUseCase @Inject constructor( | ||
private val restaurantRepository: RestaurantRepository | ||
) { | ||
suspend operator fun invoke( | ||
campusIdx: Int, | ||
order: String, | ||
group: String?, | ||
grade: String? | ||
) : Flow<PagingData<RestaurantDataEntity>> { | ||
return restaurantRepository.getUnivRestaurant(campusIdx, order, group, grade) | ||
} | ||
} |
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
Oops, something went wrong.