Skip to content

Commit

Permalink
♻️ 역 식별자 통일 (#190) (#192)
Browse files Browse the repository at this point in the history
* ♻️ 엔티티 및 SQL 파일 수정 (#190)

* ♻️ 열차 실시간 및 혼잡도 조회 수정 (#190)

* ♻️ 전체 노선 및 역 정보 조회 수정 (#190)

* ♻️ 피드백 반영 (#190)

* ♻️ 지하철 노선 ID로 Key 변경 (#190)

* ♻️ 클래스 네이밍 수정 및 SQL 구문 수정 (#190)
  • Loading branch information
semi-cloud authored Nov 21, 2023
1 parent 75b118f commit b96db9f
Show file tree
Hide file tree
Showing 22 changed files with 198 additions and 84 deletions.
2 changes: 1 addition & 1 deletion ahachul_backend/ahachul_secret
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package backend.team.ahachul_backend.api.common.adapter.`in`.dto

import backend.team.ahachul_backend.api.common.domain.entity.StationEntity
import backend.team.ahachul_backend.api.common.domain.entity.SubwayLineStationEntity
import backend.team.ahachul_backend.common.domain.entity.SubwayLineEntity
import com.fasterxml.jackson.annotation.JsonProperty

Expand Down Expand Up @@ -34,4 +36,17 @@ data class SubwayLine(
data class Station(
@JsonProperty("id") val id: Long,
@JsonProperty("name") val name: String
)
) {
companion object {
fun from(station: StationEntity): Station {
return Station(
id = station.id,
name = station.name
)
}

fun toList(stations: List<SubwayLineStationEntity>): List<Station> {
return stations.map { from(it.station) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@ class StationPersistence(
throw AdapterException(ResponseCode.INVALID_DOMAIN)
}
}

override fun findAllBySubwayLine(subwayLine: SubwayLineEntity): List<StationEntity> {
return stationRepository.findAllBySubwayLineOrderByName(subwayLine) ?:
throw DomainException(ResponseCode.INVALID_DOMAIN)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ import backend.team.ahachul_backend.common.domain.entity.SubwayLineEntity
interface StationReader {

fun getById(id: Long): StationEntity

fun findAllBySubwayLine(subwayLine: SubwayLineEntity): List<StationEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ import org.springframework.data.jpa.repository.JpaRepository

interface StationRepository: JpaRepository<StationEntity, Long> {

fun findAllBySubwayLineOrderByName(subwayLine: SubwayLineEntity): List<StationEntity>?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package backend.team.ahachul_backend.api.common.application.port.out

import backend.team.ahachul_backend.api.common.domain.entity.SubwayLineStationEntity
import org.springframework.stereotype.Component

@Component
class SubwayLineStationPersistence(
private val subwayLineStationRepository: SubwayLineStationRepository
): SubwayLineStationReader {

override fun findAll(): List<SubwayLineStationEntity> {
return subwayLineStationRepository.findAll()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package backend.team.ahachul_backend.api.common.application.port.out

import backend.team.ahachul_backend.api.common.domain.entity.SubwayLineStationEntity

interface SubwayLineStationReader {

fun findAll(): List<SubwayLineStationEntity>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package backend.team.ahachul_backend.api.common.application.port.out

import backend.team.ahachul_backend.api.common.domain.entity.SubwayLineStationEntity
import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository

interface SubwayLineStationRepository: JpaRepository<SubwayLineStationEntity, Long> {

@EntityGraph(attributePaths = ["station", "subwayLine"])
override fun findAll(): List<SubwayLineStationEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,25 @@ import backend.team.ahachul_backend.api.common.adapter.`in`.dto.SearchSubwayLine
import backend.team.ahachul_backend.api.common.adapter.`in`.dto.Station
import backend.team.ahachul_backend.api.common.adapter.`in`.dto.SubwayLine
import backend.team.ahachul_backend.api.common.application.port.`in`.SubwayLineUseCase
import backend.team.ahachul_backend.api.common.application.port.out.StationReader
import backend.team.ahachul_backend.common.domain.entity.SubwayLineEntity
import backend.team.ahachul_backend.common.persistence.SubwayLineRepository
import backend.team.ahachul_backend.api.common.application.port.out.SubwayLineStationReader
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class SubwayLineService(
private val subwayLineRepository: SubwayLineRepository,
private val stationReader: StationReader
private val lineStationReader: SubwayLineStationReader
): SubwayLineUseCase {

@Cacheable("subwayLines")
override fun searchSubwayLines(): SearchSubwayLineDto.Response {
return SearchSubwayLineDto.Response(
subwayLineRepository.findAll()
.stream()
.map { subwayLine -> SubwayLine.of(subwayLine, getStations(subwayLine)) }
.toList()
)
}
val subwayLines = lineStationReader.findAll()
.groupBy { it.subwayLine.id }
.map {
SubwayLine.of(it.value[0].subwayLine, Station.toList(it.value))
}

private fun getStations(subwayLine: SubwayLineEntity): List<Station> {
val stations = stationReader.findAllBySubwayLine(subwayLine)
return stations.map {
Station(
id = it.id,
name = it.name
)
}
return SearchSubwayLineDto.Response(subwayLines)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package backend.team.ahachul_backend.api.common.domain.entity

import backend.team.ahachul_backend.common.domain.entity.SubwayLineEntity
import backend.team.ahachul_backend.common.entity.BaseEntity
import jakarta.persistence.*

Expand All @@ -13,13 +12,7 @@ class StationEntity(
@Column(name = "station_id")
var id: Long = 0,

var name: String,

var identity: Long = 0,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "subway_line_id")
var subwayLine: SubwayLineEntity
var name: String

): BaseEntity() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package backend.team.ahachul_backend.api.common.domain.entity

import backend.team.ahachul_backend.common.domain.entity.SubwayLineEntity
import backend.team.ahachul_backend.common.entity.BaseEntity
import jakarta.persistence.*

@Entity
class SubwayLineStationEntity(

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "subway_line_station_id")
var id: Long = 0,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "station_id")
val station: StationEntity,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "subway_line_id")
val subwayLine: SubwayLineEntity,

): BaseEntity() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class TrainController(

@GetMapping("/v1/trains/real-times")
fun getTrainRealTimes(request: GetTrainRealTimesDto.Request): CommonResponse<GetTrainRealTimesDto.Response> {
val result = trainUseCase.getTrainRealTimes(request.stationId)
val result = trainUseCase.getTrainRealTimes(request.stationId, request.subwayLineId)
return CommonResponse.success(GetTrainRealTimesDto.Response(result))
}

@Authentication
@GetMapping("/v1/trains/real-times/congestion")
fun getCongestion(request: GetCongestionDto.Request): CommonResponse<GetCongestionDto.Response> {
val result = trainUseCase.getTrainCongestion(request)
val result = trainUseCase.getTrainCongestion(request.toCommand())
return CommonResponse.success(result)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package backend.team.ahachul_backend.api.train.adapter.`in`.dto

import backend.team.ahachul_backend.api.train.application.port.`in`.command.GetCongestionCommand
import backend.team.ahachul_backend.api.train.domain.model.Congestion
import backend.team.ahachul_backend.api.train.domain.model.UpDownType

class GetCongestionDto {

data class Request(
val stationId: Long,
val subwayLineId: Long,
val upDownType: UpDownType
)
) {
fun toCommand(): GetCongestionCommand {
return GetCongestionCommand(
stationId = stationId,
subwayLineId = subwayLineId,
upDownType = upDownType
)
}
}

data class Response(
val trainNo: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class GetTrainRealTimesDto {

data class Request(
val stationId: Long,
val subwayLineId: Long
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package backend.team.ahachul_backend.api.train.application.port.`in`
import backend.team.ahachul_backend.api.train.adapter.`in`.dto.GetCongestionDto
import backend.team.ahachul_backend.api.train.adapter.`in`.dto.GetTrainDto
import backend.team.ahachul_backend.api.train.adapter.`in`.dto.GetTrainRealTimesDto
import backend.team.ahachul_backend.api.train.application.port.`in`.command.GetCongestionCommand

interface TrainUseCase {

fun getTrain(trainNo: String): GetTrainDto.Response

fun getTrainRealTimes(stationId: Long): List<GetTrainRealTimesDto.TrainRealTime>
fun getTrainRealTimes(stationId: Long, subwayLineId: Long): List<GetTrainRealTimesDto.TrainRealTime>

fun getTrainCongestion(request: GetCongestionDto.Request): GetCongestionDto.Response
fun getTrainCongestion(command: GetCongestionCommand): GetCongestionDto.Response
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package backend.team.ahachul_backend.api.train.application.port.`in`.command

import backend.team.ahachul_backend.api.train.domain.model.UpDownType

class GetCongestionCommand(
val stationId: Long,
val subwayLineId: Long,
val upDownType: UpDownType
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import backend.team.ahachul_backend.api.train.adapter.`in`.dto.GetCongestionDto
import backend.team.ahachul_backend.api.train.adapter.`in`.dto.GetTrainDto
import backend.team.ahachul_backend.api.train.adapter.`in`.dto.GetTrainRealTimesDto
import backend.team.ahachul_backend.api.train.application.port.`in`.TrainUseCase
import backend.team.ahachul_backend.api.train.application.port.`in`.command.GetCongestionCommand
import backend.team.ahachul_backend.api.train.application.port.out.TrainReader
import backend.team.ahachul_backend.api.train.domain.entity.TrainEntity
import backend.team.ahachul_backend.api.train.domain.model.TrainArrivalCode
Expand All @@ -17,6 +18,7 @@ import backend.team.ahachul_backend.common.dto.TrainRealTimeDto
import backend.team.ahachul_backend.common.exception.AdapterException
import backend.team.ahachul_backend.common.exception.BusinessException
import backend.team.ahachul_backend.common.logging.Logger
import backend.team.ahachul_backend.common.persistence.SubwayLineReader
import backend.team.ahachul_backend.common.response.ResponseCode
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -26,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional
class TrainService(
private val trainReader: TrainReader,
private val stationLineReader: StationReader,
private val subwayLineReader: SubwayLineReader,

private val seoulTrainClient: SeoulTrainClient,

Expand Down Expand Up @@ -61,9 +64,9 @@ class TrainService(
)
}

override fun getTrainRealTimes(stationId: Long): List<GetTrainRealTimesDto.TrainRealTime> {
override fun getTrainRealTimes(stationId: Long, subwayLineId: Long): List<GetTrainRealTimesDto.TrainRealTime> {
val station = stationLineReader.getById(stationId)
val subwayLine = station.subwayLine
val subwayLine = subwayLineReader.getById(subwayLineId)
val subwayLineIdentity = subwayLine.identity

val cachedData = trainCacheUtils.getCache(subwayLineIdentity, stationId)
Expand All @@ -83,6 +86,7 @@ class TrainService(
var endIndex = 5
var totalSize = startIndex
val trainRealTimes = mutableListOf<GetTrainRealTimesDto.TrainRealTime>()

while (startIndex <= totalSize) {
val trainRealTimesPublicData = seoulTrainClient.getTrainRealTimes(stationName, startIndex, endIndex)
totalSize = trainRealTimesPublicData.errorMessage?.total ?: break
Expand Down Expand Up @@ -131,20 +135,20 @@ class TrainService(
}
}

override fun getTrainCongestion(request: GetCongestionDto.Request): GetCongestionDto.Response {
val station = stationReader.getById(request.stationId)
val subwayLine = station.subwayLine
override fun getTrainCongestion(command: GetCongestionCommand): GetCongestionDto.Response {
val station = stationReader.getById(command.stationId)
val subwayLine = subwayLineReader.getById(command.subwayLineId)

if (isInValidSubwayLine(subwayLine.id)) {
throw BusinessException(ResponseCode.INVALID_SUBWAY_LINE)
}

val trains = getCachedTrainOrRequestExternal(subwayLine.identity, station.id)
val trains = getCachedTrainOrRequestExternal(subwayLine.id, subwayLine.identity, station.id)
if (trains.isEmpty()) {
return GetCongestionDto.Response.from(-1, emptyList())
}

val latestTrainNo = getLatestTrainNo(trains, subwayLine, request.upDownType)
val latestTrainNo = getLatestTrainNo(trains, subwayLine, command.upDownType)
val response = trainCongestionClient.getCongestions(subwayLine.id, latestTrainNo.toInt())
val trainCongestion = response.data!!
val congestions = mapCongestionDto(response.success, trainCongestion)
Expand All @@ -156,10 +160,10 @@ class TrainService(
}

private fun getCachedTrainOrRequestExternal(
subwayLineIdentity: Long, stationId: Long
subwayLineId: Long, subwayLineIdentity: Long, stationId: Long
): List<GetTrainRealTimesDto.TrainRealTime> {
return trainCacheUtils.getCache(subwayLineIdentity, stationId)
?: getTrainRealTimes(stationId)
?: getTrainRealTimes(stationId, subwayLineId)
}

private fun getLatestTrainNo(
Expand All @@ -175,7 +179,8 @@ class TrainService(
}

private fun mapCongestionDto(
success: Boolean, trainCongestion: TrainCongestionDto.Train): List<GetCongestionDto.Section> {
success: Boolean, trainCongestion: TrainCongestionDto.Train
): List<GetCongestionDto.Section> {
if (success) {
val congestionList = parse(trainCongestion.congestionResult.congestionCar)
return congestionList.mapIndexed {
Expand Down
Loading

0 comments on commit b96db9f

Please sign in to comment.