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

[feat] 스크랩 상품 등록 API 연결 #34

Merged
merged 5 commits into from
May 23, 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 @@ -11,4 +11,9 @@ interface ProductRemoteDataSource {
suspend fun getProductDetail(productId: Int): BaseResponse<ResponseProductDetailDto>

suspend fun getRecommendProduct(memberId: Int): BaseResponse<ResponseRecommendProductDto>

suspend fun postScrap(
memberId: Int,
productId: Int,
): BaseResponse<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.sopt.kream.data.datasourceimpl

import org.sopt.kream.data.ServicePool
import org.sopt.kream.data.datasource.ProductRemoteDataSource
import org.sopt.kream.data.model.request.RequestPostScrapDto
import org.sopt.kream.data.model.response.ResponseProductDetailDto
import org.sopt.kream.data.model.response.ResponseRecommendProductDto
import org.sopt.kream.data.model.response.ResponseSearchProductDto
Expand All @@ -15,4 +16,9 @@ class ProductRemoteDataSourceImpl : ProductRemoteDataSource {
override suspend fun getProductDetail(productId: Int): BaseResponse<ResponseProductDetailDto> = productService.getProductDetail(productId = productId)

override suspend fun getRecommendProduct(memberId: Int): BaseResponse<ResponseRecommendProductDto> = productService.getRecommendProduct(memberId = memberId)

override suspend fun postScrap(
memberId: Int,
productId: Int,
): BaseResponse<Unit> = productService.postScrap(memberId = memberId, request = RequestPostScrapDto(productId))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.sopt.kream.data.model.request

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

@Serializable
data class RequestPostScrapDto(
@SerialName("productId")
val productId: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ class ProductRepositoryImpl(
runCatching {
productRemoteDataSource.getRecommendProduct(memberId = memberId).data.toRecommendProductModel()
}

override suspend fun postScrap(
memberId: Int,
productId: Int,
): Result<Unit> =
runCatching {
productRemoteDataSource.postScrap(memberId = memberId, productId = productId)
}
}
17 changes: 13 additions & 4 deletions app/src/main/java/org/sopt/kream/data/service/ProductService.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.sopt.kream.data.service

import org.sopt.kream.data.model.request.RequestPostScrapDto
import org.sopt.kream.data.model.response.ResponseProductDetailDto
import org.sopt.kream.data.model.response.ResponseRecommendProductDto
import org.sopt.kream.data.model.response.ResponseReleaseProductDto
import org.sopt.kream.data.model.response.ResponseSearchProductDto
import org.sopt.kream.util.base.BaseResponse
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query

Expand All @@ -33,12 +36,18 @@ interface ProductService {
@Header("memberId") userid: Int,
): BaseResponse<ResponseReleaseProductDto>

companion object {
const val MEMBER_ID = 1
}

@GET("product/recommend")
suspend fun getRecommendProduct(
@Header("memberId") memberId: Int,
): BaseResponse<ResponseRecommendProductDto>

@POST("scrap")
suspend fun postScrap(
@Header("memberId") memberId: Int,
@Body request: RequestPostScrapDto,
): BaseResponse<Unit>

companion object {
const val MEMBER_ID = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ interface ProductRepository {
suspend fun getProductDetail(productId: Int): Result<ProductDetailModel>

suspend fun getRecommendProduct(memberId: Int): Result<RecommendProductModel>

suspend fun postScrap(
memberId: Int,
productId: Int,
): Result<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class RecommendFragment : BindingFragment<FragmentRecommendBinding>({ FragmentRe
advertisementAdapter = RecommendAdvertisementViewPagerAdapter(RecommendAdvertisementType.RECOMMEND_ADVERTISEMENT.advertisementList)
circleMenuAdapter = RecommendCircleMenuAdapter(RecommendCircleMenuType.entries)
forYouAdapter = RecommendForYouViewPagerAdapter(::navigateToProductDetail, ::navigateToSearch)
justDroppedAdapter = RecommendJustDroppedAdapter(::navigateToProductDetail)
justDroppedAdapter = RecommendJustDroppedAdapter(::navigateToProductDetail, recommendViewModel, memberId)
styleAdapter = RecommendStyleAdapter()

with(binding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.sopt.kream.util.view.ItemDiffCallback

class RecommendJustDroppedAdapter(
private val navigateToProductDetail: (Int) -> Unit,
private val recommendViewModel: RecommendViewModel,
private val memberId: Int,
) : ListAdapter<
RecommendJustDroppedProductModel,
RecommendJustDroppedViewHolder,
Expand All @@ -29,6 +31,8 @@ class RecommendJustDroppedAdapter(
false,
),
navigateToProductDetail,
recommendViewModel,
memberId,
)

override fun onBindViewHolder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import org.sopt.kream.domain.model.RecommendJustDroppedProductModel
class RecommendJustDroppedViewHolder(
private val binding: ItemRecommendJustDroppedProductBinding,
private val navigateToProductDetail: (Int) -> Unit,
private val recommendViewModel: RecommendViewModel,
private val memberId: Int,
) : RecyclerView.ViewHolder(binding.root) {
fun onBind(
recommendJustDroppedProductModel: RecommendJustDroppedProductModel,
position: Int,
) {
initScrapClickListener(recommendJustDroppedProductModel.isScrap, position + 1)

with(binding) {
ivJustDroppedProduct.load(recommendJustDroppedProductModel.thumbnailUrl)
tvJustDroppedProductBrand.text = recommendJustDroppedProductModel.brandTitle
Expand Down Expand Up @@ -48,4 +52,18 @@ class RecommendJustDroppedViewHolder(
}
}
}

fun initScrapClickListener(
isScrap: Boolean,
productId: Int,
) {
with(binding) {
ivJustDroppedProductScrap.setOnClickListener {
if (!isScrap) {
recommendViewModel.postScrapProduct(memberId, productId)
ivJustDroppedProductScrap.setImageResource(R.drawable.ic_saved_2_on_24)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class RecommendViewModel(
private val _recommendProductState = MutableStateFlow<UiState<RecommendProductModel>>(UiState.Empty)
val recommendProductState get() = _recommendProductState.asStateFlow()

private val _postScrapState = MutableStateFlow<UiState<Unit>>(UiState.Empty)
val postScrapState get() = _postScrapState.asStateFlow()

private val _instagramList =
listOf(
InstagramModel(
Expand Down Expand Up @@ -52,4 +55,18 @@ class RecommendViewModel(
}
}
}

fun postScrapProduct(
memberId: Int,
productId: Int,
) {
viewModelScope.launch {
_postScrapState.value = UiState.Loading
productRepository.postScrap(memberId = memberId, productId = productId).onSuccess { postScrapResult ->
_postScrapState.value = UiState.Success(postScrapResult)
}.onFailure { exception: Throwable ->
_postScrapState.value = UiState.Error(exception.message)
}
}
}
}
Loading