JournalCommentController.java 5.5 KB
Newer Older
1 2
package run.halo.app.controller.admin.api;

J
John Niang 已提交
3 4
import static org.springframework.data.domain.Sort.Direction.DESC;

5
import io.swagger.annotations.ApiOperation;
J
John Niang 已提交
6
import java.util.List;
J
johnniang 已提交
7
import org.springframework.data.domain.Page;
8
import org.springframework.data.domain.PageRequest;
J
johnniang 已提交
9
import org.springframework.data.domain.Pageable;
10
import org.springframework.data.domain.Sort;
J
johnniang 已提交
11
import org.springframework.data.web.PageableDefault;
12
import org.springframework.data.web.SortDefault;
J
John Niang 已提交
13 14 15 16 17 18 19 20 21
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
22 23
import run.halo.app.model.dto.BaseCommentDTO;
import run.halo.app.model.entity.JournalComment;
24
import run.halo.app.model.enums.CommentStatus;
J
johnniang 已提交
25
import run.halo.app.model.params.CommentQuery;
26
import run.halo.app.model.params.JournalCommentParam;
27 28
import run.halo.app.model.vo.BaseCommentVO;
import run.halo.app.model.vo.BaseCommentWithParentVO;
J
johnniang 已提交
29
import run.halo.app.model.vo.JournalCommentWithJournalVO;
30
import run.halo.app.service.JournalCommentService;
31
import run.halo.app.service.OptionService;
32 33 34 35 36

/**
 * Journal comment controller.
 *
 * @author johnniang
37
 * @author guqing
38
 * @date 2019-04-25
39 40 41 42 43 44 45
 */
@RestController
@RequestMapping("/api/admin/journals/comments")
public class JournalCommentController {

    private final JournalCommentService journalCommentService;

46 47 48
    private final OptionService optionService;

    public JournalCommentController(JournalCommentService journalCommentService,
J
John Niang 已提交
49
        OptionService optionService) {
50
        this.journalCommentService = journalCommentService;
51
        this.optionService = optionService;
52 53
    }

J
johnniang 已提交
54 55
    @GetMapping
    @ApiOperation("Lists journal comments")
J
John Niang 已提交
56 57 58 59 60
    public Page<JournalCommentWithJournalVO> pageBy(
        @PageableDefault(sort = "createTime", direction = DESC) Pageable pageable,
        CommentQuery commentQuery) {
        Page<JournalComment> journalCommentPage =
            journalCommentService.pageBy(commentQuery, pageable);
J
johnniang 已提交
61 62 63 64 65

        return journalCommentService.convertToWithJournalVo(journalCommentPage);
    }

    @GetMapping("latest")
66
    @ApiOperation("Lists latest journal comments")
J
John Niang 已提交
67 68 69 70 71
    public List<JournalCommentWithJournalVO> listLatest(
        @RequestParam(name = "top", defaultValue = "10") int top,
        @RequestParam(name = "status", required = false) CommentStatus status) {
        List<JournalComment> latestComments =
            journalCommentService.pageLatest(top, status).getContent();
J
johnniang 已提交
72
        return journalCommentService.convertToWithJournalVo(latestComments);
J
johnniang 已提交
73 74
    }

75 76 77
    @GetMapping("{journalId:\\d+}/tree_view")
    @ApiOperation("Lists comments with tree view")
    public Page<BaseCommentVO> listCommentTree(@PathVariable("journalId") Integer journalId,
J
John Niang 已提交
78 79 80 81
        @RequestParam(name = "page", required = false, defaultValue = "0") int page,
        @SortDefault(sort = "createTime", direction = DESC) Sort sort) {
        return journalCommentService.pageVosAllBy(journalId,
            PageRequest.of(page, optionService.getCommentPageSize(), sort));
82 83 84 85 86
    }

    @GetMapping("{journalId:\\d+}/list_view")
    @ApiOperation("Lists comment with list view")
    public Page<BaseCommentWithParentVO> listComments(@PathVariable("journalId") Integer journalId,
J
John Niang 已提交
87 88 89 90
        @RequestParam(name = "page", required = false, defaultValue = "0") int page,
        @SortDefault(sort = "createTime", direction = DESC) Sort sort) {
        return journalCommentService.pageWithParentVoBy(journalId,
            PageRequest.of(page, optionService.getCommentPageSize(), sort));
91 92
    }

93 94 95 96 97 98 99
    @PostMapping
    @ApiOperation("Creates a journal comment")
    public BaseCommentDTO createCommentBy(@RequestBody JournalCommentParam journalCommentParam) {
        JournalComment journalComment = journalCommentService.createBy(journalCommentParam);
        return journalCommentService.convertTo(journalComment);
    }

100 101 102
    @PutMapping("{commentId:\\d+}/status/{status}")
    @ApiOperation("Updates comment status")
    public BaseCommentDTO updateStatusBy(@PathVariable("commentId") Long commentId,
J
John Niang 已提交
103
        @PathVariable("status") CommentStatus status) {
104
        // Update comment status
J
John Niang 已提交
105 106
        JournalComment updatedJournalComment =
            journalCommentService.updateStatus(commentId, status);
107 108 109
        return journalCommentService.convertTo(updatedJournalComment);
    }

110 111 112 113 114 115 116 117 118 119
    @PutMapping("/{commentId:\\d+}")
    @ApiOperation("Updates a journal comment by comment id")
    public BaseCommentDTO updateCommentBy(@PathVariable Long commentId,
        @RequestBody JournalCommentParam journalCommentParam) {
        JournalComment commentToUpdate = journalCommentService.getById(commentId);
        journalCommentParam.update(commentToUpdate);

        return journalCommentService.convertTo(journalCommentService.update(commentToUpdate));
    }

120 121 122 123 124 125
    @DeleteMapping("{commentId:\\d+}")
    @ApiOperation("Deletes comment permanently and recursively")
    public BaseCommentDTO deleteBy(@PathVariable("commentId") Long commentId) {
        JournalComment deletedJournalComment = journalCommentService.removeById(commentId);
        return journalCommentService.convertTo(deletedJournalComment);
    }
126
}