From 9557714213d0e2638ba0f935a61a021f94cd60f8 Mon Sep 17 00:00:00 2001 From: Gary Date: Sun, 25 Feb 2024 02:22:12 +0800 Subject: [PATCH] 1.fix collect status when check others --- .../music/controller/JournalController.java | 14 +++++++++++++- .../luoo/music/controller/SongController.java | 17 ++++++++++++++--- .../com/luoo/music/dao/UserCollectInfoDao.java | 5 ++++- .../music/service/UserCollectInfoService.java | 6 ++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java index 8ee27f4..036d8cd 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Tag; @@ -135,12 +136,23 @@ public class JournalController { return Result.success(new PageResult(0L, Collections.emptyList())); } List pageList = journalService.orderByField(objectIds); - Set journalCollectSet = new HashSet<>(objectIds); + UserLoginDto user = jwtUtil.getUserLoginDto(authorization); + Set journalCollectSet = getMyCollectSet(user.getUserId(),queryReq.getUserId(),objectIds); List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } + private Set getMyCollectSet(String loginUser, String checkUser, List objectIds) { + if(loginUser.equals(checkUser)) { + return new HashSet<>(objectIds); + } + List myObjectIds=userCollectInfoService.findByUserIdAndCollectTypeAndObjectIdIn(loginUser,CollectTypeEnum.JOURNAL,objectIds); + if(CollectionUtils.isEmpty(myObjectIds)) { + return Collections.emptySet(); + } + return new HashSet<>(myObjectIds); + } private List getObjectIds(CollectQueryReq queryReq) { if(null==queryReq.getPageNum()||queryReq.getPageNum()<1||null==queryReq.getPageSize()||queryReq.getPageSize()<1) { return userCollectInfoService.findByUserIdAndCollectType(queryReq.getUserId(), CollectTypeEnum.JOURNAL); diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index 18467e8..4e6e967 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -25,6 +25,7 @@ import util.JwtUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import java.util.Collections; @@ -93,13 +94,23 @@ public class SongController { return Result.success(new PageResult(0L, Collections.emptyList())); } List songs = songInfoService.orderByField(objectIds); - - Set songCollectSet = new HashSet<>(objectIds); + + UserLoginDto user = jwtUtil.getUserLoginDto(authorization); + Set songCollectSet = getMyCollectSet(user.getUserId(),queryReq.getUserId(),objectIds); List results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(results.size()), results)); } - + private Set getMyCollectSet(String loginUser, String checkUser, List objectIds) { + if(loginUser.equals(checkUser)) { + return new HashSet<>(objectIds); + } + List myObjectIds=userCollectInfoService.findByUserIdAndCollectTypeAndObjectIdIn(loginUser,CollectTypeEnum.SONG,objectIds); + if(CollectionUtils.isEmpty(myObjectIds)) { + return Collections.emptySet(); + } + return new HashSet<>(myObjectIds); + } private List getSongIds(CollectQueryReq queryReq) { if(null==queryReq.getPageNum()||queryReq.getPageNum()<1||null==queryReq.getPageSize()||queryReq.getPageSize()<1) { return userCollectInfoService.findByUserIdAndCollectType(queryReq.getUserId(), CollectTypeEnum.SONG); diff --git a/luoo_music/src/main/java/com/luoo/music/dao/UserCollectInfoDao.java b/luoo_music/src/main/java/com/luoo/music/dao/UserCollectInfoDao.java index ddb2214..7fcbc5c 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/UserCollectInfoDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/UserCollectInfoDao.java @@ -1,6 +1,5 @@ package com.luoo.music.dao; - import java.util.List; import org.springframework.data.domain.Pageable; @@ -11,5 +10,9 @@ import com.luoo.music.pojo.UserCollectInfo; public interface UserCollectInfoDao extends MongoRepository { List findByUserIdAndCollectTypeOrderByCreateTimeDesc(String userId, Integer collectType, Pageable pageable); + List findByUserIdAndCollectTypeOrderByCreateTimeDesc(String userId, Integer collectType); + + List findByUserIdAndCollectTypeAndObjectIdIn(String userId, Integer collectType, + List objectIds); } diff --git a/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java b/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java index cddbb8e..8602498 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java @@ -56,4 +56,10 @@ public class UserCollectInfoService { return userCollectInfoDao.findByUserIdAndCollectTypeOrderByCreateTimeDesc(userId, collectTypeEnum.getType()).stream().map(UserCollectInfo::getObjectId).collect(Collectors.toList()); } + + public List findByUserIdAndCollectTypeAndObjectIdIn(String userId, CollectTypeEnum collectTypeEnum, + List objectIds) { + return userCollectInfoDao.findByUserIdAndCollectTypeAndObjectIdIn(userId, + collectTypeEnum.getType(),objectIds).stream().map(UserCollectInfo::getObjectId).collect(Collectors.toList()); + } }