CommnetController 작성
package com.jcy.usedhunter.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;
import com.jcy.usedhunter.domain.CommentDto;
import com.jcy.usedhunter.service.CommentService;
//@Controller
@RestController
public class CommentController {
@Autowired
CommentService service;
// {
// "pcno":0,
// "comment":"modifyTest",
// "commenter":"asdf2"
// }
@PatchMapping("/comments/{cno}") // /comments/cno
// @ResponseBody
public ResponseEntity<String> modify(@PathVariable Integer cno, @RequestBody CommentDto commentDto, HttpSession session){
String commenter = (String)session.getAttribute("id");
// String commenter = "asdf2";
commentDto.setCommenter(commenter);
commentDto.setCno(cno);
System.out.println("dto = " + commentDto);
try {
if(service.modify(commentDto)!=1) {
throw new Exception("Modify Failed");
}
return new ResponseEntity<>("MOD_OK",HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>("MOD_ERROR",HttpStatus.BAD_REQUEST);
}
}
// {
// "pcno":0,
// "comment":"hi"
// }
@PostMapping("/comments") // /comments?bno=1081
// @ResponseBody
public ResponseEntity<String> write(@RequestBody CommentDto commentDto, Integer bno, HttpSession session){
String commenter = (String)session.getAttribute("id");
// String commenter = "asdf2";
commentDto.setCommenter(commenter);
commentDto.setBno(bno);
System.out.println("dto = " + commentDto);
try {
if(service.write(commentDto)!=1) {
throw new Exception("Write Failed");
}
return new ResponseEntity<>("WRT_OK",HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>("WRT_ERROR",HttpStatus.BAD_REQUEST);
}
}
@DeleteMapping("/comments/{cno}") // /comments/1?bno=1080
// @ResponseBody
public ResponseEntity<String> remove(@PathVariable Integer cno, Integer bno, HttpSession session){
String commenter = (String)session.getAttribute("id");
// String commenter = "asdf2";
try {
int rowCnt = service.remove(cno, bno, commenter);
if (rowCnt!=1) {
throw new Exception("Delete Failed");
}
return new ResponseEntity<>("DEL_OK", HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>("DEL_ERROR", HttpStatus.BAD_REQUEST);
}
}
@GetMapping("comments")
// @ResponseBody
public ResponseEntity<List<CommentDto>> list(Integer bno){
List<CommentDto> list = null;
try {
list = service.getList(bno);
return new ResponseEntity<List<CommentDto>>(list, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<List<CommentDto>>(HttpStatus.BAD_REQUEST);
}
}
}
remove 테스트
write 테스트
modify 테스트
list 테스트
'프로젝트 > 중고헌터' 카테고리의 다른 글
중고헌터 - 댓글 기능 구현 4 - 대댓글 (0) | 2022.10.10 |
---|---|
중고헌터 - 댓글 기능 구현 3 - UI 구현 (0) | 2022.10.10 |
중고헌터 - 댓글 기능 구현 1 - DAO와 Service의 작성 (0) | 2022.10.08 |
중고헌터 - REST API와 Ajax (0) | 2022.10.08 |
중고헌터 - 게시판 검색 기능 구현하기 (1) | 2022.10.06 |