|
@@ -15,10 +15,15 @@ import com.dbs.realworld.mapper.UserMapper;
|
|
|
import com.dbs.realworld.service.ArticleService;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.ui.ModelMap;
|
|
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PatchMapping;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
@@ -30,18 +35,21 @@ public class ArticleController {
|
|
|
private final ArticleService articleService;
|
|
|
private final UserMapper userMapper;
|
|
|
|
|
|
+
|
|
|
@Autowired
|
|
|
public ArticleController(ArticleService articleService, UserMapper userMapper) {
|
|
|
this.articleService = articleService;
|
|
|
this.userMapper = userMapper;
|
|
|
}
|
|
|
|
|
|
- @RequestMapping
|
|
|
+
|
|
|
+ @GetMapping
|
|
|
public String initArticleForm() {
|
|
|
return Views.ARTICLE_FORM;
|
|
|
}
|
|
|
|
|
|
- @RequestMapping(method = RequestMethod.POST)
|
|
|
+
|
|
|
+ @PostMapping
|
|
|
public ResponseEntity saveArticle(HttpServletRequest request, @RequestBody ArticleDTO articleDTO) {
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
|
|
@@ -49,7 +57,7 @@ public class ArticleController {
|
|
|
// 사용자 조회하여 존재하는 사용자인지 확인
|
|
|
HttpSession session = request.getSession();
|
|
|
String ssEmail = (String) session.getAttribute("ssEmail");
|
|
|
- UserDTO finded = userMapper.selectUserByEmail(ssEmail);
|
|
|
+ UserDTO finded = this.userMapper.selectUserByEmail(ssEmail);
|
|
|
|
|
|
if (finded != null) {
|
|
|
articleDTO.setWriterId(finded.getId());
|
|
@@ -63,20 +71,86 @@ public class ArticleController {
|
|
|
return ResponseEntity.created(URI.create("/article")).body(data);
|
|
|
}
|
|
|
|
|
|
- @RequestMapping("/all")
|
|
|
+
|
|
|
+ @GetMapping("/{articleId}/edit")
|
|
|
+ public String initEditForm(HttpServletRequest request, @PathVariable("articleId") int articleId, ModelMap model) {
|
|
|
+ // 아티클 정보 조회
|
|
|
+ ArticleDTO finded = this.articleService.findByArticleId(articleId);
|
|
|
+
|
|
|
+ if (finded == null) {
|
|
|
+ // mvc 방식으로 모달을 띄워주는게 나을거 같음...
|
|
|
+ // return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ int ssUserId = (int) session.getAttribute("ssId");
|
|
|
+ String ssEmail = (String) session.getAttribute("ssEmail");
|
|
|
+
|
|
|
+ // 권한 확인
|
|
|
+ if (!(finded.getWriterId() == ssUserId && finded.getWriterEmail().equals(ssEmail))) {
|
|
|
+ // mvc 방식으로 모달을 띄워주는게 나을거 같음...
|
|
|
+ // return ResponseEntity
|
|
|
+ // .status(HttpStatus.FORBIDDEN)
|
|
|
+ // .body("수정할 권한이 없습니다."); // error message
|
|
|
+ }
|
|
|
+
|
|
|
+ model.addAttribute("article", finded);
|
|
|
+ return Views.ARTICLE_FORM;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * https://stackoverflow.com/a/17376670
|
|
|
+ * HTTP PATCH Request의 경우 바디에 아무 제약도 없다
|
|
|
+ */
|
|
|
+ @PatchMapping("/{articleId}")
|
|
|
+ public ResponseEntity editArticle(HttpServletRequest request, @PathVariable("articleId") int articlId, @RequestBody ArticleDTO articleDTO) {
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("/all")
|
|
|
public ResponseEntity getArticles() {
|
|
|
- List<ArticleDTO> articleDTOs = articleService.findAll();
|
|
|
+ List<ArticleDTO> articleDTOs = this.articleService.findAll();
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
data.put("articles", articleDTOs);
|
|
|
|
|
|
return ResponseEntity.ok().body(data);
|
|
|
}
|
|
|
|
|
|
- @RequestMapping("/{articleId}")
|
|
|
+
|
|
|
+ @GetMapping("/{articleId}")
|
|
|
public String getArticleById(@PathVariable("articleId") int articleId, ModelMap model) {
|
|
|
|
|
|
- ArticleDTO articleDTO = articleService.findByArticleId(articleId);
|
|
|
+ ArticleDTO articleDTO = this.articleService.findByArticleId(articleId);
|
|
|
model.put("articleDetail", articleDTO);
|
|
|
return Views.ARTICLE_DETAIL;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @DeleteMapping("/{articleId}")
|
|
|
+ public ResponseEntity deleteArticleById(HttpServletRequest request, @PathVariable("articleId") int articleId) {
|
|
|
+
|
|
|
+ ArticleDTO finded = this.articleService.findByArticleId(articleId);
|
|
|
+
|
|
|
+ if (finded == null) {
|
|
|
+ return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ int ssUserId = (int) session.getAttribute("ssId");
|
|
|
+ String ssEmail = (String) session.getAttribute("ssEmail");
|
|
|
+
|
|
|
+ if (finded.getWriterId() == ssUserId && finded.getWriterEmail().equals(ssEmail)) {
|
|
|
+ this.articleService.remove(articleId);
|
|
|
+ } else {
|
|
|
+ return ResponseEntity
|
|
|
+ .status(HttpStatus.FORBIDDEN)
|
|
|
+ .body("삭제할 권한이 없습니다."); // error message
|
|
|
+ }
|
|
|
+
|
|
|
+ return ResponseEntity.ok().build();
|
|
|
+ }
|
|
|
}
|