Skip to content

Commit eaff448

Browse files
authored
Merge pull request #176 from dnd-side-project/refactor/first-sprint
fix(BrowsePlaylist): 둘러보기 로직 변경
2 parents 2987202 + 0863a5b commit eaff448

File tree

14 files changed

+135
-709
lines changed

14 files changed

+135
-709
lines changed

main-server/src/main/java/com/example/demo/domain/browse/controller/BrowsePlaylistController.java

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.example.demo.domain.browse.controller;
22

3-
import com.example.demo.domain.browse.dto.BrowsePlaylistCursor;
4-
import com.example.demo.domain.browse.dto.BrowsePlaylistDto;
53
import com.example.demo.domain.browse.dto.PlaylistViewCountDto;
64
import com.example.demo.domain.browse.service.BrowsePlaylistService;
75
import com.example.demo.domain.browse.service.BrowseViewCountService;
@@ -34,43 +32,23 @@ public class BrowsePlaylistController {
3432
private final BrowsePlaylistService browsePlaylistService;
3533
private final BrowseViewCountService browseViewCountService;
3634

35+
@GetMapping
3736
@Operation(
38-
summary = "셔플된 플레이리스트 목록 조회 (둘러보기)",
39-
description = """
40-
사용자의 Redis에 캐싱된 셔플된 둘러보기(BrowsePlaylist) 목록을 커서 기반으로 조회합니다.
41-
각 유저는 매일 새벽 3시에 셔플된 position 기반의 카드 목록을 가지며, position과 cardId를 함께 사용해 커서 페이징합니다.
42-
[Fallback 처리 안내]
43-
- 신규 가입자 등 캐시 데이터가 없는 경우: BrowsePlaylist 테이블의 ID 1~5번 중 하나를 무작위로 선택하여 반환합니다.
44-
- 이 경우 nextCursor는 null입니다.
45-
"""
37+
summary = "둘러보기 알고리즘",
38+
description = "둘러보기 알고리즘을 통해 선별한 플레이리스트 ID 목록을 커서 방식으로 가져옵니다"
4639
)
47-
@GetMapping
48-
public ResponseEntity<CursorPageResponse<BrowsePlaylistDto, BrowsePlaylistCursor>> browsePlaylists(
40+
public ResponseEntity<CursorPageResponse<Long, Long>> browsePlaylists(
4941
@Parameter(hidden = true)
5042
@AuthenticationPrincipal CustomUserDetails user,
5143

52-
@Parameter(
53-
description = "커서 position. 해당 position부터 이후 카드가 조회됩니다.",
54-
example = "2"
55-
)
56-
@RequestParam(required = false) Integer cursorPosition,
57-
58-
@Parameter(
59-
description = "커서 cardId. 같은 position 내 카드 중 이 ID 이후의 카드부터 조회됩니다.",
60-
example = "1"
61-
)
62-
@RequestParam(required = false) Long cursorCardId,
44+
@Parameter(description = "커서 id(=마지막 playlistId). 이 ID 이후부터 조회", example = "100")
45+
@RequestParam(required = false) Long cursorId,
6346

64-
@Parameter(
65-
description = "한 페이지에서 가져올 카드 수 (기본값: 20)",
66-
example = "20"
67-
)
47+
@Parameter(description = "한 페이지 개수(기본 20)", example = "10")
6848
@RequestParam(defaultValue = "20") int size
6949
) {
70-
CursorPageResponse<BrowsePlaylistDto, BrowsePlaylistCursor> shuffledPlaylists = browsePlaylistService.getShuffledPlaylists(
71-
user.getId(), cursorPosition, cursorCardId, size
72-
);
73-
return ResponseEntity.ok(shuffledPlaylists);
50+
var resp = browsePlaylistService.getShuffledPlaylistIds(user.getId(), cursorId, size);
51+
return ResponseEntity.ok(resp);
7452
}
7553

7654

main-server/src/main/java/com/example/demo/domain/browse/dto/BrowsePlaylistCursor.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

main-server/src/main/java/com/example/demo/domain/browse/dto/BrowsePlaylistDto.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

main-server/src/main/java/com/example/demo/domain/browse/dto/BrowseResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
public record BrowseResponse(
88

99
@Schema(description = "플레이리스트 목록")
10-
List<BrowsePlaylistDto> playlists
10+
List<Long> playlists
1111
) {}

main-server/src/main/java/com/example/demo/domain/browse/dto/CreatorDto.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

main-server/src/main/java/com/example/demo/domain/browse/entity/BrowsePlaylistCard.java

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,54 @@
11
package com.example.demo.domain.browse.repository;
22

3-
import com.example.demo.domain.browse.entity.BrowsePlaylistCard;
3+
import com.example.demo.domain.playlist.entity.Playlist;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
57

8+
import java.util.List;
69

7-
public interface BrowsePlaylistRepository extends JpaRepository<BrowsePlaylistCard, Long>, BrowsePlaylistRepositoryCustom {
8-
void deleteByUserId(String userId);
910

10-
long countByUserId(String userId);
11+
public interface BrowsePlaylistRepository extends JpaRepository<Playlist, Long> {
12+
13+
@Query(value = """
14+
SELECT p.id
15+
FROM playlist p
16+
WHERE p.is_public = TRUE
17+
AND p.user_id <> :userId
18+
ORDER BY
19+
CAST(CONV(SUBSTR(SHA2(CONCAT(:userId, :seedKey, p.id), 256), 1, 16), 16, 10) AS UNSIGNED) ASC,
20+
p.id ASC
21+
LIMIT :limitPlusOne
22+
""", nativeQuery = true)
23+
List<Long> findFirstPageIdsShuffledExcludeMine(
24+
@Param("userId") String userId,
25+
@Param("seedKey") String seedKey,
26+
@Param("limitPlusOne") int limitPlusOne
27+
);
28+
29+
@Query(value = """
30+
SELECT p.id
31+
FROM playlist p
32+
WHERE p.is_public = TRUE
33+
AND p.user_id <> :userId
34+
AND (
35+
CAST(CONV(SUBSTR(SHA2(CONCAT(:userId, :seedKey, p.id), 256), 1, 16), 16, 10) AS UNSIGNED)
36+
> CAST(CONV(SUBSTR(SHA2(CONCAT(:userId, :seedKey, :cursorId), 256), 1, 16), 16, 10) AS UNSIGNED)
37+
OR (
38+
CAST(CONV(SUBSTR(SHA2(CONCAT(:userId, :seedKey, p.id), 256), 1, 16), 16, 10) AS UNSIGNED)
39+
= CAST(CONV(SUBSTR(SHA2(CONCAT(:userId, :seedKey, :cursorId), 256), 1, 16), 16, 10) AS UNSIGNED)
40+
AND p.id > :cursorId
41+
)
42+
)
43+
ORDER BY
44+
CAST(CONV(SUBSTR(SHA2(CONCAT(:userId, :seedKey, p.id), 256), 1, 16), 16, 10) AS UNSIGNED) ASC,
45+
p.id ASC
46+
LIMIT :limitPlusOne
47+
""", nativeQuery = true)
48+
List<Long> findNextPageIdsShuffledExcludeMine(
49+
@Param("userId") String userId,
50+
@Param("seedKey") String seedKey,
51+
@Param("cursorId") Long cursorId,
52+
@Param("limitPlusOne") int limitPlusOne
53+
);
1154
}

main-server/src/main/java/com/example/demo/domain/browse/repository/BrowsePlaylistRepositoryCustom.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)