package run.halo.app.controller.content.api; import io.swagger.annotations.ApiOperation; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; import org.springframework.data.web.SortDefault; import org.springframework.web.bind.annotation.*; import run.halo.app.cache.lock.CacheLock; import run.halo.app.model.dto.BaseCommentDTO; import run.halo.app.model.dto.post.BasePostDetailDTO; import run.halo.app.model.dto.post.BasePostSimpleDTO; import run.halo.app.model.entity.Sheet; import run.halo.app.model.entity.SheetComment; import run.halo.app.model.enums.CommentStatus; import run.halo.app.model.enums.PostStatus; import run.halo.app.model.params.SheetCommentParam; import run.halo.app.model.vo.BaseCommentVO; import run.halo.app.model.vo.BaseCommentWithParentVO; import run.halo.app.model.vo.CommentWithHasChildrenVO; import run.halo.app.service.OptionService; import run.halo.app.service.SheetCommentService; import run.halo.app.service.SheetService; import java.util.List; import static org.springframework.data.domain.Sort.Direction.DESC; /** * Sheet controller. * * @author johnniang * @author ryanwang * @date 19-4-26 */ @RestController("PortalSheetController") @RequestMapping("/api/content/sheets") public class SheetController { private final SheetService sheetService; private final SheetCommentService sheetCommentService; private final OptionService optionService; public SheetController(SheetService sheetService, SheetCommentService sheetCommentService, OptionService optionService) { this.sheetService = sheetService; this.sheetCommentService = sheetCommentService; this.optionService = optionService; } @GetMapping @ApiOperation("Lists sheets") public Page pageBy(@PageableDefault(sort = "createTime", direction = DESC) Pageable pageable) { Page sheetPage = sheetService.pageBy(PostStatus.PUBLISHED, pageable); return sheetService.convertToSimple(sheetPage); } @GetMapping("{sheetId:\\d+}") @ApiOperation("Gets a sheet") public BasePostDetailDTO getBy(@PathVariable("sheetId") Integer sheetId, @RequestParam(value = "formatDisabled", required = false, defaultValue = "true") Boolean formatDisabled, @RequestParam(value = "sourceDisabled", required = false, defaultValue = "false") Boolean sourceDisabled) { BasePostDetailDTO sheetDetailVO = sheetService.convertToDetail(sheetService.getById(sheetId)); if (formatDisabled) { // Clear the format content sheetDetailVO.setFormatContent(null); } if (sourceDisabled) { // Clear the original content sheetDetailVO.setOriginalContent(null); } return sheetDetailVO; } @GetMapping("{sheetId:\\d+}/comments/top_view") public Page listTopComments(@PathVariable("sheetId") Integer sheetId, @RequestParam(name = "page", required = false, defaultValue = "0") int page, @SortDefault(sort = "createTime", direction = DESC) Sort sort) { Page result = sheetCommentService.pageTopCommentsBy(sheetId, CommentStatus.PUBLISHED, PageRequest.of(page, optionService.getCommentPageSize(), sort)); return sheetCommentService.filterIpAddress(result); } @GetMapping("{sheetId:\\d+}/comments/{commentParentId:\\d+}/children") public List listChildrenBy(@PathVariable("sheetId") Integer sheetId, @PathVariable("commentParentId") Long commentParentId, @SortDefault(sort = "createTime", direction = DESC) Sort sort) { // Find all children comments List sheetComments = sheetCommentService.listChildrenBy(sheetId, commentParentId, CommentStatus.PUBLISHED, sort); // Convert to base comment dto List result = sheetCommentService.convertTo(sheetComments); return sheetCommentService.filterIpAddress(result); } @GetMapping("{sheetId:\\d+}/comments/tree_view") @ApiOperation("Lists comments with tree view") public Page listCommentsTree(@PathVariable("sheetId") Integer sheetId, @RequestParam(name = "page", required = false, defaultValue = "0") int page, @SortDefault(sort = "createTime", direction = DESC) Sort sort) { Page result = sheetCommentService.pageVosBy(sheetId, PageRequest.of(page, optionService.getCommentPageSize(), sort)); return sheetCommentService.filterIpAddress(result); } @GetMapping("{sheetId:\\d+}/comments/list_view") @ApiOperation("Lists comment with list view") public Page listComments(@PathVariable("sheetId") Integer sheetId, @RequestParam(name = "page", required = false, defaultValue = "0") int page, @SortDefault(sort = "createTime", direction = DESC) Sort sort) { Page result = sheetCommentService.pageWithParentVoBy(sheetId, PageRequest.of(page, optionService.getCommentPageSize(), sort)); return sheetCommentService.filterIpAddress(result); } @PostMapping("comments") @ApiOperation("Comments a post") @CacheLock(autoDelete = false, traceRequest = true) public BaseCommentDTO comment(@RequestBody SheetCommentParam sheetCommentParam) { return sheetCommentService.convertTo(sheetCommentService.createBy(sheetCommentParam)); } }