|
@@ -10,9 +10,11 @@ import javax.servlet.http.HttpSession;
|
|
|
|
|
|
import com.dbs.realworld.common.Views;
|
|
|
import com.dbs.realworld.dto.ArticleDTO;
|
|
|
+import com.dbs.realworld.dto.CommentDTO;
|
|
|
import com.dbs.realworld.dto.UserDTO;
|
|
|
import com.dbs.realworld.mapper.UserMapper;
|
|
|
import com.dbs.realworld.service.ArticleService;
|
|
|
+import com.dbs.realworld.service.CommentService;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -33,12 +35,14 @@ public class ArticleController {
|
|
|
|
|
|
private final ArticleService articleService;
|
|
|
private final UserMapper userMapper;
|
|
|
+ private final CommentService commentService;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
- public ArticleController(ArticleService articleService, UserMapper userMapper) {
|
|
|
+ public ArticleController(ArticleService articleService, UserMapper userMapper, CommentService commentService) {
|
|
|
this.articleService = articleService;
|
|
|
this.userMapper = userMapper;
|
|
|
+ this.commentService = commentService;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -71,6 +75,51 @@ public class ArticleController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @PostMapping("/{articleId}/comment")
|
|
|
+ public ResponseEntity saveComment(HttpServletRequest request, @PathVariable("articleId") int articleId, @RequestBody CommentDTO commentDTO) {
|
|
|
+ // 사용자 정보 확인
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ String ssEmail = (String) session.getAttribute("ssEmail");
|
|
|
+ UserDTO finded = this.userMapper.selectUserByEmail(ssEmail);
|
|
|
+
|
|
|
+ if (finded == null) {
|
|
|
+ ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("존재하지 않는 사용자 입니다.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 게시글 확인
|
|
|
+ ArticleDTO targetArticle = articleService.findByArticleId(articleId);
|
|
|
+ if (targetArticle == null) {
|
|
|
+ ResponseEntity.status(HttpStatus.NO_CONTENT).body("존재하지 않는 게시글 입니다.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 댓글 삽입
|
|
|
+ commentDTO.setArticleId(articleId);
|
|
|
+ commentService.save(commentDTO);
|
|
|
+
|
|
|
+ // 리턴
|
|
|
+ URI uri = URI.create("/article/" + articleId + "/comment");
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("createdCommentId", commentDTO.getId());
|
|
|
+ return ResponseEntity.created(uri).body(data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("/{articleId}/comment/all")
|
|
|
+ public ResponseEntity getCommentsByArticleId(@PathVariable("articleId") int articleId) {
|
|
|
+ // 게시글 있는지 확인
|
|
|
+ ArticleDTO articleDTO = articleService.findByArticleId(articleId);
|
|
|
+ if (articleDTO == null) {
|
|
|
+ ResponseEntity.status(HttpStatus.NO_CONTENT).body("존재하지 않는 게시글 입니다.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 댓글 조회
|
|
|
+ List<CommentDTO> commentDTOs = commentService.findAllByArticleId(articleId);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("comments", commentDTOs);
|
|
|
+ return ResponseEntity.ok().body(data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@GetMapping("/{articleId}/edit")
|
|
|
public String initEditForm(HttpServletRequest request, @PathVariable("articleId") int articleId, ModelMap model) {
|
|
|
// 아티클 정보 조회
|
|
@@ -102,7 +151,6 @@ public class ArticleController {
|
|
|
* https://stackoverflow.com/a/17376670
|
|
|
* HTTP PATCH Request의 경우 바디에 아무 제약도 없다
|
|
|
*/
|
|
|
- // @PostMapping("/{articleId}")
|
|
|
@PatchMapping("/{articleId}")
|
|
|
public ResponseEntity editArticle(HttpServletRequest request, @PathVariable("articleId") int articleId, @RequestBody ArticleDTO articleDTO) {
|
|
|
HttpSession session = request.getSession();
|