-
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.
- Loading branch information
Showing
20 changed files
with
521 additions
and
8 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/backend/itracker/crawl/airpods/domain/AirPods.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,52 @@ | ||
package backend.itracker.crawl.airpods.domain | ||
|
||
import backend.itracker.crawl.common.BaseEntity | ||
import jakarta.persistence.Column | ||
import jakarta.persistence.Embedded | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.EnumType | ||
import jakarta.persistence.Enumerated | ||
import jakarta.persistence.Table | ||
|
||
@Entity | ||
@Table(name = "airpods") | ||
class AirPods( | ||
val coupangId: Long, | ||
val company: String, | ||
val releaseYear: Int, | ||
val generation: Int, | ||
val canWirelessCharging: Boolean, | ||
val chargingType: String, | ||
val color: String, | ||
|
||
@Enumerated(EnumType.STRING) | ||
val category: AirPodsCategory, | ||
|
||
@Column(columnDefinition = "TEXT") | ||
val name: String, | ||
|
||
@Column(columnDefinition = "TEXT") | ||
val productLink: String, | ||
|
||
@Column(columnDefinition = "TEXT") | ||
val thumbnail: String, | ||
|
||
@Embedded | ||
val prices: AirPodsPrices = AirPodsPrices(), | ||
|
||
id: Long = 0L | ||
) : BaseEntity(id) { | ||
|
||
fun addAllPrices(prices: AirPodsPrices) { | ||
prices.airPodsPrices.forEach(this::addPrice) | ||
} | ||
|
||
fun addPrice(airPodsPrice: AirPodsPrice) { | ||
prices.add(airPodsPrice) | ||
airPodsPrice.changeAirPods(this) | ||
} | ||
|
||
override fun toString(): String { | ||
return "AirPods(coupangId=$coupangId, company='$company', releaseYear=$releaseYear, generation=$generation, canWirelessCharging=$canWirelessCharging, chargingType='$chargingType', color='$color', category=$category, name='$name', productLink='$productLink', thumbnail='$thumbnail')" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/backend/itracker/crawl/airpods/domain/AirPodsCategory.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,7 @@ | ||
package backend.itracker.crawl.airpods.domain | ||
|
||
enum class AirPodsCategory { | ||
AIRPODS, | ||
AIRPODS_PRO, | ||
AIRPODS_MAX | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/backend/itracker/crawl/airpods/domain/AirPodsPrice.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,35 @@ | ||
package backend.itracker.crawl.airpods.domain | ||
|
||
import backend.itracker.crawl.common.BaseEntity | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.FetchType | ||
import jakarta.persistence.ForeignKey | ||
import jakarta.persistence.JoinColumn | ||
import jakarta.persistence.ManyToOne | ||
import jakarta.persistence.Table | ||
import java.math.BigDecimal | ||
|
||
@Entity | ||
@Table(name = "airpods_price") | ||
class AirPodsPrice( | ||
val discountPercentage: Int, | ||
val basePrice: BigDecimal, | ||
val currentPrice: BigDecimal, | ||
val isOutOfStock: Boolean, | ||
id: Long = 0L | ||
) : BaseEntity(id) { | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn( | ||
name = "airpods_id", nullable = false, foreignKey = ForeignKey(name = "fk_airpods_price_airpods_id_ref_airpods_id") | ||
) | ||
var airPods: AirPods? = null | ||
|
||
fun changeAirPods(airPods: AirPods) { | ||
this.airPods = airPods | ||
} | ||
|
||
override fun toString(): String { | ||
return "AirPodsPrice(discountPercentage=$discountPercentage, basePrice=$basePrice, currentPrice=$currentPrice, isOutOfStock=$isOutOfStock)" | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/backend/itracker/crawl/airpods/domain/AirPodsPrices.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,16 @@ | ||
package backend.itracker.crawl.airpods.domain | ||
|
||
import jakarta.persistence.CascadeType | ||
import jakarta.persistence.Embeddable | ||
import jakarta.persistence.OneToMany | ||
|
||
@Embeddable | ||
class AirPodsPrices( | ||
@OneToMany(mappedBy = "airPods", cascade = [CascadeType.PERSIST]) | ||
val airPodsPrices: MutableList<AirPodsPrice> = mutableListOf() | ||
) { | ||
|
||
fun add(targetAirPodsPrice: AirPodsPrice) { | ||
airPodsPrices.add(targetAirPodsPrice) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/backend/itracker/crawl/airpods/domain/repository/AirPodsRepository.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 backend.itracker.crawl.airpods.domain.repository | ||
|
||
import backend.itracker.crawl.airpods.domain.AirPods | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
import org.springframework.data.repository.query.Param | ||
import java.util.* | ||
|
||
interface AirPodsRepository : JpaRepository<AirPods, Long> { | ||
|
||
@Query( | ||
""" | ||
select a | ||
from AirPods a | ||
join fetch a.prices | ||
where a.coupangId = :coupangId | ||
""" | ||
) | ||
fun findByCoupangId(@Param("coupangId") coupangId: Long): Optional<AirPods> | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/backend/itracker/crawl/airpods/service/AirPodsService.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,24 @@ | ||
package backend.itracker.crawl.airpods.service | ||
|
||
import backend.itracker.crawl.airpods.domain.AirPods | ||
import backend.itracker.crawl.airpods.domain.repository.AirPodsRepository | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Transactional | ||
@Service | ||
class AirPodsService( | ||
private val airPodsRepository: AirPodsRepository | ||
) { | ||
|
||
fun saveAll(airPodses: List<AirPods>) { | ||
for (airPods in airPodses) { | ||
val maybeAirPods = airPodsRepository.findByCoupangId(airPods.coupangId) | ||
if (maybeAirPods.isEmpty) { | ||
airPodsRepository.save(airPods) | ||
continue | ||
} | ||
maybeAirPods.get().addAllPrices(airPods.prices) | ||
} | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/backend/itracker/crawl/service/mapper/airpods/AirPodsGen2Mapper.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,48 @@ | ||
package backend.itracker.crawl.service.mapper.airpods | ||
|
||
import backend.itracker.crawl.airpods.domain.AirPods | ||
import backend.itracker.crawl.airpods.domain.AirPodsCategory | ||
import backend.itracker.crawl.service.response.AirPodsCrawlResponse | ||
import backend.itracker.crawl.service.vo.DefaultProduct | ||
import org.springframework.stereotype.Component | ||
|
||
private const val AIR_PODS_GEN_2 = "AirPods 2세대" | ||
|
||
@Component | ||
class AirPodsGen2Mapper : AirPodsMappingComponent { | ||
|
||
override fun supports(subCategory: String): Boolean { | ||
return AIR_PODS_GEN_2 == subCategory | ||
} | ||
|
||
override fun toDomain(product: DefaultProduct): AirPods { | ||
val names = product.name.split(",") | ||
.map { it.trim() } | ||
.toList() | ||
|
||
val title = names[0].split(" ") | ||
val company = title[0] | ||
val releaseYear = "2019" | ||
val category = AirPodsCategory.AIRPODS | ||
val generation = title[2].replace("세대", "") | ||
val canWirelessCharging = title[3] == "유선" | ||
val chargingType = "Lighting 8-pin" | ||
|
||
return AirPodsCrawlResponse( | ||
coupangId = product.productId, | ||
name = product.name, | ||
company = company, | ||
releaseYear = releaseYear, | ||
category = category, | ||
generation = generation, | ||
canWirelessCharging = canWirelessCharging, | ||
chargingType = chargingType, | ||
productLink = product.productLink, | ||
thumbnail = product.thumbnailLink, | ||
basePrice = product.price.basePrice, | ||
discountPercentage = product.price.discountPercentage, | ||
currentPrice = product.price.discountPrice, | ||
isOutOfStock = product.price.isOutOfStock | ||
).toDomain() | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/backend/itracker/crawl/service/mapper/airpods/AirPodsGen3Mapper.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,48 @@ | ||
package backend.itracker.crawl.service.mapper.airpods | ||
|
||
import backend.itracker.crawl.airpods.domain.AirPods | ||
import backend.itracker.crawl.airpods.domain.AirPodsCategory | ||
import backend.itracker.crawl.service.response.AirPodsCrawlResponse | ||
import backend.itracker.crawl.service.vo.DefaultProduct | ||
import org.springframework.stereotype.Component | ||
|
||
private const val AIR_PODS_GEN_3 = "AirPods 3세대" | ||
|
||
@Component | ||
class AirPodsGen3Mapper : AirPodsMappingComponent { | ||
|
||
override fun supports(subCategory: String): Boolean { | ||
return AIR_PODS_GEN_3 == subCategory | ||
} | ||
|
||
override fun toDomain(product: DefaultProduct): AirPods { | ||
val names = product.name.split(",") | ||
.map { it.trim() } | ||
.toList() | ||
|
||
val title = names[0].split(" ") | ||
val company = title[0] | ||
val releaseYear = title[1] | ||
val category = AirPodsCategory.AIRPODS | ||
val generation = title[3].replace("세대", "") | ||
val canWirelessCharging = title[4] != "유선" | ||
val chargingType = "Lighting 8-pin" | ||
|
||
return AirPodsCrawlResponse( | ||
coupangId = product.productId, | ||
name = product.name, | ||
company = company, | ||
releaseYear = releaseYear, | ||
category = category, | ||
generation = generation, | ||
canWirelessCharging = canWirelessCharging, | ||
chargingType = chargingType, | ||
productLink = product.productLink, | ||
thumbnail = product.thumbnailLink, | ||
basePrice = product.price.basePrice, | ||
discountPercentage = product.price.discountPercentage, | ||
currentPrice = product.price.discountPrice, | ||
isOutOfStock = product.price.isOutOfStock | ||
).toDomain() | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/backend/itracker/crawl/service/mapper/airpods/AirPodsMapper.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,36 @@ | ||
package backend.itracker.crawl.service.mapper.airpods | ||
|
||
import backend.itracker.crawl.airpods.domain.AirPods | ||
import backend.itracker.crawl.exception.CrawlException | ||
import backend.itracker.crawl.service.vo.DefaultProduct | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class AirPodsMapper( | ||
private val airPodsMappers: List<AirPodsMappingComponent> | ||
) { | ||
|
||
fun toDomain(filteredProducts: List<DefaultProduct>): List<AirPods> { | ||
val airPodses = mutableListOf<AirPods>() | ||
for (product in filteredProducts) { | ||
try { | ||
for (airPodsMapper in airPodsMappers) { | ||
if (airPodsMapper.supports(product.subCategory)) { | ||
airPodses.add(airPodsMapper.toDomain(product)) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
throw CrawlException( | ||
""" | ||
|AirPods Mapping 중에 에러가 발생했습니다. | ||
|name : ${product.name}, | ||
|subcategory : ${product.subCategory}, | ||
|error: ${e.stackTraceToString()} | ||
|""".trimMargin() | ||
) | ||
} | ||
} | ||
|
||
return airPodses | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/backend/itracker/crawl/service/mapper/airpods/AirPodsMappingComponent.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,11 @@ | ||
package backend.itracker.crawl.service.mapper.airpods | ||
|
||
import backend.itracker.crawl.airpods.domain.AirPods | ||
import backend.itracker.crawl.service.vo.DefaultProduct | ||
|
||
interface AirPodsMappingComponent { | ||
|
||
fun supports(subCategory: String): Boolean | ||
|
||
fun toDomain(product: DefaultProduct): AirPods | ||
} |
Oops, something went wrong.