feat(content): 列表卡片与后台返回收藏点赞评论数
This commit is contained in:
1 parent
f90336963b
commit
256821b824
13 files changed
+324
-50
No files matched your search
+19
-3
@@ -10,8 +10,9 @@ import com.fruitshorts.content.vo.DramaPageReqVO;
|
||||
import com.fruitshorts.content.vo.DramaRespVO;
|
||||
import com.fruitshorts.content.vo.DramaSaveReqVO;
|
||||
import com.fruitshorts.content.vo.DramaStatusReqVO;
|
||||
import com.fruitshorts.framework.security.SecurityUtils;
|
||||
import com.fruitshorts.framework.log.OperationLog;
|
||||
import com.fruitshorts.framework.security.SecurityUtils;
|
||||
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;
|
||||
@@ -27,6 +28,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Tag(name = "后台-短剧管理")
|
||||
@RestController
|
||||
@@ -35,19 +37,24 @@ import java.util.List;
|
||||
public class DramaController {
|
||||
|
||||
private final DramaService dramaService;
|
||||
private final CommentService commentService;
|
||||
|
||||
@Operation(summary = "短剧分页查询")
|
||||
@GetMapping("/page")
|
||||
@PreAuthorize("@ss.hasPermission('drama:list')")
|
||||
public Result<PageResult<DramaRespVO>> page(DramaPageReqVO req) {
|
||||
return Result.ok(dramaService.page(req));
|
||||
PageResult<DramaRespVO> page = dramaService.page(req);
|
||||
fillCommentCounts(page.getList());
|
||||
return Result.ok(page);
|
||||
}
|
||||
|
||||
@Operation(summary = "短剧详情")
|
||||
@GetMapping("/{id}")
|
||||
@PreAuthorize("@ss.hasPermission('drama:list')")
|
||||
public Result<DramaRespVO> get(@PathVariable Long id) {
|
||||
return Result.ok(dramaService.get(id));
|
||||
DramaRespVO drama = dramaService.get(id);
|
||||
fillCommentCounts(List.of(drama));
|
||||
return Result.ok(drama);
|
||||
}
|
||||
|
||||
@Operation(summary = "新建短剧")
|
||||
@@ -133,4 +140,13 @@ public class DramaController {
|
||||
dramaService.changeStatus(id, action, req == null ? null : req.getReason(),
|
||||
SecurityUtils.getUserId(), SecurityUtils.getUsername());
|
||||
}
|
||||
|
||||
private void fillCommentCounts(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)));
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
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.vo.DramaPageReqVO;
|
||||
import com.fruitshorts.content.vo.DramaRespVO;
|
||||
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.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
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;
|
||||
|
||||
/** 后台短剧列表与详情的互动数量回填契约。 */
|
||||
class DramaControllerContractTest {
|
||||
|
||||
private DramaService dramaService;
|
||||
private CommentService commentService;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
dramaService = mock(DramaService.class);
|
||||
commentService = mock(CommentService.class);
|
||||
mockMvc = ControllerContractSupport.mockMvcOf(
|
||||
new DramaController(dramaService, commentService));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_fill_comment_counts_in_batch_when_page_is_requested() throws Exception {
|
||||
DramaRespVO first = drama(7L, 3200L, 1800L);
|
||||
DramaRespVO second = drama(8L, 120L, 80L);
|
||||
given(dramaService.page(any(DramaPageReqVO.class)))
|
||||
.willReturn(new PageResult<>(2L, List.of(first, second)));
|
||||
given(commentService.countByDramas(List.of(7L, 8L)))
|
||||
.willReturn(Map.of(7L, 11L));
|
||||
|
||||
mockMvc.perform(get("/admin/dramas/page"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(0))
|
||||
.andExpect(jsonPath("$.data.list[0].likeCount").value("3200"))
|
||||
.andExpect(jsonPath("$.data.list[0].favoriteCount").value("1800"))
|
||||
.andExpect(jsonPath("$.data.list[0].commentCount").value("11"))
|
||||
.andExpect(jsonPath("$.data.list[1].likeCount").value("120"))
|
||||
.andExpect(jsonPath("$.data.list[1].favoriteCount").value("80"))
|
||||
.andExpect(jsonPath("$.data.list[1].commentCount").value("0"));
|
||||
|
||||
verify(commentService).countByDramas(List.of(7L, 8L));
|
||||
}
|
||||
|
||||
@Test
|
||||
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));
|
||||
|
||||
mockMvc.perform(get("/admin/dramas/7"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.likeCount").value("3200"))
|
||||
.andExpect(jsonPath("$.data.favoriteCount").value("1800"))
|
||||
.andExpect(jsonPath("$.data.commentCount").value("11"));
|
||||
}
|
||||
|
||||
private DramaRespVO drama(Long id, Long likeCount, Long favoriteCount) {
|
||||
DramaRespVO drama = new DramaRespVO();
|
||||
drama.setId(id);
|
||||
drama.setTitle("短剧" + id);
|
||||
drama.setLikeCount(likeCount);
|
||||
drama.setFavoriteCount(favoriteCount);
|
||||
return drama;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user