feat(playlist): 添加歌单收藏时间并优化相关功能
- 在 PlaylistDTO 中添加 collectTime 字段,用于显示收藏时间 - 修改收藏歌单逻辑,记录收藏时间 - 更新取消收藏歌单逻辑,使用 deleteByUserAndPlaylist 方法 - 在获取用户收藏的歌单列表时,添加收藏时间信息 - 重命名 UserCollectPlaylist
This commit is contained in:
@@ -98,4 +98,9 @@ public class PlaylistDTO {
|
|||||||
* 热度分
|
* 热度分
|
||||||
*/
|
*/
|
||||||
private double hotScore;
|
private double hotScore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 收藏时间
|
||||||
|
*/
|
||||||
|
private LocalDateTime collectTime;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,9 +33,8 @@ public class UserCollectPlaylist {
|
|||||||
private Playlist playlist;
|
private Playlist playlist;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建时间
|
* 收藏时间
|
||||||
*/
|
*/
|
||||||
@CreationTimestamp
|
|
||||||
@Column(nullable = false, updatable = false)
|
@Column(nullable = false, updatable = false)
|
||||||
private LocalDateTime createTime;
|
private LocalDateTime collectTime;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,4 +99,6 @@ public interface UserCollectPlaylistRepository extends JpaRepository<UserCollect
|
|||||||
|
|
||||||
@Query("SELECT ucp.playlist FROM UserCollectPlaylist ucp WHERE ucp.user.id = ?1")
|
@Query("SELECT ucp.playlist FROM UserCollectPlaylist ucp WHERE ucp.user.id = ?1")
|
||||||
Page<Playlist> findCollectedPlaylist(Long userId, Pageable pageable);
|
Page<Playlist> findCollectedPlaylist(Long userId, Pageable pageable);
|
||||||
|
|
||||||
|
Optional<UserCollectPlaylist> findByUserIdAndPlaylistId(Long userId, Long playlistId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -372,7 +372,11 @@ public class PlaylistServiceImpl implements PlaylistService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 收藏歌单
|
// 收藏歌单
|
||||||
userCollectPlaylistRepository.collectPlaylist(user.getId(), playlist.getId());
|
com.test.musichouduan.entity.UserCollectPlaylist userCollectPlaylist = new com.test.musichouduan.entity.UserCollectPlaylist();
|
||||||
|
userCollectPlaylist.setUser(user);
|
||||||
|
userCollectPlaylist.setPlaylist(playlist);
|
||||||
|
userCollectPlaylist.setCollectTime(java.time.LocalDateTime.now());
|
||||||
|
userCollectPlaylistRepository.save(userCollectPlaylist);
|
||||||
|
|
||||||
// 更新收藏数
|
// 更新收藏数
|
||||||
playlist.setCollectCount((playlist.getCollectCount() == null ? 0 : playlist.getCollectCount()) + 1);
|
playlist.setCollectCount((playlist.getCollectCount() == null ? 0 : playlist.getCollectCount()) + 1);
|
||||||
@@ -399,7 +403,7 @@ public class PlaylistServiceImpl implements PlaylistService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 取消收藏歌单
|
// 取消收藏歌单
|
||||||
userCollectPlaylistRepository.uncollectPlaylist(user.getId(), playlist.getId());
|
userCollectPlaylistRepository.deleteByUserAndPlaylist(user, playlist);
|
||||||
|
|
||||||
// 更新收藏数
|
// 更新收藏数
|
||||||
playlist.setCollectCount((playlist.getCollectCount() == null ? 0 : playlist.getCollectCount()) - 1);
|
playlist.setCollectCount((playlist.getCollectCount() == null ? 0 : playlist.getCollectCount()) - 1);
|
||||||
@@ -430,7 +434,13 @@ public class PlaylistServiceImpl implements PlaylistService {
|
|||||||
|
|
||||||
// 转换为DTO
|
// 转换为DTO
|
||||||
List<PlaylistDTO> playlistDTOs = playlistPage.getContent().stream()
|
List<PlaylistDTO> playlistDTOs = playlistPage.getContent().stream()
|
||||||
.map(this::convertToDTO)
|
.map(playlist -> {
|
||||||
|
PlaylistDTO dto = convertToDTO(playlist);
|
||||||
|
// 从收藏记录中获取收藏时间
|
||||||
|
userCollectPlaylistRepository.findByUserIdAndPlaylistId(userId, playlist.getId())
|
||||||
|
.ifPresent(ucp -> dto.setCollectTime(ucp.getCollectTime()));
|
||||||
|
return dto;
|
||||||
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
return new PageResult<>(
|
return new PageResult<>(
|
||||||
|
|||||||
Reference in New Issue
Block a user