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 @@ -72,7 +72,6 @@ public AiRecommendationResponse getCategoryRecommendationsForUser(Long userId) {
log.info("추천 이름 파싱: {}", recommendedNamesByCategory);

String userUuid = findUserUuid(userId);

return new AiRecommendationResponse(
mapToRecommendedSouvenirs(recommendedNamesByCategory, userUuid)
);
Expand Down Expand Up @@ -125,7 +124,7 @@ public AiRecommendationResponse getRecentSouvenirRecommendations(Long userId) {

String userUuid = findUserUuid(userId);
List<Long> souvenirIds = souvenirs.stream().map(Souvenir::getId).toList();
Set<Long> wishlistedIds = wishlistRepository.findSouvenirIdsByUserId(userUuid);
Set<Long> wishlistedIds = wishlistRepository.findSouvenirIdsByUserUserId(userUuid);
Map<Long, Long> wishlistCountMap = wishlistRepository.countBySouvenirIds(souvenirIds);

List<AiRecommendationResponse.RecommendedSouvenir> finalSouvenirs = souvenirs.stream()
Expand Down Expand Up @@ -217,7 +216,7 @@ private List<AiRecommendationResponse.RecommendedSouvenir> mapToRecommendedSouve
.toList();

Map<Long, FileResponse> thumbnailMap = getThumbnails(souvenirIds);
Set<Long> wishlistedIds = wishlistRepository.findSouvenirIdsByUserId(userUuid);
Set<Long> wishlistedIds = wishlistRepository.findSouvenirIdsByUserUserId(userUuid);
Map<Long, Long> wishlistCountMap = wishlistRepository.countBySouvenirIds(souvenirIds);

return souvenirs.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public List<CountryRecommendationDto> getTopCountriesWithTop10Souvenirs(@Nullabl

String userId = extractUserId(authorizationHeader);
Set<Long> wishlistedIds = userId != null
? wishlistRepository.findSouvenirIdsByUserId(userId)
? wishlistRepository.findSouvenirIdsByUserUserId(userId)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge general 추천의 찜 조회 ID를 UUID와 맞추세요

medium: 이 경로의 extractUserId()JwtTokenProvider.getUserIdFromToken()에서 나온 DB PK Long을 문자열로 바꾸지만, 새로 호출하는 findSouvenirIdsByUserUserId()w.user.userId(UUID)를 조회합니다. 인증 사용자가 /api/countries/souvenirs를 호출해도 numeric ID로 UUID 컬럼을 검색해 항상 빈 Set이 되어 isWishlisted가 false/null처럼 내려가므로, 여기와 toDto()의 동일한 호출은 UserRepository로 UUID를 찾거나 새로 추가한 Long 기반 findSouvenirIdsByUserId(Long)를 사용해야 합니다.

Useful? React with 👍 / 👎.

: Collections.emptySet();
Map<Long, Long> wishlistCountMap = wishlistRepository.countBySouvenirIds(allSouvenirIds);

Expand Down Expand Up @@ -116,7 +116,7 @@ private List<GeneralRecommendationDto> toDto(List<Souvenir> souvenirs, @Nullable

String userId = extractUserId(authorizationHeader);
Set<Long> wishlistedIds = userId != null
? wishlistRepository.findSouvenirIdsByUserId(userId)
? wishlistRepository.findSouvenirIdsByUserUserId(userId)
: Collections.emptySet();
Map<Long, Long> wishlistCountMap = wishlistRepository.countBySouvenirIds(souvenirIds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ public SouvenirNearbyListResponse getNearbySouvenirs(
double radiusMeter,
@Nullable String authorizationHeader
) {
String userId = extractUserId(authorizationHeader);
Long userId = extractUserIdAsLong(authorizationHeader);

List<Object[]> results = souvenirRepository.findNearbySouvenirs(latitude, longitude, radiusMeter);

Set<Long> wishlistedIds = userId != null
? wishlistRepository.findSouvenirIdsByUserId(userId)
String userUuid = userId != null ? findUserUuid(userId) : null;
Set<Long> wishlistedIds = userUuid != null
? wishlistRepository.findSouvenirIdsByUserUserId(userUuid)
: Collections.emptySet();

List<SouvenirNearbyResponse> list = results.stream()
Expand All @@ -79,11 +80,12 @@ public SouvenirDetailResponse getSouvenir(
Long souvenirId,
@Nullable String authorizationHeader
) {
String userId = extractUserId(authorizationHeader);
Long userId = extractUserIdAsLong(authorizationHeader);
Souvenir souvenir = findSouvenirById(souvenirId);
List<FileResponse> files = getFiles(souvenirId);
boolean isOwned = souvenir.isOwnedBy(userId);
boolean isWishlisted = userId != null && wishlistRepository.existsByUserUserIdAndSouvenirId(userId, souvenirId);
String userUuid = userId != null ? findUserUuid(userId) : null;
boolean isOwned = userId != null && souvenir.isOwnedBy(userUuid);
boolean isWishlisted = userUuid != null && wishlistRepository.existsByUserUserIdAndSouvenirId(userUuid, souvenirId);
long wishlistCount = wishlistRepository.countBySouvenirId(souvenirId);
PriceResponse priceResponse = createPriceResponse(souvenir);
return SouvenirDetailResponse.of(souvenir, files, isOwned, isWishlisted, wishlistCount, priceResponse);
Expand Down Expand Up @@ -220,7 +222,7 @@ private PriceResponse createPriceResponse(Souvenir souvenir) {
}

@Nullable
private String extractUserId(@Nullable String authorizationHeader) {
private Long extractUserIdAsLong(@Nullable String authorizationHeader) {
if (hasNoAuthorizationHeader(authorizationHeader)) {
return null;
}
Expand All @@ -235,7 +237,13 @@ private String extractUserId(@Nullable String authorizationHeader) {
return null;
}

return parseUserIdFromToken(token);
return parseUserIdFromTokenAsLong(token);
}

@Nullable
private String extractUserId(@Nullable String authorizationHeader) {
Long userId = extractUserIdAsLong(authorizationHeader);
return userId != null ? String.valueOf(userId) : null;
}

private boolean hasNoAuthorizationHeader(String authorizationHeader) {
Expand All @@ -255,16 +263,21 @@ private boolean isEmptyToken(String token) {
}

@Nullable
private String parseUserIdFromToken(String token) {
private Long parseUserIdFromTokenAsLong(String token) {
try {
Long userId = jwtTokenProvider.getUserIdFromToken(token);
return userId != null ? String.valueOf(userId) : null;
return jwtTokenProvider.getUserIdFromToken(token);
} catch (Exception e) {
log.debug("Failed to parse token", e);
return null;
}
}

private String findUserUuid(Long userId) {
return userRepository.findById(userId)
.map(User::getUserId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
}

private void requireUserId(Long userId) {
if (isNullUserId(userId)) {
throw new BusinessException(ErrorCode.UNAUTHORIZED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public MySouvenirListResponse getMySouvenirs(Long userId, int page, int size) {
.toList();

Map<Long, FileResponse> thumbnailMap = getThumbnails(souvenirIds);
Set<Long> wishlistedIds = wishlistRepository.findSouvenirIdsByUserId(user.getUserId());
Set<Long> wishlistedIds = wishlistRepository.findSouvenirIdsByUserUserId(user.getUserId());
Map<Long, Long> wishlistCountMap = wishlistRepository.countBySouvenirIds(souvenirIds);

Page<MySouvenirResponse> responsePage = souvenirPage.map(souvenir -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
@Repository
public interface WishlistRepository extends JpaRepository<Wishlist, Long> {

boolean existsByUserIdAndSouvenirId(Long userId, Long souvenirId);

boolean existsByUserUserIdAndSouvenirId(String userId, Long souvenirId);

@Modifying
Expand All @@ -29,8 +31,11 @@ public interface WishlistRepository extends JpaRepository<Wishlist, Long> {
)
Page<Wishlist> findByUserIdOrderByCreatedAtDesc(@Param("userId") Long userId, Pageable pageable);

@Query("SELECT w.souvenir.id FROM Wishlist w WHERE w.user.id = :userId")
Set<Long> findSouvenirIdsByUserId(@Param("userId") Long userId);

@Query("SELECT w.souvenir.id FROM Wishlist w WHERE w.user.userId = :userId")
Set<Long> findSouvenirIdsByUserId(@Param("userId") String userId);
Set<Long> findSouvenirIdsByUserUserId(@Param("userId") String userId);

@Query("SELECT COUNT(w) FROM Wishlist w WHERE w.souvenir.id = :souvenirId")
long countBySouvenirId(@Param("souvenirId") Long souvenirId);
Expand Down
Loading