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 @@ -490,10 +490,13 @@ public APIResponse<PartyQueryListResponse> getParty(
schema = @Schema(allowableValues = {"ALL", "DELIVERY", "GROCERY", "HOUSEHOLD"})
)
@RequestParam(defaultValue = "ALL") PartyCategory category,
@RequestParam(required = false, name = "lat") Double userLat,
@RequestParam(required = false, name = "lon") Double userLon,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size
) {
return APIResponse.success(partySearchService.searchParty(q, category, page, size));

return APIResponse.success(partySearchService.searchParty(q, category, page, size, userLat, userLon));
}

@Operation(
Expand All @@ -511,7 +514,7 @@ public APIResponse<List<String>> getRecentLog() {
@Operation(
summary = "ํŠน์ • ์ตœ๊ทผ ๊ฒ€์ƒ‰์–ด ์‚ญ์ œ",
description = """
์ตœ๊ทผ ๊ฒ€์ƒ‰์–ด์—์„œ ํŠน์ • ๊ฒ€์ƒ‰์–ด๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. <br>
์ตœ๊ทผ ๊ฒ€์ƒ‰์–ด์—์„œ ํŠน์ • ๊ฒ€์ƒ‰์–ด๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. <br>
์ด๋•Œ ๊ฒ€์ƒ‰์–ด์— ๋Œ€ํ•œ Id๊ฐ’์€ ์—†๊ณ , ์ตœ๊ทผ ๊ฒ€์ƒ‰์–ด ์ž์ฒด๋ฅผ keyword์— ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
"""
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package ita.tinybite.domain.party.repository;

import io.lettuce.core.dynamic.annotation.Param;
import ita.tinybite.domain.chat.enums.ChatRoomType;
import ita.tinybite.domain.party.entity.Party;
import ita.tinybite.domain.party.enums.PartyCategory;
import java.util.List;
import java.util.Optional;

import ita.tinybite.domain.party.enums.PartyStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
Expand All @@ -25,8 +22,4 @@ public interface PartyRepository extends JpaRepository<Party, Long> {
List<Party> findByPickupLocation_PlaceAndCategory(String place, PartyCategory category);

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

Page<Party> findByTitleContaining(String title, Pageable pageable);

Page<Party> findByTitleContainingAndCategory(String title, PartyCategory category, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ita.tinybite.domain.party.repository;

import ita.tinybite.domain.party.entity.Party;
import ita.tinybite.domain.party.enums.PartyCategory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface PartySearchRepository extends JpaRepository<Party, Long> {

Page<Party> findByTitleContaining(String title, Pageable pageable);

Page<Party> findByTitleContainingAndCategory(String title, PartyCategory category, Pageable pageable);

@Query(value = """
SELECT p.*
FROM party p
WHERE p.title LIKE CONCAT('%', :title, '%')
ORDER BY (6371000 * acos(
cos(radians(:lat)) * cos(radians(p.pickup_latitude))
* cos(radians(p.pickup_longitude) - radians(:lon))
+ sin(radians(:lat)) * sin(radians(p.pickup_latitude))))
""", countQuery = """
SELECT COUNT(*)
FROM party p
WHERE p.title LIKE CONCAT('%', :title, '%')
""", nativeQuery = true)
Page<Party> findByTitleContainingWithDistance(String title, @Param("lat") Double lat, @Param("lon") Double lon, Pageable pageable);

@Query(value = """
SELECT p.*
FROM party p
WHERE p.
ORDER BY (6371000 * acos(
cos(radians(:lat)) * cos(radians(p.pickup_latitude))
* cos(radians(p.pickup_longitude) - radians(:lon))
+ sin(radians(:lat)) * sin(radians(p.pickup_latitude))))
""", countQuery = """
SELECT COUNT(*)
FROM party p
WHERE p.title LIKE CONCAT('%', :title, '%')
""", nativeQuery = true)
Page<Party> findByTitleContainingAndCategoryWithDistance(String q, Double lat, Double lon, PartyCategory category, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import ita.tinybite.domain.party.enums.ParticipantStatus;
import ita.tinybite.domain.party.enums.PartyCategory;
import ita.tinybite.domain.party.repository.PartyParticipantRepository;
import ita.tinybite.domain.party.repository.PartyRepository;
import ita.tinybite.domain.party.repository.PartySearchRepository;
import ita.tinybite.global.util.DistanceCalculator;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -23,7 +24,7 @@
@RequiredArgsConstructor
public class PartySearchService {

private final PartyRepository partyRepository;
private final PartySearchRepository partySearchRepository;
private final PartyParticipantRepository participantRepository;
private final StringRedisTemplate redisTemplate;
private final SecurityProvider securityProvider;
Expand All @@ -35,7 +36,7 @@ private String key(Long userId) {
}

// ํŒŒํ‹ฐ ๊ฒ€์ƒ‰ ์กฐํšŒ
public PartyQueryListResponse searchParty(String q, PartyCategory category, int page, int size) {
public PartyQueryListResponse searchParty(String q, PartyCategory category, int page, int size, Double lat, Double lon) {
Long userId = securityProvider.getCurrentUser().getUserId();

// recent_search:{userId}
Expand All @@ -45,24 +46,49 @@ public PartyQueryListResponse searchParty(String q, PartyCategory category, int
redisTemplate.opsForZSet().add(key, q, System.currentTimeMillis());

Pageable pageable = PageRequest.of(page, size);

// category๊ฐ€ ์—†์„ ์‹œ์—๋Š” ALL๋กœ ์ฒ˜๋ฆฌ
Page<Party> result = (category == null || category == PartyCategory.ALL)
? partyRepository.findByTitleContaining(q, pageable)
: partyRepository.findByTitleContainingAndCategory(q, category, pageable);

List<PartyCardResponse> partyCardResponseList = result.stream()
.map(party -> {
int currentParticipants = participantRepository
.countByPartyIdAndStatus(party.getId(), ParticipantStatus.APPROVED);
return PartyCardResponse.from(party, currentParticipants);
})
.toList();

return PartyQueryListResponse.builder()
.parties(partyCardResponseList)
.hasNext(result.hasNext())
.build();
List<PartyCardResponse> partyCardResponseList;

// ๊ฑฐ๋ฆฌ ์ •๋ณด X
if(lat == null || lon == null) {
// category๊ฐ€ ์—†์„ ์‹œ์—๋Š” ALL๋กœ ์ฒ˜๋ฆฌ
Page<Party> queryResults = (category == null || category == PartyCategory.ALL)
? partySearchRepository.findByTitleContaining(q, pageable)
: partySearchRepository.findByTitleContainingAndCategory(q, category, pageable);

partyCardResponseList = queryResults.stream()
.map(party -> {
int currentParticipants = participantRepository
.countByPartyIdAndStatus(party.getId(), ParticipantStatus.APPROVED);
return PartyCardResponse.from(party, currentParticipants);
})
.toList();

return PartyQueryListResponse.builder()
.parties(partyCardResponseList)
.hasNext(queryResults.hasNext())
.build();
} else {
// ๊ฑฐ๋ฆฌ ์ •๋ณด O (lat, lon)
Page<Party> queryResults = (category == null || category == PartyCategory.ALL)
? partySearchRepository.findByTitleContainingWithDistance(q, lat, lon, pageable)
: partySearchRepository.findByTitleContainingAndCategoryWithDistance(q, lat, lon, category, pageable);

partyCardResponseList = queryResults.stream()
.map(party -> {
int currentParticipants = participantRepository
.countByPartyIdAndStatus(party.getId(), ParticipantStatus.APPROVED);
PartyCardResponse res = PartyCardResponse.from(party, currentParticipants);
Double distance = DistanceCalculator.calculateDistance(lat, lon, party.getPickupLocation().getPickupLatitude(), party.getPickupLocation().getPickupLongitude());
res.addDistanceKm(distance);
return res;
})
.toList();

return PartyQueryListResponse.builder()
.parties(partyCardResponseList)
.hasNext(queryResults.hasNext())
.build();
}
}


Expand Down