feat(playlist): 增加歌单收藏和评论功能
- 在 CommentServiceImpl 中添加歌单评论相关逻辑 - 在 PlaylistServiceImpl 中实现歌单收藏和取消收藏功能 - 在 PlaylistDetail.vue 中显示歌单的收藏数和评论数 - 更新数据库模型,在 Singer 实体中添加收藏数和评论数字段
This commit is contained in:
@@ -48,6 +48,18 @@ public class Singer {
|
||||
@JoinColumn(name = "type_id")
|
||||
private SingerType type;
|
||||
|
||||
/**
|
||||
* 收藏数
|
||||
*/
|
||||
@Column
|
||||
private Integer collectCount;
|
||||
|
||||
/**
|
||||
* 评论数
|
||||
*/
|
||||
@Column
|
||||
private Integer commentCount;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.test.musichouduan.entity.User;
|
||||
import com.test.musichouduan.exception.BusinessException;
|
||||
import com.test.musichouduan.repository.CommentRepository;
|
||||
import com.test.musichouduan.repository.MusicRepository;
|
||||
import com.test.musichouduan.repository.PlaylistRepository;
|
||||
import com.test.musichouduan.repository.UserRepository;
|
||||
import com.test.musichouduan.service.CommentService;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate; // 添加导入
|
||||
@@ -44,6 +45,9 @@ public class CommentServiceImpl implements CommentService {
|
||||
@Autowired
|
||||
private MusicRepository musicRepository;
|
||||
|
||||
@Autowired
|
||||
private PlaylistRepository playlistRepository;
|
||||
|
||||
@Autowired
|
||||
private SimpMessagingTemplate messagingTemplate; // 注入 SimpMessagingTemplate
|
||||
|
||||
@@ -121,6 +125,12 @@ public class CommentServiceImpl implements CommentService {
|
||||
musicRepository.save(music);
|
||||
}
|
||||
});
|
||||
} else if (savedComment.getTargetType() == 2) { // 2 代表歌单
|
||||
playlistRepository.findById(savedComment.getTargetId()).ifPresent(playlist -> {
|
||||
long commentCount = commentRepository.countByTargetIdAndTargetType(savedComment.getTargetId(), 2);
|
||||
playlist.setCommentCount((int) commentCount);
|
||||
playlistRepository.save(playlist);
|
||||
});
|
||||
}
|
||||
|
||||
// 将新评论转换为DTO
|
||||
@@ -153,6 +163,21 @@ public class CommentServiceImpl implements CommentService {
|
||||
// 删除评论
|
||||
commentRepository.delete(comment);
|
||||
|
||||
// 更新相关实体的评论数
|
||||
if (comment.getTargetType() == 1) {
|
||||
musicRepository.findById(comment.getTargetId()).ifPresent(music -> {
|
||||
long commentCount = commentRepository.countByTargetIdAndTargetType(comment.getTargetId(), 1);
|
||||
music.setCommentCount((int) commentCount);
|
||||
musicRepository.save(music);
|
||||
});
|
||||
} else if (comment.getTargetType() == 2) {
|
||||
playlistRepository.findById(comment.getTargetId()).ifPresent(playlist -> {
|
||||
long commentCount = commentRepository.countByTargetIdAndTargetType(comment.getTargetId(), 2);
|
||||
playlist.setCommentCount((int) commentCount);
|
||||
playlistRepository.save(playlist);
|
||||
});
|
||||
}
|
||||
|
||||
// 如果是父评论,同时删除子评论
|
||||
if (comment.getParentId() == null) {
|
||||
List<Comment> children = commentRepository.findByParentIdOrderByCreateTimeAsc(id);
|
||||
|
||||
@@ -275,6 +275,10 @@ public class PlaylistServiceImpl implements PlaylistService {
|
||||
// 收藏歌单
|
||||
userPlaylistRepository.collectPlaylist(user.getId(), playlist.getId());
|
||||
|
||||
// 更新收藏数
|
||||
playlist.setCollectCount(playlist.getCollectCount() + 1);
|
||||
playlistRepository.save(playlist);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -298,6 +302,10 @@ public class PlaylistServiceImpl implements PlaylistService {
|
||||
// 取消收藏歌单
|
||||
userPlaylistRepository.uncollectPlaylist(user.getId(), playlist.getId());
|
||||
|
||||
// 更新收藏数
|
||||
playlist.setCollectCount(playlist.getCollectCount() - 1);
|
||||
playlistRepository.save(playlist);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -141,6 +141,12 @@ const toggleCollect = async () => {
|
||||
const res = await toggleCollectPlaylist(playlistId.value)
|
||||
if (res.code === 200) {
|
||||
isCollected.value = !isCollected.value
|
||||
// 更新收藏数
|
||||
if (isCollected.value) {
|
||||
playlist.value.collectCount++
|
||||
} else {
|
||||
playlist.value.collectCount--
|
||||
}
|
||||
ElMessage.success(isCollected.value ? '收藏成功' : '已取消收藏')
|
||||
} else {
|
||||
ElMessage.error(res.message || '操作失败')
|
||||
@@ -163,6 +169,8 @@ const deleteComment = async (commentId) => {
|
||||
const res = await apiDeleteComment(commentId)
|
||||
if (res.code === 200) {
|
||||
comments.value = comments.value.filter(c => c.id !== commentId)
|
||||
// 更新评论数
|
||||
playlist.value.commentCount--
|
||||
ElMessage.success('评论已删除')
|
||||
} else {
|
||||
ElMessage.error(res.message || '删除失败')
|
||||
@@ -256,6 +264,8 @@ const submitComment = async (isReply = false) => {
|
||||
commentContent.value = ''
|
||||
}
|
||||
ElMessage.success('评论已发送')
|
||||
// 更新评论数
|
||||
playlist.value.commentCount++
|
||||
} else {
|
||||
ElMessage.error(res.message || '评论失败')
|
||||
}
|
||||
@@ -388,6 +398,11 @@ onUnmounted(() => {
|
||||
</div>
|
||||
<div class="playlist-time">创建时间:{{ playlist.createTime }}</div>
|
||||
<div class="playlist-count">播放次数:{{ formatPlayCount(playlist.playCount) }}</div>
|
||||
<div class="playlist-stats">
|
||||
<span>收藏数:{{ playlist.collectCount }}</span>
|
||||
<el-divider direction="vertical" />
|
||||
<span>评论数:{{ playlist.commentCount }}</span>
|
||||
</div>
|
||||
<div class="playlist-desc">{{ playlist.description }}</div>
|
||||
|
||||
<div class="playlist-actions">
|
||||
@@ -488,7 +503,7 @@ onUnmounted(() => {
|
||||
></el-empty>
|
||||
</el-tab-pane>
|
||||
|
||||
<el-tab-pane label="评论">
|
||||
<el-tab-pane :label="`评论(${playlist.commentCount || 0})`">
|
||||
<!-- 评论输入框 -->
|
||||
<div class="comment-input">
|
||||
<el-input
|
||||
|
||||
Reference in New Issue
Block a user