feat(interaction): 点赞与评论按集统计

This commit is contained in:
Zhou Weidong committed 2026-10-21 15:56:26 +08:00
1 parent c7ff729f84
commit c6f729fb27
28 files changed
+683 -41

No files matched your search

@@ -6,6 +6,7 @@ import com.fruitshorts.common.pojo.PageResult;
import com.fruitshorts.common.pojo.Result;
import com.fruitshorts.content.dal.entity.DramaReviewRecord;
import com.fruitshorts.content.service.DramaService;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.content.vo.DramaPageReqVO;
import com.fruitshorts.content.vo.DramaRespVO;
import com.fruitshorts.content.vo.DramaSaveReqVO;
@@ -38,13 +39,14 @@ public class DramaController {
private final DramaService dramaService;
private final CommentService commentService;
private final EpisodeService episodeService;
@Operation(summary = "短剧分页查询")
@GetMapping("/page")
@PreAuthorize("@ss.hasPermission('drama:list')")
public Result<PageResult<DramaRespVO>> page(DramaPageReqVO req) {
PageResult<DramaRespVO> page = dramaService.page(req);
fillCommentCounts(page.getList());
fillInteractionCounts(page.getList());
return Result.ok(page);
}
@@ -53,7 +55,7 @@ public class DramaController {
@PreAuthorize("@ss.hasPermission('drama:list')")
public Result<DramaRespVO> get(@PathVariable Long id) {
DramaRespVO drama = dramaService.get(id);
fillCommentCounts(List.of(drama));
fillInteractionCounts(List.of(drama));
return Result.ok(drama);
}
@@ -141,12 +143,16 @@ public class DramaController {
SecurityUtils.getUserId(), SecurityUtils.getUsername());
}
private void fillCommentCounts(List<DramaRespVO> dramas) {
private void fillInteractionCounts(List<DramaRespVO> dramas) {
if (dramas == null || dramas.isEmpty()) {
return;
}
List<Long> ids = dramas.stream().map(DramaRespVO::getId).toList();
Map<Long, Long> counts = commentService.countByDramas(ids);
dramas.forEach(drama -> drama.setCommentCount(counts.getOrDefault(drama.getId(), 0L)));
Map<Long, Long> likeCounts = episodeService.sumLikeCountsByDramas(ids);
Map<Long, Long> commentCounts = commentService.countByDramas(ids);
dramas.forEach(drama -> {
drama.setLikeCount(likeCounts.getOrDefault(drama.getId(), 0L));
drama.setCommentCount(commentCounts.getOrDefault(drama.getId(), 0L));
});
}
}
@@ -3,11 +3,14 @@
package com.fruitshorts.admin.controller;
import com.fruitshorts.common.pojo.Result;
import com.fruitshorts.content.dal.entity.Drama;
import com.fruitshorts.content.service.DramaService;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.content.vo.EpisodeRespVO;
import com.fruitshorts.content.vo.EpisodeSaveReqVO;
import com.fruitshorts.content.vo.EpisodeSortReqVO;
import com.fruitshorts.framework.log.OperationLog;
import com.fruitshorts.interaction.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
@@ -24,6 +27,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@Tag(name = "后台-剧集管理")
@RestController
@@ -32,13 +36,24 @@ import java.util.List;
public class EpisodeController {
private final EpisodeService episodeService;
private final CommentService commentService;
private final DramaService dramaService;
@Operation(summary = "剧集列表")
@GetMapping
@PreAuthorize("@ss.hasPermission('episode:list')")
public Result<List<EpisodeRespVO>> list(@RequestParam Long dramaId,
@RequestParam(required = false) String status) {
return Result.ok(episodeService.listByDrama(dramaId, status));
List<EpisodeRespVO> episodes = episodeService.listByDrama(dramaId, status);
Map<Long, Long> commentCounts = commentService.countByEpisodes(
episodes.stream().map(EpisodeRespVO::getId).toList());
Drama drama = dramaService.getExisting(dramaId);
episodes.forEach(episode -> {
episode.setCommentCount(commentCounts.getOrDefault(episode.getId(), 0L));
episode.setFavoriteCount(drama.getFavoriteCount());
episode.setHeat(drama.getViewCount());
});
return Result.ok(episodes);
}
@Operation(summary = "新建剧集")
@@ -5,6 +5,7 @@ package com.fruitshorts.admin.controller;
import com.fruitshorts.admin.support.ControllerContractSupport;
import com.fruitshorts.common.pojo.PageResult;
import com.fruitshorts.content.service.DramaService;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.content.vo.DramaPageReqVO;
import com.fruitshorts.content.vo.DramaRespVO;
import com.fruitshorts.interaction.service.CommentService;
@@ -28,14 +29,16 @@ class DramaControllerContractTest {
private DramaService dramaService;
private CommentService commentService;
private EpisodeService episodeService;
private MockMvc mockMvc;
@BeforeEach
void setUp() {
dramaService = mock(DramaService.class);
commentService = mock(CommentService.class);
episodeService = mock(EpisodeService.class);
mockMvc = ControllerContractSupport.mockMvcOf(
new DramaController(dramaService, commentService));
new DramaController(dramaService, commentService, episodeService));
}
@Test
@@ -46,6 +49,8 @@ class DramaControllerContractTest {
.willReturn(new PageResult<>(2L, List.of(first, second)));
given(commentService.countByDramas(List.of(7L, 8L)))
.willReturn(Map.of(7L, 11L));
given(episodeService.sumLikeCountsByDramas(List.of(7L, 8L)))
.willReturn(Map.of(7L, 3200L, 8L, 120L));
mockMvc.perform(get("/admin/dramas/page"))
.andExpect(status().isOk())
@@ -65,6 +70,7 @@ class DramaControllerContractTest {
void should_fill_comment_count_when_detail_is_requested() throws Exception {
given(dramaService.get(7L)).willReturn(drama(7L, 3200L, 1800L));
given(commentService.countByDramas(List.of(7L))).willReturn(Map.of(7L, 11L));
given(episodeService.sumLikeCountsByDramas(List.of(7L))).willReturn(Map.of(7L, 3200L));
mockMvc.perform(get("/admin/dramas/7"))
.andExpect(status().isOk())
@@ -0,0 +1,70 @@
// Author: fruit.com.cn
package com.fruitshorts.admin.controller;
import com.fruitshorts.admin.support.ControllerContractSupport;
import com.fruitshorts.content.dal.entity.Drama;
import com.fruitshorts.content.service.DramaService;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.content.vo.EpisodeRespVO;
import com.fruitshorts.interaction.service.CommentService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import java.util.Map;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/** K89:后台剧集列表按集回填点赞/评论,并带所属剧收藏/热度。 */
class EpisodeControllerContractTest {
private EpisodeService episodeService;
private CommentService commentService;
private DramaService dramaService;
private MockMvc mockMvc;
@BeforeEach
void setUp() {
episodeService = mock(EpisodeService.class);
commentService = mock(CommentService.class);
dramaService = mock(DramaService.class);
mockMvc = ControllerContractSupport.mockMvcOf(
new EpisodeController(episodeService, commentService, dramaService));
}
@Test
void should_return_episode_and_drama_interaction_counts() throws Exception {
given(episodeService.listByDrama(5L, null)).willReturn(List.of(
episode(11L, 10L),
episode(12L, 20L)));
given(commentService.countByEpisodes(List.of(11L, 12L)))
.willReturn(Map.of(11L, 3L));
Drama drama = new Drama();
drama.setFavoriteCount(88L);
drama.setViewCount(1000L);
given(dramaService.getExisting(5L)).willReturn(drama);
mockMvc.perform(get("/admin/episodes").param("dramaId", "5"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data[0].likeCount").value("10"))
.andExpect(jsonPath("$.data[0].commentCount").value("3"))
.andExpect(jsonPath("$.data[0].favoriteCount").value("88"))
.andExpect(jsonPath("$.data[0].heat").value("1000"))
.andExpect(jsonPath("$.data[1].likeCount").value("20"))
.andExpect(jsonPath("$.data[1].commentCount").value("0"));
}
private EpisodeRespVO episode(Long id, Long likeCount) {
EpisodeRespVO episode = new EpisodeRespVO();
episode.setId(id);
episode.setDramaId(5L);
episode.setLikeCount(likeCount);
return episode;
}
}