77import ita .tinybite .domain .party .enums .ParticipantStatus ;
88import ita .tinybite .domain .party .enums .PartyCategory ;
99import ita .tinybite .domain .party .repository .PartyParticipantRepository ;
10- import ita .tinybite .domain .party .repository .PartyRepository ;
10+ import ita .tinybite .domain .party .repository .PartySearchRepository ;
11+ import ita .tinybite .global .util .DistanceCalculator ;
1112import lombok .RequiredArgsConstructor ;
1213import org .springframework .data .domain .Page ;
1314import org .springframework .data .domain .PageRequest ;
2324@ RequiredArgsConstructor
2425public class PartySearchService {
2526
26- private final PartyRepository partyRepository ;
27+ private final PartySearchRepository partySearchRepository ;
2728 private final PartyParticipantRepository participantRepository ;
2829 private final StringRedisTemplate redisTemplate ;
2930 private final SecurityProvider securityProvider ;
@@ -35,7 +36,7 @@ private String key(Long userId) {
3536 }
3637
3738 // 파티 검색 조회
38- public PartyQueryListResponse searchParty (String q , PartyCategory category , int page , int size ) {
39+ public PartyQueryListResponse searchParty (String q , PartyCategory category , int page , int size , Double lat , Double lon ) {
3940 Long userId = securityProvider .getCurrentUser ().getUserId ();
4041
4142 // recent_search:{userId}
@@ -45,24 +46,49 @@ public PartyQueryListResponse searchParty(String q, PartyCategory category, int
4546 redisTemplate .opsForZSet ().add (key , q , System .currentTimeMillis ());
4647
4748 Pageable pageable = PageRequest .of (page , size );
48-
49- // category가 없을 시에는 ALL로 처리
50- Page <Party > result = (category == null || category == PartyCategory .ALL )
51- ? partyRepository .findByTitleContaining (q , pageable )
52- : partyRepository .findByTitleContainingAndCategory (q , category , pageable );
53-
54- List <PartyCardResponse > partyCardResponseList = result .stream ()
55- .map (party -> {
56- int currentParticipants = participantRepository
57- .countByPartyIdAndStatus (party .getId (), ParticipantStatus .APPROVED );
58- return PartyCardResponse .from (party , currentParticipants );
59- })
60- .toList ();
61-
62- return PartyQueryListResponse .builder ()
63- .parties (partyCardResponseList )
64- .hasNext (result .hasNext ())
65- .build ();
49+ List <PartyCardResponse > partyCardResponseList ;
50+
51+ // 거리 정보 X
52+ if (lat == null || lon == null ) {
53+ // category가 없을 시에는 ALL로 처리
54+ Page <Party > queryResults = (category == null || category == PartyCategory .ALL )
55+ ? partySearchRepository .findByTitleContaining (q , pageable )
56+ : partySearchRepository .findByTitleContainingAndCategory (q , category , pageable );
57+
58+ partyCardResponseList = queryResults .stream ()
59+ .map (party -> {
60+ int currentParticipants = participantRepository
61+ .countByPartyIdAndStatus (party .getId (), ParticipantStatus .APPROVED );
62+ return PartyCardResponse .from (party , currentParticipants );
63+ })
64+ .toList ();
65+
66+ return PartyQueryListResponse .builder ()
67+ .parties (partyCardResponseList )
68+ .hasNext (queryResults .hasNext ())
69+ .build ();
70+ } else {
71+ // 거리 정보 O (lat, lon)
72+ Page <Party > queryResults = (category == null || category == PartyCategory .ALL )
73+ ? partySearchRepository .findByTitleContainingWithDistance (q , lat , lon , pageable )
74+ : partySearchRepository .findByTitleContainingAndCategoryWithDistance (q , lat , lon , category , pageable );
75+
76+ partyCardResponseList = queryResults .stream ()
77+ .map (party -> {
78+ int currentParticipants = participantRepository
79+ .countByPartyIdAndStatus (party .getId (), ParticipantStatus .APPROVED );
80+ PartyCardResponse res = PartyCardResponse .from (party , currentParticipants );
81+ Double distance = DistanceCalculator .calculateDistance (lat , lon , party .getPickupLocation ().getPickupLatitude (), party .getPickupLocation ().getPickupLongitude ());
82+ res .addDistanceKm (distance );
83+ return res ;
84+ })
85+ .toList ();
86+
87+ return PartyQueryListResponse .builder ()
88+ .parties (partyCardResponseList )
89+ .hasNext (queryResults .hasNext ())
90+ .build ();
91+ }
6692 }
6793
6894
0 commit comments