feat(singer): 添加歌手评论和收藏功能
- 在 CommentServiceImpl 中增加对歌手评论的处理逻辑- 在 SingerServiceImpl 中实现歌手收藏和取消收藏的功能 - 在前端 SingerDetail 组件中添加歌手的评论数、收藏数显示和相关操作
This commit is contained in:
@@ -11,6 +11,7 @@ import com.test.musichouduan.exception.BusinessException;
|
|||||||
import com.test.musichouduan.repository.CommentRepository;
|
import com.test.musichouduan.repository.CommentRepository;
|
||||||
import com.test.musichouduan.repository.MusicRepository;
|
import com.test.musichouduan.repository.MusicRepository;
|
||||||
import com.test.musichouduan.repository.PlaylistRepository;
|
import com.test.musichouduan.repository.PlaylistRepository;
|
||||||
|
import com.test.musichouduan.repository.SingerRepository;
|
||||||
import com.test.musichouduan.repository.UserRepository;
|
import com.test.musichouduan.repository.UserRepository;
|
||||||
import com.test.musichouduan.service.CommentService;
|
import com.test.musichouduan.service.CommentService;
|
||||||
import org.springframework.messaging.simp.SimpMessagingTemplate; // 添加导入
|
import org.springframework.messaging.simp.SimpMessagingTemplate; // 添加导入
|
||||||
@@ -48,6 +49,9 @@ public class CommentServiceImpl implements CommentService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PlaylistRepository playlistRepository;
|
private PlaylistRepository playlistRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SingerRepository singerRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SimpMessagingTemplate messagingTemplate; // 注入 SimpMessagingTemplate
|
private SimpMessagingTemplate messagingTemplate; // 注入 SimpMessagingTemplate
|
||||||
|
|
||||||
@@ -131,6 +135,12 @@ public class CommentServiceImpl implements CommentService {
|
|||||||
playlist.setCommentCount((int) commentCount);
|
playlist.setCommentCount((int) commentCount);
|
||||||
playlistRepository.save(playlist);
|
playlistRepository.save(playlist);
|
||||||
});
|
});
|
||||||
|
} else if (savedComment.getTargetType() == 3) { // 3 代表歌手
|
||||||
|
singerRepository.findById(savedComment.getTargetId()).ifPresent(singer -> {
|
||||||
|
long commentCount = commentRepository.countByTargetIdAndTargetType(savedComment.getTargetId(), 3);
|
||||||
|
singer.setCommentCount((int) commentCount);
|
||||||
|
singerRepository.save(singer);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将新评论转换为DTO
|
// 将新评论转换为DTO
|
||||||
@@ -176,6 +186,12 @@ public class CommentServiceImpl implements CommentService {
|
|||||||
playlist.setCommentCount((int) commentCount);
|
playlist.setCommentCount((int) commentCount);
|
||||||
playlistRepository.save(playlist);
|
playlistRepository.save(playlist);
|
||||||
});
|
});
|
||||||
|
} else if (comment.getTargetType() == 3) {
|
||||||
|
singerRepository.findById(comment.getTargetId()).ifPresent(singer -> {
|
||||||
|
long commentCount = commentRepository.countByTargetIdAndTargetType(comment.getTargetId(), 3);
|
||||||
|
singer.setCommentCount((int) commentCount);
|
||||||
|
singerRepository.save(singer);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果是父评论,同时删除子评论
|
// 如果是父评论,同时删除子评论
|
||||||
@@ -279,6 +295,27 @@ public class CommentServiceImpl implements CommentService {
|
|||||||
// 删除评论
|
// 删除评论
|
||||||
commentRepository.delete(comment);
|
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);
|
||||||
|
});
|
||||||
|
} else if (comment.getTargetType() == 3) {
|
||||||
|
singerRepository.findById(comment.getTargetId()).ifPresent(singer -> {
|
||||||
|
long commentCount = commentRepository.countByTargetIdAndTargetType(comment.getTargetId(), 3);
|
||||||
|
singer.setCommentCount((int) commentCount);
|
||||||
|
singerRepository.save(singer);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 如果是父评论,同时删除子评论
|
// 如果是父评论,同时删除子评论
|
||||||
if (comment.getParentId() == null) {
|
if (comment.getParentId() == null) {
|
||||||
List<Comment> children = commentRepository.findByParentIdOrderByCreateTimeAsc(id);
|
List<Comment> children = commentRepository.findByParentIdOrderByCreateTimeAsc(id);
|
||||||
|
|||||||
@@ -237,6 +237,10 @@ public class SingerServiceImpl implements SingerService {
|
|||||||
// 收藏歌手
|
// 收藏歌手
|
||||||
userSingerRepository.collectSinger(user.getId(), singer.getId());
|
userSingerRepository.collectSinger(user.getId(), singer.getId());
|
||||||
|
|
||||||
|
// 更新收藏数
|
||||||
|
singer.setCollectCount(singer.getCollectCount() + 1);
|
||||||
|
singerRepository.save(singer);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,6 +264,10 @@ public class SingerServiceImpl implements SingerService {
|
|||||||
// 取消收藏歌手
|
// 取消收藏歌手
|
||||||
userSingerRepository.uncollectSinger(user.getId(), singer.getId());
|
userSingerRepository.uncollectSinger(user.getId(), singer.getId());
|
||||||
|
|
||||||
|
// 更新收藏数
|
||||||
|
singer.setCollectCount(singer.getCollectCount() - 1);
|
||||||
|
singerRepository.save(singer);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -183,6 +183,12 @@ const toggleCollect = async () => {
|
|||||||
const res = await toggleCollectSinger(singerId.value)
|
const res = await toggleCollectSinger(singerId.value)
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
isCollected.value = !isCollected.value
|
isCollected.value = !isCollected.value
|
||||||
|
// 更新收藏数
|
||||||
|
if (isCollected.value) {
|
||||||
|
singer.value.collectCount++
|
||||||
|
} else {
|
||||||
|
singer.value.collectCount--
|
||||||
|
}
|
||||||
ElMessage.success(isCollected.value ? '收藏成功' : '已取消收藏')
|
ElMessage.success(isCollected.value ? '收藏成功' : '已取消收藏')
|
||||||
} else {
|
} else {
|
||||||
ElMessage.error(res.message || '操作失败')
|
ElMessage.error(res.message || '操作失败')
|
||||||
@@ -274,6 +280,8 @@ const submitComment = async (isReply = false) => {
|
|||||||
commentContent.value = ''
|
commentContent.value = ''
|
||||||
}
|
}
|
||||||
ElMessage.success('评论已发送')
|
ElMessage.success('评论已发送')
|
||||||
|
// 更新评论数
|
||||||
|
singer.value.commentCount++
|
||||||
} else {
|
} else {
|
||||||
ElMessage.error(res.message || '评论失败')
|
ElMessage.error(res.message || '评论失败')
|
||||||
}
|
}
|
||||||
@@ -295,6 +303,8 @@ const deleteComment = async (commentId) => {
|
|||||||
const res = await apiDeleteComment(commentId)
|
const res = await apiDeleteComment(commentId)
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
comments.value = comments.value.filter(c => c.id !== commentId)
|
comments.value = comments.value.filter(c => c.id !== commentId)
|
||||||
|
// 更新评论数
|
||||||
|
singer.value.commentCount--
|
||||||
ElMessage.success('评论已删除')
|
ElMessage.success('评论已删除')
|
||||||
} else {
|
} else {
|
||||||
ElMessage.error(res.message || '删除失败')
|
ElMessage.error(res.message || '删除失败')
|
||||||
@@ -414,6 +424,14 @@ onUnmounted(() => {
|
|||||||
<div class="stat-value">{{ formatFans(singer.fansCount || 0) }}</div>
|
<div class="stat-value">{{ formatFans(singer.fansCount || 0) }}</div>
|
||||||
<div class="stat-label">粉丝数</div>
|
<div class="stat-label">粉丝数</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="stat-item">
|
||||||
|
<div class="stat-value">{{ singer.collectCount || 0 }}</div>
|
||||||
|
<div class="stat-label">收藏数</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat-item">
|
||||||
|
<div class="stat-value">{{ singer.commentCount || 0 }}</div>
|
||||||
|
<div class="stat-label">评论数</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="singer-intro">{{ singer.introduction }}</div>
|
<div class="singer-intro">{{ singer.introduction }}</div>
|
||||||
|
|
||||||
@@ -500,7 +518,7 @@ onUnmounted(() => {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<el-tab-pane label="评论">
|
<el-tab-pane :label="`评论(${singer.commentCount || 0})`">
|
||||||
<!-- 评论输入框 -->
|
<!-- 评论输入框 -->
|
||||||
<div class="comment-input">
|
<div class="comment-input">
|
||||||
<el-input
|
<el-input
|
||||||
|
|||||||
Reference in New Issue
Block a user