Skip to content

Commit

Permalink
feat : add search history logic (#13)
Browse files Browse the repository at this point in the history
* feat : add search history logic

* fix : jpa bug
  • Loading branch information
kshired authored May 24, 2024
1 parent 3fdf618 commit 5574bbe
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.sipethon.travel.application

import jakarta.transaction.Transactional

import me.sipethon.travel.domain.TravelPlan
import me.sipethon.travel.domain.TravelPlanKeyword
import me.sipethon.travel.infrastructure.TravelPlanKeywordRepository
Expand All @@ -9,7 +9,9 @@ import me.sipethon.travel.infrastructure.UserRepository
import me.sipethon.travel.interfaces.request.TravelPlanRequest
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Transactional(readOnly = true)
@Service
class TravelPlanService(
private val travelPlanRepository: TravelPlanRepository,
Expand Down Expand Up @@ -63,4 +65,17 @@ class TravelPlanService(

return travelPlan
}

fun searchHistory(userId: Long, onlyBookmarked: Boolean): Map<TravelPlan, List<TravelPlanKeyword>> {
val travelPlans = if (onlyBookmarked) {
travelPlanRepository.findAllByUserIdAndIsBookmarkedTrueOrderByCreatedAtDesc(userId)
} else {
travelPlanRepository.findAllByUserIdOrderByCreatedAtDesc(userId)
}
val travelPlanKeywords = travelPlanKeywordRepository.findAllByTravelPlanIdIn(travelPlans.map { it.id })

return travelPlans.associateWith { travelPlan ->
travelPlanKeywords.filter { it.travelPlanId == travelPlan.id }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package me.sipethon.travel.infrastructure
import me.sipethon.travel.domain.TravelPlanKeyword
import org.springframework.data.jpa.repository.JpaRepository

interface TravelPlanKeywordRepository: JpaRepository<TravelPlanKeyword, Long>
interface TravelPlanKeywordRepository: JpaRepository<TravelPlanKeyword, Long> {
fun findAllByTravelPlanIdIn(travelPlainIds: List<Long>): List<TravelPlanKeyword>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ package me.sipethon.travel.infrastructure
import me.sipethon.travel.domain.TravelPlan
import org.springframework.data.jpa.repository.JpaRepository

interface TravelPlanRepository : JpaRepository<TravelPlan, Long>
interface TravelPlanRepository : JpaRepository<TravelPlan, Long> {
fun findAllByUserIdOrderByCreatedAtDesc(userId: Long): List<TravelPlan>
fun findAllByUserIdAndIsBookmarkedTrueOrderByCreatedAtDesc(userId: Long): List<TravelPlan>
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class TravelPlanController(
@LoginUser userId: Long,
@RequestParam onlyBookmarked: Boolean
): SearchHistoryResponse {
TODO()
val result = travelPlanService.searchHistory(userId, onlyBookmarked)
return SearchHistoryResponse(result)
}

@PostMapping("/bookmark/{travelPlanId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package me.sipethon.travel.interfaces.response

import me.sipethon.travel.domain.TravelPlan
import me.sipethon.travel.domain.TravelPlanKeyword
import java.time.format.DateTimeFormatter

data class SearchHistoryResponse(
val searchHistory: List<SearchHistoryDetailResponse>
)
) {
constructor(travelPlanMap: Map<TravelPlan, List<TravelPlanKeyword>>) : this(
travelPlanMap.map { (travelPlan, keywords) ->
SearchHistoryDetailResponse(
id = travelPlan.id,
thumbnail = travelPlan.thumbnail,
location = travelPlan.location,
duration = travelPlan.duration,
people = travelPlan.people,
budget = travelPlan.budget,
groupType = travelPlan.groupType,
keywords = keywords.map { it.keyword },
createdAt = DateTimeFormatter.ofPattern("M월 d일, a h시").format(travelPlan.createdAt),
isBookmarked = travelPlan.isBookmarked
)
}
)
}

0 comments on commit 5574bbe

Please sign in to comment.