You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

120 lines
3.6 KiB

package cc.bnblogs.controller;
import cc.bnblogs.common.PageHelper;
import cc.bnblogs.common.Result;
import cc.bnblogs.common.WebSite;
import cc.bnblogs.enums.ResultEnum;
import cc.bnblogs.pojo.Article;
import cc.bnblogs.pojo.Comment;
import cc.bnblogs.service.CommentService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
@RestController
@RequestMapping("/admin/comment")
public class CommentController {
private final CommentService commentService;
private final WebSite webSite;
public CommentController(CommentService commentService, WebSite webSite) {
this.commentService = commentService;
this.webSite = webSite;
}
/**
* 根据评论是否已读返回分页后的评论列表
* 按评论创建时间降序返回
* @param view 是否已读
* @param pageNumber 页号
* @param pageSize 页大小
* @return 分页后的评论
*/
@GetMapping("/")
public Result<PageHelper<Comment>> list(@RequestParam(required = false) Boolean view,
@RequestParam(required = false,defaultValue = "1") Integer pageNumber,
@RequestParam(required = false,defaultValue = "1")Integer pageSize) {
pageNumber = Math.max(1,pageNumber);
pageSize = Math.max(1,pageSize);
return Result.success(commentService.list(view,pageNumber,pageSize));
}
/**
* 请求评论详情的接口
* @param id 评论id
* @return 返回评论的详情
*/
@GetMapping("/{id}")
public Result<Comment> detail(@PathVariable Integer id) {
Comment comment = commentService.detail(id);
if (Objects.isNull(comment)) {
return Result.error(ResultEnum.RESULT_NOT_FOUND);
} else {
return Result.success(comment);
}
}
/**
* 保存修改后的评论数据
* @param comment 修改的评论
* @return 修改结果
*/
@PostMapping("/")
public Result<String> save(Comment comment) {
commentService.save(comment);
return Result.success();
}
/**
* 删除一个评论
* @param id 删除的评论id
* @return 删除结果
*/
@DeleteMapping("/{id}")
public Result<String> delete(@PathVariable Integer id) {
commentService.delete(id);
return Result.success();
}
/**
* 一键已读所有评论
*/
@GetMapping("/read")
public Result<String> readAll(){
commentService.readAll();
return Result.success();
}
/**
* 回复评论
* @param pid 父评论id
* @param aid 文章id
* @param message 回复内容
* @return 操作结果
*/
@PostMapping("/{pid}")
public Result<String> reply(@PathVariable Integer pid,Integer aid,String message) {
// 构造一个comment对象
// 填充站长的昵称、邮箱、网站地址、父评论id、文章id、回复内容
Comment comment = Comment.builder()
.article(Article.builder().id(aid).build())
.nickname(webSite.getNickname())
.email(webSite.getMail())
.url(webSite.getDomain())
.pid(pid)
.content(message)
.build();
// 保存评论
commentService.save(comment);
return Result.success();
}
}