Skip to content

Commit

Permalink
✨ Location API 분리 (region, sub_region) (#29)
Browse files Browse the repository at this point in the history
Replaced searchLocations with more specific methods: getLocationRegions and getLocationsByRegion. These changes improve clarity and specificity in location-related queries by focusing on distinct operations. Updated corresponding repository and adapter classes to support these new methods.
  • Loading branch information
waterfogSW authored Oct 10, 2024
1 parent cf3a677 commit dce4fd6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,29 @@ package com.threedays.bootstrap.api.user
import com.threedays.domain.user.repository.LocationQueryRepository
import com.threedays.oas.api.LocationsApi
import com.threedays.oas.model.Location
import com.threedays.oas.model.SearchLocationsResponse
import com.threedays.support.common.base.domain.UUIDTypeId
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RestController
import java.util.*

@RestController
class LocationsController(
private val locationQueryRepository: LocationQueryRepository,
) : LocationsApi {

override fun searchLocations(
name: String,
next: UUID?,
limit: Int
): ResponseEntity<SearchLocationsResponse> {
val (resultLocation, resultNext) = locationQueryRepository.searchLocations(
name = name,
next = next?.let { UUIDTypeId.from(it) },
limit = limit
)

val locationResponse: List<Location> = resultLocation.map {
Location(
id = it.id.value,
region = it.region.value,
subRegion = it.subRegion.value,
)
}
override fun getLocationRegions(): ResponseEntity<List<String>> {
return ResponseEntity.ok(locationQueryRepository.getLocationRegions())
}

return ResponseEntity.ok(
SearchLocationsResponse(
locations = locationResponse,
next = resultNext?.value
)
)
override fun getLocationsByRegion(regionName: String): ResponseEntity<List<Location>> {
return locationQueryRepository
.getLocationsByRegion(regionName)
.map {
Location(
id = it.id.value,
region = it.region.value,
subRegion = it.subRegion.value,
)
}
.let { ResponseEntity.ok(it) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import com.threedays.support.common.base.domain.QueryRepository

interface LocationQueryRepository : QueryRepository<Location, Location.Id> {

fun searchLocations(
name: String,
next: Location.Id?,
limit: Int
): Pair<List<Location>, Location.Id?>
/**
* 지역명(시,도) 목록을 조회합니다.
* @return 지역명 목록
*/
fun getLocationRegions(): List<String>

/**
* 지역명(시,도)으로 지역 목록을 조회합니다.
* @param regionName 지역명
* @return 지역 목록
*/
fun getLocationsByRegion(regionName: String): List<Location>

}
Original file line number Diff line number Diff line change
@@ -1,57 +1,31 @@
package com.threedays.persistence.user.adapter

import com.linecorp.kotlinjdsl.dsl.jpql.jpql
import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
import com.linecorp.kotlinjdsl.render.RenderContext
import com.linecorp.kotlinjdsl.support.spring.data.jpa.extension.createQuery
import com.threedays.domain.user.entity.Location
import com.threedays.domain.user.repository.LocationQueryRepository
import com.threedays.persistence.user.entity.LocationJpaEntity
import com.threedays.persistence.user.repository.LocationJpaRepository
import jakarta.persistence.EntityManager
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
@Transactional(readOnly = true)
class LocationQueryPersistenceAdapter(
private val locationJpaRepository: LocationJpaRepository,
private val entityManager: EntityManager,
private val jdslRenderContext: RenderContext,
) : LocationQueryRepository {

override fun searchLocations(
name: String,
next: Location.Id?,
limit: Int
): Pair<List<Location>, Location.Id?> {
val query: SelectQuery<LocationJpaEntity> = jpql {
select(
entity(LocationJpaEntity::class)
).from(
entity(LocationJpaEntity::class)
).whereAnd(
or(
path(LocationJpaEntity::region).like("%$name%"),
path(LocationJpaEntity::subRegion).like("%$name%")
),
next?.let { path(LocationJpaEntity::id).greaterThanOrEqualTo(it.value) }
)
}
override fun getLocationRegions(): List<String> {
return locationJpaRepository
.findAll {
select(path(LocationJpaEntity::region))
.from(entity(LocationJpaEntity::class))
.groupBy(path(LocationJpaEntity::region))
}.filterNotNull()
}

val result = entityManager
.createQuery(query, jdslRenderContext)
.apply { setMaxResults(limit + 1) }
.resultList
override fun getLocationsByRegion(regionName: String): List<Location> {
return locationJpaRepository
.findAllByRegion(regionName)
.map { it.toDomainEntity() }

val hasNextPage = result.size > limit
val nextId: Location.Id? = if (hasNextPage) {
result[limit].id
} else {
null
}
return result.take(limit) to nextId
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ import org.springframework.stereotype.Repository
import java.util.*

@Repository
interface LocationJpaRepository : JpaRepository<LocationJpaEntity, UUID>, KotlinJdslJpqlExecutor
interface LocationJpaRepository : JpaRepository<LocationJpaEntity, UUID>, KotlinJdslJpqlExecutor {

fun findAllByRegion(region: String): List<LocationJpaEntity>

}
2 changes: 1 addition & 1 deletion openapi
Submodule openapi updated 1 files
+113 −24 openapi.yaml

0 comments on commit dce4fd6

Please sign in to comment.