Skip to content

Commit

Permalink
Merge branch 'feat/#9-goods-api' of https://github.com/SOPT-all/35-CO…
Browse files Browse the repository at this point in the history
…LLABORATION-ANDROID-MARKETKURLY into feat/#11-review-wishlist-api
  • Loading branch information
yskim6772 committed Nov 26, 2024
2 parents d39c5fe + ad1fc91 commit bd59026
Show file tree
Hide file tree
Showing 19 changed files with 291 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,34 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.market_kurly.domain.repository.ExampleRepository
import com.example.market_kurly.domain.repository.GoodsRepository
import com.example.market_kurly.domain.repository.LikeRepository
import com.example.market_kurly.feature.ExampleViewModel
import com.example.market_kurly.feature.goods.viewmodel.GoodsViewModel

class BaseViewModelFactory(
private val exampleRepository: ExampleRepository? = null,
private val goodsRepository: GoodsRepository? = null,
private val likeRepository: LikeRepository? = null,
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return when {
modelClass.isAssignableFrom(ExampleViewModel::class.java) -> {
@Suppress("UNCHECKED_CAST")
exampleRepository?.let { ExampleViewModel(it) } as T
}

modelClass.isAssignableFrom(GoodsViewModel::class.java) -> {
@Suppress("UNCHECKED_CAST")
goodsRepository?.let { GoodsViewModel(it) } as T
goodsRepository?.let {
likeRepository?.let {
GoodsViewModel(
goodsRepository = goodsRepository,
likeRepository = likeRepository
)
}
} as T
}

else -> throw IllegalArgumentException("Unknown ViewModel class")
}
}
Expand Down

This file was deleted.

4 changes: 4 additions & 0 deletions app/src/main/java/com/example/market_kurly/data/ApiFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.example.market_kurly.data

