package org.joonzis.controller;
import java.util.List;
import org.joonzis.domain.ReplyVO;
import org.joonzis.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.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;
import org.springframework.web.bind.annotation.RestController;
import lombok.AllArgsConstructor;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
@Log4j
@RestController
@RequestMapping("/replies/")
public class ReplyController {
/*
* 동작에 따른 url 방법(http 전송 방식)
* 1. 등록 - /replies/new - POST
* 2. 조회 - /replies/:rno - GET
* 3. 삭제 - /replies/:rno - DELETE
* 4. 수정 - /replies/:rno - PUT or PATCH
* 5. 페이지 - /replies/pages/:bno/:page - GET
*
* == REST 방식으로 설계할 땐 PK 기준으로 작성하는 것이 좋다. PK 만으로 CRUD가 가능하기 때문
* == 다만 댓글 목록은 PK 만으론 안되고 bno와 페이지 번호 정보가 필요
*/
@Setter(onMethod_ = @Autowired)
private ReplyService service;
// 1. 등록 - /replies/new - POST
// consumes = 수신 데이터 포맷
// produces = 송신 데이터 포맷
@PostMapping(value = "/new",
consumes = "application/json", //전달받는 데이터
produces = MediaType.TEXT_PLAIN_VALUE) //view에서 전달할 데이터
public ResponseEntity<String> create(@RequestBody ReplyVO vo){
log.info("ReplyVO..." + vo);
int insertCount = service.register(vo);
log.info("Reply Insert Count : " + insertCount);
//삼항연산자
return insertCount == 1 ?
new ResponseEntity<>("success", HttpStatus.OK) :
new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
// 2. 목록 - /replies/pages/:bno/:page - GET
// URL로 이동하기 때문에 전달받을 파라미터가 없다
@GetMapping(value = "/pages/{bno}/{page}",
produces = {MediaType.APPLICATION_ATOM_XML_VALUE,//xml로 요청하면 데이터 준다
MediaType.APPLICATION_JSON_VALUE})//json으로 요청하면 데이터 준다
public ResponseEntity<List<ReplyVO>> getList(@PathVariable("bno")long bno,
@PathVariable("page")int page){
log.info("getList...");
return new ResponseEntity<>(service.getList(bno), HttpStatus.OK);
}
// 3. 조회 - /replies/:rno - GET
@GetMapping(value = "/{rno}",
produces = {MediaType.APPLICATION_ATOM_XML_VALUE,
MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<ReplyVO> get(@PathVariable("rno") long rno){
log.info("get..." + rno);
return new ResponseEntity<>(service.get(rno), HttpStatus.OK);
}
// 4. 삭제 - /replies/:rno - DELETE
@DeleteMapping(value = "/{rno}",produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> remove(@PathVariable("rno")long rno){
log.info("remove..." + rno);
return service.remove(rno) == 1?
new ResponseEntity<>("success", HttpStatus.OK) :
new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
// 5. 수정 - /replies/:rno - PUT or PATCH
@RequestMapping(method = {RequestMethod.PUT, RequestMethod.PATCH},
value = "/{rno}", produces = MediaType.TEXT_PLAIN_VALUE,
consumes = "application/json")
public ResponseEntity<String> modify(@PathVariable("rno")long rno, @RequestBody ReplyVO vo){
log.info("modify..." + vo);
log.info("rno..." + rno);
vo.setRno(rno);
int modifyCount = service.modify(vo);
log.info("Reply Modify Count : " + modifyCount);
return modifyCount == 1 ?
new ResponseEntity<>("success", HttpStatus.OK) :
new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}