Skip to content

Commit bd9c27d

Browse files
committed
feat(CollectionController): add keyword search functionality for user collections
1 parent 69254b1 commit bd9c27d

6 files changed

Lines changed: 66 additions & 24 deletions

File tree

flow-client/src/main/java/com/ligg/flowclient/controller/CollectionController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class CollectionController {
3636
*
3737
* @param subjectType 条目大类,默认 2(动画)
3838
* @param type 收藏状态,默认 2(看过)
39+
* @param keyword 条目名称关键字,可匹配原名或中文名
3940
* @param limit 每页条数,默认 20
4041
* @param offset 偏移量,默认 0
4142
*/
@@ -44,13 +45,14 @@ public Result<UserCollectionsVo> getMeCollections(
4445
@RequestAttribute(AuthorizationInterceptor.ACCESS_TOKEN_REQUEST_ATTRIBUTE) String flowAccessToken,
4546
@RequestParam(defaultValue = "2") int subjectType,
4647
@RequestParam(defaultValue = "2") int type,
48+
@RequestParam(required = false) String keyword,
4749
@RequestParam(defaultValue = "20") int limit,
4850
@RequestParam(defaultValue = "0") int offset) {
4951
Long userId = jwtTokenService.validateAccessToken(flowAccessToken);
5052
UserOauthEntity bangumiOauth = bangumiOAuthTokenService.findBangumiOauth(userId);
5153
String bangumiAccessToken = bangumiOauth != null ? bangumiOauth.getAccessToken() : null;
5254
UserCollectionsVo vo = userBgmCollectionService.listMyCollections(
53-
bangumiAccessToken, userId, subjectType, type, limit, offset);
55+
bangumiAccessToken, userId, subjectType, type, keyword, limit, offset);
5456
return Result.success(ResponseCode.SUCCESS, vo);
5557
}
5658

flow-client/src/main/java/com/ligg/flowclient/mapper/UserBgmCollectionMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ public interface UserBgmCollectionMapper extends BaseMapper<UserBgmCollectionEnt
1414

1515
long countByUserFilter(@Param("userId") Long userId,
1616
@Param("type") int type,
17-
@Param("subjectType") int subjectType);
17+
@Param("subjectType") int subjectType,
18+
@Param("keyword") String keyword);
1819

1920
List<UserBgmCollectionRow> selectPageByUserFilter(@Param("userId") Long userId,
2021
@Param("type") int type,
2122
@Param("subjectType") int subjectType,
23+
@Param("keyword") String keyword,
2224
@Param("limit") int limit,
2325
@Param("offset") int offset);
2426

