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/jstak/kakao route #97

Merged
merged 5 commits into from
Oct 12, 2023
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
@@ -1,6 +1,9 @@
package com.hh2.katj.history.component

import com.hh2.katj.user.model.dto.KakaoAddressSearchResponse
import com.hh2.katj.history.model.dto.KakaoAddressSearchResponse
import com.hh2.katj.history.model.dto.KakaoRouteSearchResponse
import com.hh2.katj.trip.model.DepartureRoadAddress
import com.hh2.katj.trip.model.DestinationRoadAddress
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
Expand Down Expand Up @@ -30,19 +33,18 @@ class KakaoApiManager(
return response
}

fun requestCarDirectionSearch(origin: String?, destination: String?): KakaoAddressSearchResponse? {
// TODO 형식 검사
// ${X좌표},${Y좌표},name=${출발지명} 또는 ${X좌표},${Y좌표}
checkNotNull(origin)
checkNotNull(destination)
fun requestCarDirectionSearch(departureRoadAddress: DepartureRoadAddress,
destinationRoadAddress: DestinationRoadAddress): KakaoRouteSearchResponse? {
val origin = "${departureRoadAddress.departureLongitude},${departureRoadAddress.departureLatitude}"
val destination = "${destinationRoadAddress.destinationLongitude},${destinationRoadAddress.destinationLatitude}"

val uri = kakaoUriBuilder.buildUriByCarDirectionSearch(origin, destination)
val headers = HttpHeaders()
headers.set(HttpHeaders.AUTHORIZATION, "KakaoAK $kakaoRestApiKey")

val httpEntity = HttpEntity<String>(headers)

return restTemplate.exchange(uri, HttpMethod.GET, httpEntity, KakaoAddressSearchResponse::class.java).body
return restTemplate.exchange(uri, HttpMethod.GET, httpEntity, KakaoRouteSearchResponse::class.java).body
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.hh2.katj.history.component

import com.hh2.katj.history.model.entity.SearchRouteHistory
import com.hh2.katj.history.repository.RouteHistoryRepository
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
class RouteHistoryManager(
private val routeHistoryRepository: RouteHistoryRepository,
) {

@Transactional
fun addRouteHistory(routeHistory: SearchRouteHistory): SearchRouteHistory {
val history = routeHistoryRepository.save(routeHistory)
return history
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class LocationHistoryController(
private val userService: UserService,
) {

/**
* 검색어로 위치정보(RoadAddress) 검색 및 이력 저장
*/
@PostMapping
fun saveLocation(@RequestBody request: RequestLocationHistory): ResponseEntity<ResponseLocationHistory> {
fun addLocationHistory(@RequestBody request: RequestLocationHistory): ResponseEntity<ResponseLocationHistory> {
val user = userService.findByUserId(userId = request.userId)
val response = locationHistoryService.saveLocationHistory(user, request.keyword)
return ResponseEntity.ok(response)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.hh2.katj.history.controller

import com.hh2.katj.history.model.dto.RequestRouteHistory
import com.hh2.katj.history.model.dto.Summary
import com.hh2.katj.history.service.RouteHistoryService
import com.hh2.katj.user.service.UserService
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RequestMapping("/route")
@RestController
class RouteHistoryController(
private val routeHistoryService: RouteHistoryService,
private val userService: UserService,
) {

/**
* 출/도착 정보로 택시 요금 조회 및 이력 저장
*/
@PostMapping
fun addRouteHistory(
@RequestBody requestRouteHistory: RequestRouteHistory,
): ResponseEntity<Summary> {
val findUser = userService.findByUserId(requestRouteHistory.userId)
val summary =
routeHistoryService.addRouteHistory(
findUser.id,
requestRouteHistory.toEntity(findUser)
)
return ResponseEntity.ok(summary)
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hh2.katj.user.model.dto
package com.hh2.katj.history.model.dto

import com.fasterxml.jackson.annotation.JsonProperty
import com.hh2.katj.util.model.RoadAddress
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.hh2.katj.history.model.dto

import com.fasterxml.jackson.annotation.JsonProperty

data class KakaoRouteSearchResponse(
@JsonProperty("trans_id")
val transId: String,
@JsonProperty("routes")
val routes: List<Routes>,
)

data class Routes(
@JsonProperty("result_code")
val resultCode: Int,
@JsonProperty("result_msg")
val resultMsg: String,
@JsonProperty("summary")
val summary: Summary,
)

data class Summary(
@JsonProperty("fare")
val fare: Fare,
@JsonProperty("distance")
val distance: Int,
@JsonProperty("duration")
val duration: Int,
)

data class Fare(
@JsonProperty("taxi")
val taxiFare: Int,
@JsonProperty("toll")
val tollFare: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.hh2.katj.history.model.dto

import com.hh2.katj.history.model.entity.SearchRouteHistory
import com.hh2.katj.trip.model.DepartureRoadAddress
import com.hh2.katj.trip.model.DestinationRoadAddress
import com.hh2.katj.user.model.entity.User

data class RequestRouteHistory(
val userId: Long,
val departureRoadAddress: DepartureRoadAddress,
val destinationRoadAddress: DestinationRoadAddress,
) {
fun toEntity(user: User): SearchRouteHistory {
return SearchRouteHistory(
user = user,
departureRoadAddress = departureRoadAddress,
destinationRoadAddress = destinationRoadAddress,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.hh2.katj.history.model.entity

import com.hh2.katj.trip.model.DepartureRoadAddress
import com.hh2.katj.trip.model.DestinationRoadAddress
import com.hh2.katj.user.model.entity.User
import com.hh2.katj.util.model.BaseEntity
import jakarta.persistence.*

@Entity
@Table(name = "search_route_history")
class SearchRouteHistory(
user: User,
departureRoadAddress: DepartureRoadAddress,
destinationRoadAddress: DestinationRoadAddress,
) : BaseEntity() {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false,
foreignKey = ForeignKey(ConstraintMode.NO_CONSTRAINT)
)
var user = user
protected set

@Embedded
@Column(nullable = false)
var departureRoadAddress = departureRoadAddress
protected set

@Embedded
@Column(nullable = false)
var destinationRoadAddress = destinationRoadAddress
protected set
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hh2.katj.history.repository

import com.hh2.katj.history.model.entity.SearchRouteHistory
import org.springframework.data.jpa.repository.JpaRepository

interface RouteHistoryRepository : JpaRepository<SearchRouteHistory, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LocationHistoryService(
// TODO 예외 메시지
checkNotNull(response) { "api 호출 오류" }
check(response.documents.isNotEmpty()) {
failWithMessage(ExceptionMessage.NO_SEARCH_LOCATION_RESULT.name)
failWithMessage(ExceptionMessage.NO_SEARCH_RESULT.name)
}

val roadAddress = response.documents[0].roadAddress
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hh2.katj.history.service

import com.hh2.katj.history.component.KakaoApiManager
import com.hh2.katj.history.component.RouteHistoryManager
import com.hh2.katj.history.model.dto.KakaoRouteSearchResponse
import com.hh2.katj.history.model.dto.Summary
import com.hh2.katj.history.model.entity.SearchRouteHistory
import com.hh2.katj.user.service.UserService
import com.hh2.katj.util.exception.ExceptionMessage
import com.hh2.katj.util.exception.failWithMessage
import org.springframework.stereotype.Service

@Service
class RouteHistoryService(
private val kakaoApiManager: KakaoApiManager,
private val routeHistoryManager: RouteHistoryManager,
private val userService: UserService,
) {

fun addRouteHistory(userId: Long, routeHistory: SearchRouteHistory): Summary {
val validatedUser = userService.userValidationCheck(userId)
userService.userStatusActiveCheck(validatedUser)

val response = kakaoApiManager.requestCarDirectionSearch(
routeHistory.departureRoadAddress,
routeHistory.destinationRoadAddress
)

checkNotNull(response) { "api 호출 오류" }
check(response.routes.isNotEmpty()) {
failWithMessage(ExceptionMessage.NO_SEARCH_RESULT.name)
}

routeHistoryManager.addRouteHistory(routeHistory)

return response.routes[0].summary
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ enum class ExceptionMessage(message: String, status: Int) {
USER_DOES_NOT_EXIST("user does not exist", 400),
INTERNAL_SERVER_ERROR_FROM_DATABASE("internal server error from database", 500),
INCORRECT_STATUS_VALUE("incorrect status value", 500),
NO_SEARCH_LOCATION_RESULT("no search results", 400),
NO_SEARCH_RESULT("no search results", 400),
NO_SUCH_VALUE_EXISTS("no such value exists", 400),
INVALID_PAYMENT_METHOD("invalid payment method", 500),
}
Loading
Loading