|
|
@@ -1,7 +1,12 @@
|
|
|
package cn.seecoder.fdroidrepository.Controller;
|
|
|
|
|
|
import cn.seecoder.fdroidrepository.DataObject.Comment;
|
|
|
-import cn.seecoder.fdroidrepository.Service.CommentService;
|
|
|
+import cn.seecoder.fdroidrepository.DataObject.Event;
|
|
|
+import cn.seecoder.fdroidrepository.DataObject.ForumPost;
|
|
|
+import cn.seecoder.fdroidrepository.Service.ICommentService;
|
|
|
+import cn.seecoder.fdroidrepository.Service.IForumPostService;
|
|
|
+import cn.seecoder.fdroidrepository.event.EventProducer;
|
|
|
+import cn.seecoder.fdroidrepository.result.SingleResult;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
@@ -10,24 +15,45 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
+import static cn.seecoder.fdroidrepository.common.constants.TOPIC_COMMENT;
|
|
|
+
|
|
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
|
|
@RestController
|
|
|
@RequestMapping("/api/comments")
|
|
|
public class CommentController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ICommentService ICommentService;
|
|
|
+
|
|
|
@Autowired
|
|
|
- private CommentService commentService;
|
|
|
+ private IForumPostService IForumPostService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private EventProducer eventProducer;
|
|
|
@GetMapping("/post/{postId}")
|
|
|
public ResponseEntity<List<Comment>> getCommentsByPostId(@PathVariable Long postId) {
|
|
|
- List<Comment> comments = commentService.getCommentsByPostId(postId);
|
|
|
+ List<Comment> comments = ICommentService.getCommentsByPostId(postId);
|
|
|
return new ResponseEntity<>(comments, HttpStatus.OK);
|
|
|
}
|
|
|
|
|
|
@PostMapping("/post/{postId}")
|
|
|
- public ResponseEntity<String> saveComment(@PathVariable Long postId, @RequestBody Comment comment) {
|
|
|
+ public SingleResult<?> saveComment(@PathVariable Long postId, @RequestBody Comment comment) {
|
|
|
comment.setPostId(postId);
|
|
|
- commentService.saveComment(comment);
|
|
|
- return new ResponseEntity<>("Comment saved successfully", HttpStatus.OK);
|
|
|
+ int rows = ICommentService.saveComment(comment);
|
|
|
+ if(rows==1){
|
|
|
+ Event event = new Event()
|
|
|
+ .setTopic(TOPIC_COMMENT)
|
|
|
+ .setUserId(comment.getUserId())
|
|
|
+ .setEntityId(comment.getCommentId())
|
|
|
+ .setData("postId",postId);
|
|
|
+ ForumPost forumPost = IForumPostService.getPostById(postId);
|
|
|
+ event.setEntityUserId(forumPost.getUserId());
|
|
|
+ eventProducer.fireEvent(event);
|
|
|
+ return SingleResult.success("comment successfully!");
|
|
|
+ }else {
|
|
|
+ return SingleResult.failure("400","comment failed!");
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|