Skip to content
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

[Feature/#26] : 밥상 추천 모델 API 연결 #38

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package com.hackathon.alddeul_babsang.data.datasource

import com.hackathon.alddeul_babsang.data.dto.BaseResponse
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangDto
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangRecommendDto

interface BabsangDataSource {
suspend fun postStores(
userId: Int
): BaseResponse<List<ResponseBabsangDto>>

suspend fun postRecommendStores(
userId: Int
): BaseResponse<List<ResponseBabsangRecommendDto>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.hackathon.alddeul_babsang.data.datasourceimpl
import com.hackathon.alddeul_babsang.data.datasource.BabsangDataSource
import com.hackathon.alddeul_babsang.data.dto.BaseResponse
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangDto
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangRecommendDto
import com.hackathon.alddeul_babsang.data.service.BabsangApiService
import javax.inject.Inject

Expand All @@ -14,4 +15,10 @@ class BabsangDataSourceImpl @Inject constructor(
): BaseResponse<List<ResponseBabsangDto>> {
return babsangApiService.postStores(userId)
}

override suspend fun postRecommendStores(
userId: Int
): BaseResponse<List<ResponseBabsangRecommendDto>> {
return babsangApiService.postRecommendStores(userId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hackathon.alddeul_babsang.data.dto.request

import kotlinx.serialization.SerialName

class RequestBabsangRecommendDto(
@SerialName("userId") val userId: Long
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.hackathon.alddeul_babsang.data.dto.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponseBabsangRecommendDto (
@SerialName("name") val name: String,
@SerialName("category") val category: String,
@SerialName("region") val region: String,
@SerialName("storeId") val storeId: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hackathon.alddeul_babsang.data.repositoryimpl

import com.hackathon.alddeul_babsang.data.datasource.BabsangDataSource
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangDto
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangRecommendDto
import com.hackathon.alddeul_babsang.domain.repository.BabsangRepository
import javax.inject.Inject

Expand All @@ -17,4 +18,14 @@ class BabsangRepositoryImpl @Inject constructor(
).result ?: emptyList()
}
}

override suspend fun postRecommendStores(
userId: Int
): Result<List<ResponseBabsangRecommendDto>> {
return runCatching {
babsangDataSource.postRecommendStores(
userId = userId
).result ?: emptyList()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ object ApiKeyStorage {
const val STORE_ID = "storeId"
const val ID = "id"
const val REVIEWS = "reviews"
const val RECOMMEND = "recommend"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.hackathon.alddeul_babsang.data.service

import com.hackathon.alddeul_babsang.data.dto.BaseResponse
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangDto
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangRecommendDto
import com.sopt.data.service.ApiKeyStorage.RECOMMEND
import com.sopt.data.service.ApiKeyStorage.STORES
import retrofit2.http.POST
import retrofit2.http.Query
Expand All @@ -11,4 +13,9 @@ interface BabsangApiService {
suspend fun postStores(
@Query("userId") userId: Int
): BaseResponse<List<ResponseBabsangDto>>

@POST("/$RECOMMEND")
suspend fun postRecommendStores(
@Query("userId") userId: Int
) : BaseResponse<List<ResponseBabsangRecommendDto>>
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.hackathon.alddeul_babsang.domain.repository

import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangDto
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangRecommendDto

interface BabsangRepository {
suspend fun postStores(
userId: Int
): Result<List<ResponseBabsangDto>>

suspend fun postRecommendStores(
userId: Int
): Result<List<ResponseBabsangRecommendDto>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.hackathon.alddeul_babsang.R
import com.hackathon.alddeul_babsang.core_ui.theme.AlddeulBabsangTheme
import com.hackathon.alddeul_babsang.core_ui.theme.Blue
Expand All @@ -41,12 +38,12 @@ import com.hackathon.alddeul_babsang.core_ui.theme.body4Regular
import com.hackathon.alddeul_babsang.core_ui.theme.body7Semi
import com.hackathon.alddeul_babsang.core_ui.theme.head4Bold
import com.hackathon.alddeul_babsang.core_ui.theme.head5Bold
import com.hackathon.alddeul_babsang.domain.entity.BabsangRecommendEntity
import com.hackathon.alddeul_babsang.data.dto.response.ResponseBabsangRecommendDto

@Composable
fun BabsangRecommendItem(
onClick: () -> Unit = {},
data: BabsangRecommendEntity
data: ResponseBabsangRecommendDto
) {
Column(
modifier = Modifier
Expand All @@ -60,8 +57,8 @@ fun BabsangRecommendItem(
.fillMaxHeight()
.clip(RoundedCornerShape(14.dp))
) {
// AsyncImage 로드
ReplaceImage(data.codeName, data.avatar)
//카테고리별 이미지 지정
LoadImage(data.category)
Column(
modifier = Modifier
.padding(top = 110.dp)
Expand All @@ -75,7 +72,7 @@ fun BabsangRecommendItem(
.background(White),
contentAlignment = Alignment.Center
) {
Text(data.address, textAlign = TextAlign.Center, style = body7Semi)
Text(data.region, textAlign = TextAlign.Center, style = body7Semi)
}
Text(
modifier = Modifier
Expand All @@ -89,7 +86,7 @@ fun BabsangRecommendItem(
)
Text(
modifier = Modifier.padding(top = 6.dp),
text = data.codeName,
text = data.category,
style = body4Regular,
color = White
)
Expand All @@ -105,7 +102,7 @@ fun BabsangRecommendItem(
)
Spacer(modifier = Modifier.width(15.dp))
Text(
text = data.codeName,
text = data.category,
style = body2Regular,
color = Orange800,
modifier = Modifier
Expand All @@ -115,7 +112,7 @@ fun BabsangRecommendItem(
}
Spacer(modifier = Modifier.height(12.dp))
Text(
text = data.address,
text = data.region,
style = body4Regular,
color = Gray300,
modifier = Modifier.padding(start = 20.dp)
Expand All @@ -126,7 +123,7 @@ fun BabsangRecommendItem(
}

@Composable
fun ReplaceImage(codeName: String, imageUrl: String?) {
fun LoadImage(codeName: String) {
val imageId = when (codeName) {
"경양식/일식" -> R.drawable.ic_japanese_food
"한식" -> R.drawable.ic_korean_food
Expand All @@ -146,32 +143,16 @@ fun ReplaceImage(codeName: String, imageUrl: String?) {
.fillMaxHeight()
.background(color = backgroundColor) // 배경색 설정
) {
if (imageUrl.isNullOrEmpty()) {
// imageUrl이 null이나 empty일 경우 대체 이미지 표시
Image(
painter = painterResource(id = imageId), // 대체 이미지
contentDescription = null,
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
.padding(horizontal = 15.dp)
.padding(bottom = 80.dp)
.padding(top = 15.dp)
)
} else {
// imageUrl이 null이 아니면 AsyncImage로 비동기 이미지 로드
AsyncImage(
model = imageUrl,
contentDescription = null,
placeholder = painterResource(id = imageId),
contentScale = ContentScale.Crop, // 이미지 비율 유지, 크기 확대 또는 자르기
modifier = Modifier
.fillMaxWidth() // 가로는 꽉 차게
.fillMaxHeight() // 세로도 꽉 차게
.clip(RoundedCornerShape(topStart = 14.dp, topEnd = 14.dp))
)

}
Image(
painter = painterResource(id = imageId), // 대체 이미지
contentDescription = null,
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
.padding(horizontal = 15.dp)
.padding(bottom = 80.dp)
.padding(top = 15.dp)
)
}
}

Expand All @@ -180,12 +161,11 @@ fun ReplaceImage(codeName: String, imageUrl: String?) {
fun BabsangRecommendItemPreview() {
AlddeulBabsangTheme {
BabsangRecommendItem(
data = BabsangRecommendEntity(
id = 1,
avatar = null,
data = ResponseBabsangRecommendDto(
storeId = 1,
name = "송이네 밥상",
codeName = "경양식/일식",
address = "용산구",
category = "한식",
region = "용산구",
)
)
}
Expand Down
Loading
Loading