Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -380,25 +380,22 @@ public ResponseEntity<PartyListResponse> getPartyList(
) {
Double lat = null;
Double lon = null;
// 거리순 정렬 시 위치 정보 검증
if (sortType == PartySortType.DISTANCE) {
if (userLat == null || userLon == null) {
throw new IllegalArgumentException("거리순 정렬을 위해서는 현재 위치 정보가 필요합니다.");
}

try {
lat = Double.parseDouble(userLat);
lon = Double.parseDouble(userLon);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
String.format("위치 정보 형식이 올바르지 않습니다. userLat: %s, userLon: %s", userLat, userLon)
);
}

// 위도/경도 범위 검증
if (lat < -90 || lat > 90 || lon < -180 || lon > 180) {
throw new IllegalArgumentException("위도/경도 값이 유효한 범위를 벗어났습니다.");
}
if (userLat == null || userLon == null) {
throw new IllegalArgumentException("거리순 정렬을 위해서는 현재 위치 정보가 필요합니다.");
}

try {
lat = Double.parseDouble(userLat);
lon = Double.parseDouble(userLon);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
String.format("위치 정보 형식이 올바르지 않습니다. userLat: %s, userLon: %s", userLat, userLon)
);
}

// 위도/경도 범위 검증
if (lat < -90 || lat > 90 || lon < -180 || lon > 180) {
throw new IllegalArgumentException("위도/경도 값이 유효한 범위를 벗어났습니다.");
}

PartyListRequest request = PartyListRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class PartyDetailResponse {
private String title;
private PartyCategory category;
private String timeAgo;
private String town;

// 파티장 정보
private HostInfo host;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/ita/tinybite/domain/party/entity/Party.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class Party {
@Column(length = 500)
private String link; // 링크 (예: 배달앱 링크)

@Column(length = 30)
private String town;

@Embedded
@Column
private PickupLocation pickupLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public interface PartyRepository extends JpaRepository<Party, Long> {
List<Party> findByPickupLocation_PlaceAndCategory(String place, PartyCategory category);

List<Party> findByHostUserIdAndStatus(Long userId, PartyStatus partyStatus);

List<Party> findByTown(String location);

List<Party> findByTownAndCategory(String location, PartyCategory category);
}
35 changes: 21 additions & 14 deletions src/main/java/ita/tinybite/domain/party/service/PartyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public Long createParty(Long userId, PartyCreateRequest request) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다"));

String myTown = getMyTown(request.getPickupLocation().getPickupLatitude(), request.getPickupLocation().getPickupLongitude());

// 카테고리별 유효성 검증
validateProductLink(request.getCategory(), request.getProductLink());

Expand All @@ -78,6 +80,7 @@ public Long createParty(Long userId, PartyCreateRequest request) {
.category(request.getCategory())
.price(request.getTotalPrice())
.maxParticipants(request.getMaxParticipants())
.town(myTown)
.pickupLocation(PickupLocation.builder()
.place(request.getPickupLocation().getPlace())
.pickupLatitude(request.getPickupLocation().getPickupLatitude())
Expand Down Expand Up @@ -140,13 +143,17 @@ public PartyListResponse getPartyList(Long userId, PartyListRequest request) {
int page = request.getPage() != null ? request.getPage() : 0;
int size = request.getSize() != null ? request.getSize() : 20;

String myTown = getMyTown(request.getUserLat(),request.getUserLon());

// 동네 기준으로 파티 조회
List<Party> parties = fetchPartiesByLocation(user, request);
List<Party> parties = fetchPartiesByTown(user, request, myTown);

// PartyCardResponse로 변환
List<PartyCardResponse> cardResponses = parties.stream()
.map(party -> {
if (request.getSortType() == PartySortType.DISTANCE) {
// 위치 정보가 있으면 항상 거리 계산
if (request.getUserLat() != null && request.getUserLon() != null
&& party.getPickupLocation() != null) {
double distance = DistanceCalculator.calculateDistance(
request.getUserLat(),
request.getUserLon(),
Expand All @@ -155,21 +162,22 @@ public PartyListResponse getPartyList(Long userId, PartyListRequest request) {
);
return convertToCardResponseWithDistance(party, distance);
}
return convertToCardResponse(party,party.getCreatedAt());
return convertToCardResponse(party, party.getCreatedAt());
})
.toList();


// 진행 중 파티 정렬
List<PartyCardResponse> activeParties = cardResponses.stream()
.filter(p -> !p.getIsClosed())
.sorted(getComparator(request.getSortType()))
.collect(Collectors.toList());
.toList();

// 마감된 파티 정렬
List<PartyCardResponse> closedParties = cardResponses.stream()
.filter(PartyCardResponse::getIsClosed)
.sorted(getComparator(request.getSortType()))
.collect(Collectors.toList());
.toList();

// 진행 중 + 마감된 파티 합치기 (진행 중이 먼저)
List<PartyCardResponse> allParties = new ArrayList<>();
Expand Down Expand Up @@ -380,17 +388,12 @@ private PartyDetailResponse convertToDetailResponse(Party party, double distance
int currentCount = party.getCurrentParticipants();
int pricePerPerson = party.getPrice() / party.getMaxParticipants();

// 이미지 파싱
// List<String> images = new ArrayList<>();
// if (party.getImages() != null && !party.getImages().isEmpty()) {
// images = List.of(party.getImages());
// }

return PartyDetailResponse.builder()
.partyId(party.getId())
.title(party.getTitle())
.category(party.getCategory())
.timeAgo(party.getTimeAgo())
.town(party.getTown())
.host(HostInfo.builder()
.userId(party.getHost().getUserId())
.nickname(party.getHost().getNickname())
Expand Down Expand Up @@ -805,7 +808,7 @@ private String formatDistanceIfExists(Double distance) {
}

//카테고리에 따라 파티 조회
private List<Party> fetchPartiesByLocation(User user, PartyListRequest request) {
private List<Party> fetchPartiesByTown(User user, PartyListRequest request,String myTown) {
if (user == null || user.getLocation() == null) {
return List.of();
}
Expand All @@ -814,9 +817,9 @@ private List<Party> fetchPartiesByLocation(User user, PartyListRequest request)
PartyCategory category = request.getCategory();

if (category == PartyCategory.ALL) {
return partyRepository.findByPickupLocation_Place(location);
return partyRepository.findByTown(location);
} else {
return partyRepository.findByPickupLocation_PlaceAndCategory(location, category);
return partyRepository.findByTownAndCategory(location, category);
}
}

Expand All @@ -839,5 +842,9 @@ private PartyCardResponse convertToCardResponseWithDistance(
response.addDistanceKm(distance);
return response;
}

private String getMyTown(Double pickupLatitude, Double pickupLongitude) {
return locationService.getLocation(Double.toString(pickupLatitude), Double.toString(pickupLongitude));
}
}