Skip to content

Commit

Permalink
feat: 방 필터 목록 조회 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
HyungJu committed May 21, 2024
1 parent 5bd044a commit aaf0ab0
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.nagaza.nagazaserver.domain.model

data class CafeRoomAddressMap(
val address1: String,
val address2: String,
val count: Int,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.nagaza.nagazaserver.domain.repository

import kr.nagaza.nagazaserver.domain.model.CafeRoom
import kr.nagaza.nagazaserver.domain.model.CafeRoomAddressMap

interface CafeRoomRepository {
fun getAllRoomByCafeId(cafeId: String): List<CafeRoom>
Expand All @@ -14,4 +15,8 @@ interface CafeRoomRepository {
address2: String?,
cafeId: String?,
): List<CafeRoom>

fun getAll(): List<CafeRoom>

fun getRoomCountGroupByAddress(): List<CafeRoomAddressMap>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kr.nagaza.nagazaserver.domain.model.CafeRoom
import kr.nagaza.nagazaserver.domain.repository.CafeRepository
import kr.nagaza.nagazaserver.domain.repository.CafeRoomRepository
import kr.nagaza.nagazaserver.presenter.restapi.dto.request.RoomSearchQuery
import kr.nagaza.nagazaserver.presenter.restapi.dto.response.CafeRoomAreaFilterItem
import org.springframework.stereotype.Service

@Service
Expand Down Expand Up @@ -35,4 +36,33 @@ class CafeRoomService(
cafeId = cafeRoomSearchQuery.cafeId,
)
}

fun getRoomAreaFilters(): CafeRoomAreaFilterItem {
val res = cafeRoomRepository.getRoomCountGroupByAddress()

val depth1 =
res.groupBy { it.address1 }.map {
CafeRoomAreaFilterItem(
name = it.key,
depth = 1,
count = it.value.size,
children =
it.value.map { depth2 ->
CafeRoomAreaFilterItem(
name = depth2.address2,
depth = 2,
count = depth2.count,
children = emptyList(),
)
},
)
}

return CafeRoomAreaFilterItem(
name = "전국",
depth = 0,
count = res.size,
children = depth1,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.querydsl.jpa.impl.JPAQuery
import jakarta.persistence.EntityManager
import jakarta.persistence.PersistenceContext
import kr.nagaza.nagazaserver.domain.model.CafeRoom
import kr.nagaza.nagazaserver.domain.model.CafeRoomAddressMap
import kr.nagaza.nagazaserver.domain.repository.CafeRoomRepository
import kr.nagaza.nagazaserver.infrastructure.jpa.entity.QCafeEntity
import kr.nagaza.nagazaserver.infrastructure.jpa.entity.QCafeRoomEntity
Expand Down Expand Up @@ -67,4 +68,32 @@ class CafeRoomRepositoryImpl(
it.toModel()
}
}

override fun getAll(): List<CafeRoom> {
return jpaCafeRoomRepository.findAll().map {
it.toModel()
}
}

override fun getRoomCountGroupByAddress(): List<CafeRoomAddressMap> {
val cafeRoomEntity = QCafeRoomEntity.cafeRoomEntity
val cafeEntity = QCafeEntity.cafeEntity
val result =
JPAQuery<CafeRoom>(entityManager)
.select(cafeEntity.addressOne, cafeEntity.addressTwo, cafeRoomEntity.count())
.from(cafeRoomEntity)
.join(cafeEntity).on(cafeRoomEntity.cafeId.eq(cafeEntity.cafeId))
.groupBy(cafeEntity.addressOne, cafeEntity.addressTwo)
.fetch()

return result.filter {
it.get(cafeEntity.addressOne) != null && it.get(cafeEntity.addressTwo) != null
}.map {
CafeRoomAddressMap(
address1 = it.get(cafeEntity.addressOne)!!,
address2 = it.get(cafeEntity.addressTwo)!!,
count = it.get(cafeRoomEntity.count())?.toInt() ?: 0,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.nagaza.nagazaserver.presenter.restapi.api

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import kr.nagaza.nagazaserver.presenter.restapi.dto.response.CafeRoomAreaFilterItem
import kr.nagaza.nagazaserver.presenter.restapi.dto.response.CafeRoomResponse
import org.springframework.data.repository.query.Param
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -28,4 +29,8 @@ interface CafeRoomApi {
fun getCafeRoom(
@PathVariable roomId: String,
): CafeRoomResponse

@Operation(summary = "카페 방 지역 필터", description = "카페 방 지역 필터 목록을 반환합니다.")
@GetMapping("/filter")
fun getCafeRoomAreaFilters(): CafeRoomAreaFilterItem
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RestController

@Tag(name = "카페 검색 API", description = "카페 검색 관련 API")
@RestController
@RequestMapping("/v1/cafes/search")
@RequestMapping("/v1/cafes/rooms/search")
interface CafeSearchApi {
@Operation(summary = "카페 검색", description = "문자열로 카페를 검색합니다.")
@GetMapping(params = ["q"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kr.nagaza.nagazaserver.presenter.restapi.controller
import kr.nagaza.nagazaserver.domain.service.CafeRoomService
import kr.nagaza.nagazaserver.presenter.restapi.api.CafeRoomApi
import kr.nagaza.nagazaserver.presenter.restapi.dto.request.RoomSearchQuery
import kr.nagaza.nagazaserver.presenter.restapi.dto.response.CafeRoomAreaFilterItem
import kr.nagaza.nagazaserver.presenter.restapi.dto.response.CafeRoomResponse
import org.springframework.stereotype.Controller

Expand All @@ -15,6 +16,10 @@ class CafeRoomController(
.let(CafeRoomResponse::fromModel)
}

override fun getCafeRoomAreaFilters(): CafeRoomAreaFilterItem {
return this.cafeRoomService.getRoomAreaFilters()
}

override fun searchRooms(
queryString: String?,
genre: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kr.nagaza.nagazaserver.presenter.restapi.dto.response

import com.fasterxml.jackson.annotation.JsonProperty
import io.swagger.v3.oas.annotations.media.Schema

@Schema(description = "방 지역 필터 응답")
data class CafeRoomAreaFilterItem(
@JsonProperty("name")
@Schema(description = "지역 구분 이름", example = "서울시")
val name: String,
@JsonProperty("depth")
@Schema(description = "지역 구분 깊이 (0: 전체, 1: 시, 2: 구)", example = "1")
val depth: Int,
@JsonProperty("count")
@Schema(description = "해당 지역에 속한 방 수", example = "23")
val count: Int,
@JsonProperty("children")
@Schema(description = "하위 지역 목록", example = "[]")
val children: List<CafeRoomAreaFilterItem>,
)

0 comments on commit aaf0ab0

Please sign in to comment.