Skip to content

Commit

Permalink
Merge pull request #89 from mash-up-kr/feature/output-web/dhyeonkim
Browse files Browse the repository at this point in the history
feat: 방 정보 자동 생성 어댑터 구현 (LEMON)
  • Loading branch information
K-Diger authored Jul 20, 2024
2 parents 6354226 + 89edc93 commit 0846347
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import java.io.Serializable

class ThumbnailLinks(
data class ThumbnailLinks(
@JsonIgnore
val contents: String?,
) : Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.piikii.application.domain.place
import com.piikii.application.domain.generic.Origin
import com.piikii.application.domain.generic.ThumbnailLinks

class OriginPlace(
data class OriginPlace(
val originMapId: Long,
val url: String,
val thumbnailLinks: ThumbnailLinks,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.piikii.application.port.output.web

import com.piikii.application.domain.place.OriginPlace

interface UrlClient {
fun get(url: String): OriginPlace
}
2 changes: 2 additions & 0 deletions piikii-bootstrap/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ dependencies {
implementation(project(":piikii-output-cache:redis"))
implementation(project(":piikii-output-cache:caffeine"))
implementation(project(":piikii-output-storage:ncp"))
implementation(project(":piikii-output-web:avocado"))
implementation(project(":piikii-output-web:lemon"))

implementation("org.springframework.boot:spring-boot-starter-web")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import org.springframework.boot.runApplication

@SpringBootApplication(scanBasePackages = ["com.piikii"])
@ConfigurationPropertiesScan("com.piikii.output.storage.ncp.config")
@ConfigurationPropertiesScan("com.piikii.output.storage.ncp.config", "com.piikii.output.web.lemon.config")
class PiikiiBootstrapApplication

fun main(args: Array<String>) {
Expand Down
1 change: 1 addition & 0 deletions piikii-bootstrap/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ spring:
import:
- classpath:http-config/application.yml
- classpath:storage-config/application.yml
- classpath:lemon-config/application.yml
application:
name: "piikii"
messages:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ enum class ExceptionCode(

// 500
SECRET_MANAGER_CONFIG_NOT_SET(500, "시크릿 매니저 설정 값이 입력되지 않았습니다."),
URL_PROCESS_ERROR(500, "URL에 해당하는 장소의 정보를 불러오는 중 예기치 못한 오류가 발생했습니다."),
}
25 changes: 25 additions & 0 deletions piikii-output-web/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
project(":piikii-output-web:avocado") {
dependencies {
implementation("org.springframework:spring-web")
}
}

project(":piikii-output-web:lemon") {
dependencies {
implementation("org.springframework:spring-web")
}
}

allprojects {
dependencies {
implementation(project(":piikii-application"))
}

tasks.bootJar {
enabled = false
}

tasks.jar {
enabled = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.piikii.output.web.lemon

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.piikii.application.domain.generic.Origin
import com.piikii.application.domain.generic.ThumbnailLinks
import com.piikii.application.domain.place.OriginPlace

@JsonIgnoreProperties(ignoreUnknown = true)
data class UrlClientResponse(
val isMapUser: String?,
val isExist: Boolean?,
val basicInfo: BasicInfo,
val comment: Comment,
val menuInfo: MenuInfo,
val photo: Photo,
) {
fun toOriginPlace(url: String): OriginPlace {
val fullAddress = "${basicInfo.address.region.newaddrfullname} ${basicInfo.address.newaddr.newaddrfull}".trim()
return OriginPlace(
originMapId = basicInfo.cid,
url = url,
thumbnailLinks = ThumbnailLinks(basicInfo.mainphotourl),
address = fullAddress,
phoneNumber = basicInfo.phonenum,
starGrade = basicInfo.feedback.calculateStarGrade(),
origin = Origin.LEMON,
)
}

@JsonIgnoreProperties(ignoreUnknown = true)
data class BasicInfo(
val cid: Long,
val placenamefull: String,
val mainphotourl: String,
val phonenum: String,
val address: Address,
val homepage: String,
val category: Category,
val feedback: Feedback,
val openHour: OpenHour,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Address(
val newaddr: NewAddress,
val region: Region,
val addrbunho: String? = null,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class NewAddress(
val newaddrfull: String,
val bsizonno: String,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Region(
val name3: String,
val fullname: String,
val newaddrfullname: String,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Category(
val catename: String,
val cate1name: String,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Feedback(
val scoresum: Int,
val scorecnt: Int,
) {
fun calculateStarGrade(): Float? = if (scorecnt > 0) scoresum.toFloat() / scorecnt else null
}

@JsonIgnoreProperties(ignoreUnknown = true)
data class OpenHour(
val periodList: List<Period>,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Period(
val periodName: String,
val timeList: List<Time>,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Time(
val timeName: String,
val timeSE: String,
val dayOfWeek: String,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Comment(
val kamapComntcnt: Int,
val scoresum: Int,
val scorecnt: Int,
val list: List<CommentItem>,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class CommentItem(
val contents: String,
val point: Int,
val username: String,
val date: String,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class MenuInfo(
val menuList: List<MenuItem>,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class MenuItem(
val price: String,
val menu: String,
val desc: String,
val img: String,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class Photo(
val photoCount: Int,
val photoList: List<PhotoItem>,
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class PhotoItem(
val photoid: String,
val orgurl: String,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.piikii.output.web.lemon.adapter

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.piikii.application.domain.place.OriginPlace
import com.piikii.application.port.output.web.UrlClient
import com.piikii.common.exception.ExceptionCode
import com.piikii.common.exception.PiikiiException
import com.piikii.output.web.lemon.UrlClientResponse
import com.piikii.output.web.lemon.util.SaltAdditive
import org.springframework.stereotype.Component
import org.springframework.web.client.RestClient

@Component
class LemonUrlClient(
private val saltAdditive: SaltAdditive,
private val objectMapper: ObjectMapper,
) : UrlClient {
override fun get(url: String): OriginPlace {
val saltedUrl = saltAdditive.execute(url)
val response = fetchResponse(saltedUrl)
val placeResponse = parseResponse(response)
return placeResponse.toOriginPlace(url)
}

private fun fetchResponse(url: String): String {
val client = RestClient.builder().baseUrl(url).build()
return client.get().retrieve().body(String::class.java) ?: throw PiikiiException(
exceptionCode = ExceptionCode.URL_PROCESS_ERROR,
detailMessage = "url : $url",
)
}

private fun parseResponse(response: String): UrlClientResponse {
return objectMapper.readValue(response)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.piikii.output.web.lemon.config

import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "lemon")
data class LemonProperties(
val baseUrl: String,
val salt: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.piikii.output.web.lemon.util

import com.piikii.output.web.lemon.config.LemonProperties
import org.springframework.stereotype.Component

@Component
class SaltAdditive(
private val lemonProperties: LemonProperties,
) {
fun execute(url: String): String {
val regex = """(${lemonProperties.baseUrl})(\d+)""".toRegex()
return regex.replace(url) { matchResult ->
val (baseUrl, id) = matchResult.destructured
"$baseUrl${lemonProperties.salt}$id"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lemon:
base-url: ${LEMON_BASE_URL}
salt: ${LEMON_SALT}
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ include("piikii-output-persistence")
include("piikii-output-persistence:postgresql")
include("piikii-output-storage")
include("piikii-output-storage:ncp")
include("piikii-output-web")
include("piikii-output-web:avocado")
include("piikii-output-web:lemon")

0 comments on commit 0846347

Please sign in to comment.