flow-client/src/main/java/com/ligg/flowclient/service/UserBgmCollectionService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ public interface UserBgmCollectionService {
99
* 查询当前登录用户已同步到本地的 Bangumi 收藏列表。
1010
* accessToken 可能为null
1111
*/
12-
UserCollectionsVo listMyCollections(String accessToken, long userId, int subjectType, int type, int limit, int offset);
12+
UserCollectionsVo listMyCollections(
13+
String accessToken,
14+
long userId,
15+
int subjectType,
16+
int type,
17+
String keyword,
18+
int limit,
19+
int offset);
1320

1421
/**
1522
* 更新当前用户对条目的 Bangumi 收藏,并同步写入本地 {@code user_bgm_collection}。

flow-client/src/main/java/com/ligg/flowclient/service/impl/UserBgmCollectionServiceImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,31 @@ public class UserBgmCollectionServiceImpl implements UserBgmCollectionService {
4848
private final ImageBackfillService imageBackfillService;
4949

5050
@Override
51-
public UserCollectionsVo listMyCollections(String accessToken, long userId, int subjectType, int type, int limit, int offset) {
51+
public UserCollectionsVo listMyCollections(
52+
String accessToken,
53+
long userId,
54+
int subjectType,
55+
int type,
56+
String keyword,
57+
int limit,
58+
int offset) {
5259
UserCollectionsVo vo = new UserCollectionsVo();
5360
if (limit <= 0) {
5461
vo.setData(Collections.emptyList());
5562
vo.setTotal(0);
5663
return vo;
5764
}
5865

59-
long total = userBgmCollectionMapper.countByUserFilter(userId, type, subjectType);
66+
String normalizedKeyword = StringUtils.hasText(keyword) ? keyword.trim() : null;
67+
long total = userBgmCollectionMapper.countByUserFilter(userId, type, subjectType, normalizedKeyword);
6068
vo.setTotal((int) total);
6169
if (total == 0) {
6270
vo.setData(Collections.emptyList());
6371
return vo;
6472
}
6573

6674
List<UserBgmCollectionRow> rows = userBgmCollectionMapper.selectPageByUserFilter(
67-
userId, type, subjectType, limit, offset);
75+
userId, type, subjectType, normalizedKeyword, limit, offset);
6876

6977
List<UserCollectionsDto.Item> items = new ArrayList<>(rows.size());
7078
for (UserBgmCollectionRow row : rows) {

flow-client/src/main/resources/mapper/UserBgmCollectionMapper.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66
WHERE c.user_id = #{userId}
77
AND c.type = #{type}
88
AND c.subject_type = #{subjectType}
9+
<if test="keyword != null and keyword != ''">
10+
AND (
11+
s.name LIKE CONCAT('%', #{keyword}, '%')
12+
OR s.name_cn LIKE CONCAT('%', #{keyword}, '%')
13+
)
14+
</if>
915
</sql>
1016

1117
<select id="countByUserFilter" resultType="long">
1218
SELECT COUNT(*)
1319
FROM user_bgm_collection c
20+
<if test="keyword != null and keyword != ''">
21+
LEFT JOIN bangumi_subject s ON s.id = c.subject_id
22+
</if>
1423
<include refid="userFilterWhere"/>
1524
</select>
1625

flow-client/src/test/java/com/ligg/flowclient/controller/FlowUserControllerTest.java renamed to flow-client/src/test/java/com/ligg/flowclient/controller/CollectionControllerTest.java

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import com.ligg.common.response.Result;
44
import com.ligg.common.statuenum.ResponseCode;
55
import com.ligg.common.vo.bangumi.UserCollectionsVo;
6+
import com.ligg.common.entity.UserOauthEntity;
67
import com.ligg.flowclient.service.BangumiOAuthTokenService;
78
import com.ligg.flowclient.service.JwtTokenService;
89
import com.ligg.flowclient.service.UserBgmCollectionService;
910
import com.ligg.flowclient.service.UserBgmCollectionSyncService;
10-
import com.ligg.flowclient.service.UserEpisodeWatchService;
11-
import com.ligg.flowclient.service.UserService;
1211
import org.junit.jupiter.api.Test;
1312
import org.junit.jupiter.api.extension.ExtendWith;
1413
import org.mockito.junit.jupiter.MockitoExtension;
@@ -21,10 +20,7 @@
2120
import static org.mockito.Mockito.when;
2221

2322
@ExtendWith(MockitoExtension.class)
24-
class FlowUserControllerTest {
25-
26-
@org.mockito.Mock
27-
private UserService userService;
23+
class CollectionControllerTest {
2824

2925
@org.mockito.Mock
3026
private UserBgmCollectionService userBgmCollectionService;
@@ -38,27 +34,45 @@ class FlowUserControllerTest {
3834
@org.mockito.Mock
3935
private JwtTokenService jwtTokenService;
4036

41-
@org.mockito.Mock
42-
private UserEpisodeWatchService userEpisodeWatchService;
43-
4437
@Test
4538
void getMeCollections_allowsMissingBangumiOauth() {
46-
FlowUserController controller = new FlowUserController(
47-
userService,
48-
userBgmCollectionService,
49-
bangumiOAuthTokenService,
50-
userBgmCollectionSyncService,
39+
CollectionController controller = new CollectionController(
5140
jwtTokenService,
52-
userEpisodeWatchService);
41+
bangumiOAuthTokenService,
42+
userBgmCollectionService,
43+
userBgmCollectionSyncService);
5344
UserCollectionsVo collections = new UserCollectionsVo();
5445
when(jwtTokenService.validateAccessToken("flow-token")).thenReturn(10L);
5546
when(bangumiOAuthTokenService.findBangumiOauth(10L)).thenReturn(null);
56-
when(userBgmCollectionService.listMyCollections(null, 10L, 2, 2, 20, 0)).thenReturn(collections);
47+
when(userBgmCollectionService.listMyCollections(null, 10L, 2, 2, null, 20, 0)).thenReturn(collections);
48+
49+
Result<UserCollectionsVo> result = controller.getMeCollections("flow-token", 2, 2, null, 20, 0);
50+
51+
assertEquals(ResponseCode.SUCCESS.getCode(), result.getCode());
52+
assertSame(collections, result.getData());
53+
verify(userBgmCollectionService).listMyCollections(isNull(), eq(10L), eq(2), eq(2), isNull(), eq(20), eq(0));
54+
}
55+
56+
@Test
57+
void getMeCollections_passesKeyword() {
58+
CollectionController controller = new CollectionController(
59+
jwtTokenService,
60+
bangumiOAuthTokenService,
61+
userBgmCollectionService,
62+
userBgmCollectionSyncService);
63+
UserOauthEntity oauth = new UserOauthEntity();
64+
oauth.setAccessToken("bangumi-token");
65+
UserCollectionsVo collections = new UserCollectionsVo();
66+
when(jwtTokenService.validateAccessToken("flow-token")).thenReturn(10L);
67+
when(bangumiOAuthTokenService.findBangumiOauth(10L)).thenReturn(oauth);
68+
when(userBgmCollectionService.listMyCollections(
69+
"bangumi-token", 10L, 2, 2, "葬送", 20, 0)).thenReturn(collections);
5770

58-
Result<UserCollectionsVo> result = controller.getMeCollections("flow-token", 2, 2, 20, 0);
71+
Result<UserCollectionsVo> result = controller.getMeCollections("flow-token", 2, 2, "葬送", 20, 0);
5972

6073
assertEquals(ResponseCode.SUCCESS.getCode(), result.getCode());
6174
assertSame(collections, result.getData());
62-
verify(userBgmCollectionService).listMyCollections(isNull(), eq(10L), eq(2), eq(2), eq(20), eq(0));
75+
verify(userBgmCollectionService).listMyCollections(
76+
eq("bangumi-token"), eq(10L), eq(2), eq(2), eq("葬送"), eq(20), eq(0));
6377
}
6478
}

0 commit comments

Comments
 (0)