1
0
Files
light-blog/src/main/java/xyz/fortern/controller/ReplyController.java
2024-03-28 04:24:35 +08:00

61 lines
2.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package xyz.fortern.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import xyz.fortern.pojo.Reply;
import xyz.fortern.pojo.User;
import xyz.fortern.service.ReplyService;
import xyz.fortern.util.PageInfo;
@Slf4j
@Controller
@RequestMapping("/reply")
public class ReplyController {
private final ReplyService replyService;
public ReplyController(ReplyService replyService) {
this.replyService = replyService;
}
/**
* 获取某评论下的所有回复
*
* @param cid 评论id
* @param page 页码
* @param orderByTime 时间排序规则false旧的在前true新的在前
* @return 一页评论信息
*/
@GetMapping("/comment/{cid}")
public ResponseEntity<PageInfo<Reply>> getByCommentId(@PathVariable int cid, Integer page, boolean orderByTime) {
return ResponseEntity.ok(replyService.getByCommentId(cid, page, 10, orderByTime));
}
/**
* 添加一条评论
*
* @param cid 所属评论的id
* @param rid 回复的那条回复的id
* @param content 回复内容
* @param user 操作用户
* @return 操作结果
*/
//TODO 改变获取用户Id的方式
@PostMapping("/new/{cid}")
public ResponseEntity<?> addNewReply(@PathVariable int cid,
@RequestParam(required = false) Integer rid,
@RequestParam String content,
@SessionAttribute("user") User user) {
var reply = Reply.builder().cid(cid).rid(rid).uid(user.getId()).content(content).build();
replyService.addNewReply(reply);
return ResponseEntity.ok(reply);
}
}