44import ita .tinybite .domain .chat .enums .ChatRoomType ;
55import ita .tinybite .domain .chat .repository .ChatRoomRepository ;
66import ita .tinybite .domain .party .dto .request .PartyCreateRequest ;
7- import ita .tinybite .domain .party .dto .request .PartyQueryListResponse ;
7+ import ita .tinybite .domain .party .dto .request .PartyListRequest ;
88import ita .tinybite .domain .party .dto .request .PartyUpdateRequest ;
99import ita .tinybite .domain .party .dto .response .*;
1010import ita .tinybite .domain .party .entity .Party ;
1111import ita .tinybite .domain .party .entity .PartyParticipant ;
1212import ita .tinybite .domain .party .entity .PickupLocation ;
1313import ita .tinybite .domain .party .enums .ParticipantStatus ;
1414import ita .tinybite .domain .party .enums .PartyCategory ;
15+ import ita .tinybite .domain .party .enums .PartySortType ;
1516import ita .tinybite .domain .party .enums .PartyStatus ;
1617import ita .tinybite .domain .party .repository .PartyParticipantRepository ;
1718import ita .tinybite .domain .party .repository .PartyRepository ;
1819import ita .tinybite .domain .user .entity .User ;
1920import ita .tinybite .domain .user .repository .UserRepository ;
20-
21- import java .time .LocalDateTime ;
22- import java .util .List ;
23- import java .util .stream .Collectors ;
24-
2521import ita .tinybite .global .location .LocationService ;
2622import ita .tinybite .global .util .DistanceCalculator ;
2723import lombok .RequiredArgsConstructor ;
28- import org .springframework .data .domain .Page ;
29- import org .springframework .data .domain .PageRequest ;
30- import org .springframework .data .domain .Pageable ;
3124import org .springframework .stereotype .Service ;
3225import org .springframework .transaction .annotation .Transactional ;
3326
27+ import java .time .LocalDateTime ;
28+ import java .util .Comparator ;
29+ import java .util .List ;
30+ import java .util .stream .Collectors ;
31+
3432
3533@ Service
3634@ RequiredArgsConstructor
@@ -42,6 +40,7 @@ public class PartyService {
4240 private final PartyParticipantRepository partyParticipantRepository ;
4341 private final ChatRoomRepository chatRoomRepository ;
4442 private final PartyParticipantRepository participantRepository ;
43+
4544 /**
4645 * 파티 생성
4746 */
@@ -105,72 +104,43 @@ public Long createParty(Long userId, PartyCreateRequest request) {
105104 /**
106105 * 파티 목록 조회 (홈 화면)
107106 */
108- public PartyListResponse getPartyList (Long userId , PartyCategory category ) {
107+ public PartyListResponse getPartyList (Long userId , PartyListRequest request ) {
109108 User user = null ;
110109 if (userId != null ) {
111110 user = userRepository .findById (userId ).orElse (null );
112111 }
113112
114113 // 동네 기준으로 파티 조회
115- List <Party > parties = List .of ();
116- if (user != null && user .getLocation () != null ) {
117- if (category == PartyCategory .ALL ) {
118- parties = partyRepository .findByPickupLocation_Place (user .getLocation ());
119- } else {
120- parties = partyRepository .findByPickupLocation_PlaceAndCategory (
121- user .getLocation (), category );
122- }
123- }
124- // else {
125- // // 비회원이거나 동네 미설정 시
126- // String location = locationService.getLocation(userLat, userLon);
127- // if (category == PartyCategory.ALL) {
128- // parties = partyRepository.findByPickupLocation_Place(location);
129- // } else {
130- // parties = partyRepository.findByPickupLocation_PlaceAndCategory(
131- // location, category);
132- // }
133- // }
134-
135- // List<PartyCardResponse> cardResponses = parties.stream()
136- // .map(party -> {
137- // // DistanceCalculator 활용
138- // double distance = DistanceCalculator.calculateDistance(
139- // Double.parseDouble(userLat), Double.parseDouble(userLon),
140- // party.getLatitude(), party.getLongitude()
141- // );
142- // return convertToCardResponse(party, distance, userId, party.getCreatedAt());
143- // })
144- // .collect(Collectors.toList());
145- //
146- // // 진행 중 파티: 거리 가까운 순 정렬
147- // List<PartyCardResponse> activeParties = cardResponses.stream()
148- // .filter(p -> !p.getIsClosed())
149- // .sorted((a, b) -> Double.compare(a.getDistanceKm(), b.getDistanceKm()))
150- // .collect(Collectors.toList());
151- //
152- // // 마감된 파티: 거리 가까운 순 정렬
153- // List<PartyCardResponse> closedParties = cardResponses.stream()
154- // .filter(PartyCardResponse::getIsClosed)
155- // .sorted((a, b) -> Double.compare(a.getDistanceKm(), b.getDistanceKm()))
156- // .collect(Collectors.toList());
157-
114+ List <Party > parties = fetchPartiesByLocation (user , request );
158115
116+ // PartyCardResponse로 변환
159117 List <PartyCardResponse > cardResponses = parties .stream ()
160- .map (party -> convertToCardResponse (party , userId , party .getCreatedAt ()))
118+ .map (party -> {
119+ // 거리순 정렬인 경우 거리 계산
120+ if (request .getSortType () == PartySortType .DISTANCE ) {
121+ double distance = DistanceCalculator .calculateDistance (
122+ request .getUserLat (),
123+ request .getUserLon (),
124+ party .getPickupLocation ().getPickupLatitude (),
125+ party .getPickupLocation ().getPickupLongitude ()
126+ );
127+ return convertToCardResponseWithDistance (party , distance );
128+ }
129+ return convertToCardResponse (party ,party .getCreatedAt ());
130+ })
131+ .toList ();
132+
133+ // 진행 중 파티 정렬
134+ List <PartyCardResponse > activeParties = cardResponses .stream ()
135+ .filter (p -> !p .getIsClosed ())
136+ .sorted (getComparator (request .getSortType ()))
161137 .collect (Collectors .toList ());
162138
163- // 진행 중 파티: 최신순 정렬 (createdAt 기준 내림차순)
164- List <PartyCardResponse > activeParties = cardResponses .stream ()
165- .filter (p -> !p .getIsClosed ())
166- .sorted ((a , b ) -> b .getCreatedAt ().compareTo (a .getCreatedAt ()))
167- .collect (Collectors .toList ());
168-
169- // 마감된 파티: 최신순 정렬 (createdAt 기준 내림차순)
170- List <PartyCardResponse > closedParties = cardResponses .stream ()
171- .filter (PartyCardResponse ::getIsClosed )
172- .sorted ((a , b ) -> b .getCreatedAt ().compareTo (a .getCreatedAt ()))
173- .collect (Collectors .toList ());
139+ // 마감된 파티 정렬
140+ List <PartyCardResponse > closedParties = cardResponses .stream ()
141+ .filter (PartyCardResponse ::getIsClosed )
142+ .sorted (getComparator (request .getSortType ()))
143+ .collect (Collectors .toList ());
174144
175145 return PartyListResponse .builder ()
176146 .activeParties (activeParties )
@@ -272,7 +242,7 @@ private String getDefaultImageIfEmpty(List<String> images, PartyCategory categor
272242 };
273243 }
274244
275- private PartyCardResponse convertToCardResponse (Party party , Long userId ,
245+ private PartyCardResponse convertToCardResponse (Party party ,
276246 LocalDateTime createdAt ) {
277247 int pricePerPerson = party .getPrice () / party .getMaxParticipants ();
278248 String participantStatus = party .getCurrentParticipants () + "/"
@@ -675,6 +645,40 @@ private String formatDistanceIfExists(Double distance) {
675645 return distance != null ? DistanceCalculator .formatDistance (distance ):null ;
676646 }
677647
648+ //카테고리에 따라 파티 조회
649+ private List <Party > fetchPartiesByLocation (User user , PartyListRequest request ) {
650+ if (user == null || user .getLocation () == null ) {
651+ return List .of ();
652+ }
653+
654+ String location = user .getLocation ();
655+ PartyCategory category = request .getCategory ();
678656
657+ if (category == PartyCategory .ALL ) {
658+ return partyRepository .findByPickupLocation_Place (location );
659+ } else {
660+ return partyRepository .findByPickupLocation_PlaceAndCategory (location , category );
661+ }
662+ }
663+
664+ // 정렬 기준에 따른 Comparator 반환
665+ private Comparator <PartyCardResponse > getComparator (PartySortType sortType ) {
666+ if (sortType == PartySortType .DISTANCE ) {
667+ // 거리 가까운 순
668+ return Comparator .comparing (PartyCardResponse ::getDistanceKm )
669+ .thenComparing ((a , b ) -> b .getCreatedAt ().compareTo (a .getCreatedAt ()));
670+ } else {
671+ // 최신순 (createdAt 내림차순)
672+ return (a , b ) -> b .getCreatedAt ().compareTo (a .getCreatedAt ());
673+ }
674+ }
675+
676+ // 거리 정보 포함 변환
677+ private PartyCardResponse convertToCardResponseWithDistance (
678+ Party party , Double distance ) {
679+ PartyCardResponse response = convertToCardResponse (party , party .getCreatedAt ());
680+ response .addDistanceKm (distance );
681+ return response ;
682+ }
679683}
680684
0 commit comments