CommentController.java 2.85 KB
package com.tanpu.community.controller;


import com.tanpu.common.api.CommonResp;
import com.tanpu.common.auth.AuthLogin;
import com.tanpu.common.auth.UserHolder;
import com.tanpu.community.api.beans.qo.CommentQo;
import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
import com.tanpu.community.api.beans.req.comment.LikeCommentReq;
import com.tanpu.community.api.beans.req.comment.QueryCommentReq;
import com.tanpu.community.api.beans.req.comment.ReportCommentReq;
import com.tanpu.community.api.beans.req.page.Page;
import com.tanpu.community.manager.CommentManager;
import com.tanpu.community.util.PageUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/api/theme")
public class CommentController {

    @Autowired
    private CommentManager commentManager;

    @Resource
    private UserHolder userHolder;

    @ApiOperation("发表评论")
    @AuthLogin
    @PostMapping(value = "/publishComment")
    @ResponseBody
    public CommonResp<CommentQo> publishCommet(@Validated @RequestBody CreateCommentReq req) {
        String userId = userHolder.getUserId();
        return CommonResp.success(commentManager.comment(req, userId));
    }

    @ApiOperation("评论列表")
    @AuthLogin
    @PostMapping(value = "/queryComment")
    @ResponseBody
    public CommonResp<Page<CommentQo>> queryComment(@Validated @RequestBody QueryCommentReq req) {
        String userId = userHolder.getUserId();
        List<CommentQo> result = commentManager.queryComments(req.getThemeId(), userId);
        return CommonResp.success(PageUtils.page(req.getPage(), result));
    }

    @ApiOperation("点赞评论")
    @PostMapping(value = "/likeComment")
    @AuthLogin
    @ResponseBody
    public CommonResp<Void> likeComment(@Validated @RequestBody LikeCommentReq req) {
        String userId = userHolder.getUserId();
        commentManager.likeComment(req, userId);
        return CommonResp.success();
    }

    @ApiOperation("举报评论")
    @PostMapping(value = "/reportComment")
    @AuthLogin
    @ResponseBody
    public CommonResp<Void> reportComment(@Validated @RequestBody ReportCommentReq req) {
        String userId = userHolder.getUserId();
        commentManager.report(req, userId);
        return CommonResp.success();
    }

    @ApiOperation("删除评论")
    @GetMapping(value = "/deleteComment")
    @AuthLogin
    @ResponseBody
    public CommonResp<Void> reportComment(@RequestParam(value = "commentId") String commentId) {
        String userId = userHolder.getUserId();
        commentManager.delete(commentId, userId);
        return CommonResp.success();
    }
}