import com.example.market_kurly.BuildConfig
import com.example.market_kurly.data.service.ExampleService
import com.example.market_kurly.data.service.GoodsService
import com.example.market_kurly.data.service.LikeService
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
Expand Down Expand Up @@ -31,4 +33,6 @@ object ApiFactory {

object ServicePool {
val exampleService = ApiFactory.create<ExampleService>()
val goodsService = ApiFactory.create<GoodsService>()
val likeService = ApiFactory.create<LikeService>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import kotlinx.serialization.Serializable
data class BaseResponse<T>(
@SerialName("success")
val success: Boolean,
@SerialName("error")
val error: BaseError? = null,
@SerialName("status")
val status: Int? = null,
@SerialName("message")
val message: String,
@SerialName("data")
val data: T? = null,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.market_kurly.data.dto.response

import kotlinx.serialization.Serializable


//TODO: 서버쌤들이 null 반환 고쳐주면 수정 필요
@Serializable
data class ResponseGoodsDetailDto(
val name: String,
val price: Int,
val discount: Int,
val discountedPrice: Int,
val image: String,
val membersDiscount: Int,
val membersDiscountedPrice: Int,
val deliveryType: String,
val seller: String,
val origin: String,
val packagingType: String,
val sellingUnit: String,
val weight: String,
val expiration: String? = null,
val brix: Float? = null,
val notification: String,
val category: String,
val allergy: String,
val isInterest: Boolean,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.market_kurly.data.mapper

import com.example.market_kurly.data.dto.response.ResponseGoodsDetailDto
import com.example.market_kurly.domain.model.GoodsInfoData
import com.example.market_kurly.domain.model.GoodsUiData
fun ResponseGoodsDetailDto.toGoodsUiData() = GoodsUiData(
deliverType = this.deliveryType,
discount = this.discount,
image = this.image,
membersDiscount = this.membersDiscount,
discountedPrice = this.discountedPrice,
membersDiscountedPrice = this.membersDiscountedPrice,
name = this.name,
price = this.price,
seller = this.seller,
origin = this.origin,
isInterest = this.isInterest,
infoData = GoodsInfoData(
allergy = this.allergy,
brix = this.brix,
expiration = this.expiration,
notification = this.notification,
packagingType = this.packagingType,
sellingUnit = this.sellingUnit,
weight = this.weight,
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.market_kurly.data.service

import com.example.market_kurly.data.dto.base.BaseResponse
import com.example.market_kurly.data.dto.response.ResponseGoodsDetailDto
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Path

interface GoodsService {
companion object {
const val PRODUCTS = "products"
}

@GET("$PRODUCTS/{productId}")
suspend fun getGoodsDetailById(
@Path("productId") productsId : Int,
@Header("memberId") memberId : Int,
): BaseResponse<ResponseGoodsDetailDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.market_kurly.data.service

import com.example.market_kurly.data.dto.base.BaseResponse
import retrofit2.http.DELETE
import retrofit2.http.Header
import retrofit2.http.POST
import retrofit2.http.Path

interface LikeService {
companion object {
const val LIKES = "likes"
const val PRODUCTS = "products"
}

@POST("$LIKES/$PRODUCTS/{productId}")
suspend fun like(
@Path("productId") productsId : Int,
@Header("memberId") memberId : Int,
): BaseResponse<String?>

@DELETE("$LIKES/$PRODUCTS/{productId}")
suspend fun unLike(
@Path("productId") productsId : Int,
@Header("memberId") memberId : Int,
): BaseResponse<String?>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.market_kurly.core.dummymodel
package com.example.market_kurly.domain.model

data class AlsoViewedData(
val image: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.example.market_kurly.core.dummymodel
package com.example.market_kurly.domain.model

//TODO: 서버쌤들이 null 반환 고쳐주면 수정 필요
data class GoodsUiData(
val deliverType: String,
val discount: Int,
val discountedPrice: Int,
val image: String,
val membersDiscount: Int,
val membersDiscountedPrice: Int,
val name: String,
val price: Int,
val seller: String,
val origin: String,
val isFavorite: Boolean,
val isInterest: Boolean,
val infoData: GoodsInfoData,
)
data class GoodsInfoData(
val allergy: String,
val brix: String,
val expiration: String,
val livestock: String,
val brix: Float?,
val expiration: String?,
val notification: String,
val packagingType: String,
val sellingUnit: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.market_kurly.domain.repository

import com.example.market_kurly.core.dummymodel.AlsoViewedData
import com.example.market_kurly.core.dummymodel.GoodsUiData
import com.example.market_kurly.domain.model.AlsoViewedData
import com.example.market_kurly.domain.model.GoodsUiData

interface GoodsRepository {
fun getDummyAlsoViewedList(): List<AlsoViewedData>
fun getDummyGoodsDetail(): GoodsUiData
suspend fun getGoodsDetailById(productsId : Int, memberId : Int): Result<GoodsUiData?>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.market_kurly.domain.repository

interface LikeRepository {
suspend fun like(productsId : Int, memberId : Int): Result<String?>
suspend fun unLike(productsId : Int, memberId : Int): Result<String?>
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
package com.example.market_kurly.domain.repositoryimpl

import com.example.market_kurly.core.dummymodel.AlsoViewedData
import com.example.market_kurly.core.dummymodel.GoodsDetailData
import com.example.market_kurly.core.dummymodel.GoodsUiData
import com.example.market_kurly.core.dummymodel.toGoodsUiData
import com.example.market_kurly.data.mapper.toGoodsUiData
import com.example.market_kurly.data.service.GoodsService
import com.example.market_kurly.domain.model.AlsoViewedData
import com.example.market_kurly.domain.model.GoodsUiData
import com.example.market_kurly.domain.repository.GoodsRepository
import okhttp3.internal.immutableListOf

class GoodsRepositoryImpl() : GoodsRepository {
class GoodsRepositoryImpl(
private val goodsService: GoodsService,
) : GoodsRepository {
override fun getDummyAlsoViewedList(): List<AlsoViewedData> =
immutableListOf(
AlsoViewedData("https://product-image.kurly.com/hdims/resize/%5E%3E720x%3E936/cropcenter/720x936/quality/85/src/product/image/59526651-9c34-4a39-9cd0-e30669a9ec4f.jpg", "[KF365] 유명산지 고당도사과 1.5kg (5~6입)", 16, 19900),
AlsoViewedData("https://product-image.kurly.com/hdims/resize/%5E%3E720x%3E936/cropcenter/720x936/quality/85/src/product/image/d79e3f0a-2739-4d54-a5b3-3dbd1d5b4388.jpeg", "고랭지 햇사과 1.3kg (4~6입)", 13, 14900),
AlsoViewedData("https://img-cf.kurly.com/hdims/resize/%5E%3E720x%3E936/cropcenter/720x936/quality/85/src/shop/data/goods/160342712083l0.jpg", "감홍 사과 1.3kg (4~6입)", 25, 19900),
AlsoViewedData("https://product-image.kurly.com/hdims/resize/%5E%3E720x%3E936/cropcenter/720x936/quality/85/src/product/image/30b30de4-14f7-438a-844d-604b5a2acde9.jpg", "세척 사과 1.4kg (7입)", 16, 14900),
AlsoViewedData(
"https://product-image.kurly.com/hdims/resize/%5E%3E720x%3E936/cropcenter/720x936/quality/85/src/product/image/59526651-9c34-4a39-9cd0-e30669a9ec4f.jpg",
"[KF365] 유명산지 고당도사과 1.5kg (5~6입)",
16,
19900
),
AlsoViewedData(
"https://product-image.kurly.com/hdims/resize/%5E%3E720x%3E936/cropcenter/720x936/quality/85/src/product/image/d79e3f0a-2739-4d54-a5b3-3dbd1d5b4388.jpeg",
"고랭지 햇사과 1.3kg (4~6입)",
13,
14900
),
AlsoViewedData(
"https://img-cf.kurly.com/hdims/resize/%5E%3E720x%3E936/cropcenter/720x936/quality/85/src/shop/data/goods/160342712083l0.jpg",
"감홍 사과 1.3kg (4~6입)",
25,
19900
),
AlsoViewedData(
"https://product-image.kurly.com/hdims/resize/%5E%3E720x%3E936/cropcenter/720x936/quality/85/src/product/image/30b30de4-14f7-438a-844d-604b5a2acde9.jpg",
"세척 사과 1.4kg (7입)",
16,
14900
),
)

override fun getDummyGoodsDetail(): GoodsUiData =
GoodsDetailData(
allergy = "0",
brix = "13.5 Brix 이상",
category = "과일, 견과, 쌀",
deliverType = "샛별배송",
discount = 13,
expiration = "농산물로 별도의 소비기한은 없으나 가급적 빠르게 드시기 바랍니다.",
image = "https://img-cf.kurly.com/hdims/resize/%5E%3E720x%3E936/cropcenter/720x936/quality/85/src/shop/data/goods/1538466010000_l.jpg",
livestock = "0",
membersDiscount = 26,
name = "아삭하고 달콤한 황금사과1.3kg (5~7입)[품종:시나노골드]",
notification = "식품 특성상 중량은 3% 내외의 차이가 발생할 수 있습니다.\n신선식품 특성상 원물마다 크기 및 형태가 일정하지 않을 수 있습니다.",
origin = "국산",
packagingType = "냉장 (종이포장)",
price = 14900,
seller = "컬리",
sellingUnit = "1봉",
view = 156327,
weight = "1.3kg 내외",
isFavorite = true,
).toGoodsUiData()
}
override suspend fun getGoodsDetailById(productsId: Int, memberId: Int): Result<GoodsUiData?> =
runCatching {
val response = goodsService.getGoodsDetailById(productsId, memberId)
response.data?.toGoodsUiData()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.market_kurly.domain.repositoryimpl

import com.example.market_kurly.data.service.LikeService
import com.example.market_kurly.domain.repository.LikeRepository

class LikeRepositoryImpl(
private val likeService: LikeService,
) : LikeRepository {

override suspend fun like(productsId: Int, memberId: Int): Result<String?> = runCatching {
val response = likeService.like(productsId, memberId)
response.data
}

override suspend fun unLike(productsId: Int, memberId: Int): Result<String?> = runCatching {
val response = likeService.unLike(productsId, memberId)
response.data
}
}
Loading

0 comments on commit bd59026

Please sign in to comment.