[B -2-37] Ajax 댓글 처리 10
2019. 10. 8. 22:48ㆍProject B (SPMS)/Project B 파트5
반응형
댓글 삭제 / 조회
src/main/java
com.spms.controller
ReplyController.java
get()
remove()
...더보기
package com.spms.controller;
import java.util.List;
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.RestController;
import com.spms.domain.Criteria;
import com.spms.domain.ReplyVO;
import com.spms.service.ReplyService;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
@RequestMapping("/replies/")
@RestController
@Log4j
@AllArgsConstructor
public class ReplyController {
private ReplyService replyService;
@PostMapping(value = "/new", consumes = "application/json", produces = { MediaType.TEXT_PLAIN_VALUE })
public ResponseEntity<String> create(@RequestBody ReplyVO reply) {
log.info("ReplyVO: " + reply);
int insertCount = replyService.register(reply);
log.info("Reply INSERT COUNT: " + insertCount);
return insertCount == 1 ? new ResponseEntity<>("success", HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
@GetMapping(value = "/pages/{bno}/{page}",
produces = {
MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_UTF8_VALUE
})
public ResponseEntity<List<ReplyVO>> getList(
@PathVariable("page") int page,
@PathVariable("bno") Long bno) {
log.info("getList.................");
Criteria cri = new Criteria(page,10);
log.info(cri);
return new ResponseEntity<>(replyService.getList(cri, bno), HttpStatus.OK);
}
@GetMapping(value = "/{rno}",
produces = { MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_UTF8_VALUE })
public ResponseEntity<ReplyVO> get(@PathVariable("rno") Long rno) {
log.info("get: " + rno);
return new ResponseEntity<>(replyService.get(rno), HttpStatus.OK);
}
@DeleteMapping(value = "/{rno}", produces = { MediaType.TEXT_PLAIN_VALUE })
public ResponseEntity<String> remove(@PathVariable("rno") Long rno) {
log.info("remove: " + rno);
return replyService.remove(rno) == 1
? new ResponseEntity<>("success", HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
댓글의 수정 / 삭제 / 조회는 JSON이나 문자열을 반환하도록 설계한다.
댓글 수정
...더보기
package com.spms.controller;
import java.util.List;
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 com.spms.domain.Criteria;
import com.spms.domain.ReplyVO;
import com.spms.service.ReplyService;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
@RequestMapping("/replies/")
@RestController
@Log4j
@AllArgsConstructor
public class ReplyController {
private ReplyService replyService;
@PostMapping(value = "/new", consumes = "application/json", produces = { MediaType.TEXT_PLAIN_VALUE })
public ResponseEntity<String> create(@RequestBody ReplyVO reply) {
log.info("ReplyVO: " + reply);
int insertCount = replyService.register(reply);
log.info("Reply INSERT COUNT: " + insertCount);
return insertCount == 1 ? new ResponseEntity<>("success", HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
@GetMapping(value = "/pages/{bno}/{page}",
produces = {
MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_UTF8_VALUE
})
public ResponseEntity<List<ReplyVO>> getList(
@PathVariable("page") int page,
@PathVariable("bno") Long bno) {
log.info("getList.................");
Criteria cri = new Criteria(page,10);
log.info(cri);
return new ResponseEntity<>(replyService.getList(cri, bno), HttpStatus.OK);
}
@GetMapping(value = "/{rno}",
produces = { MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_UTF8_VALUE })
public ResponseEntity<ReplyVO> get(@PathVariable("rno") Long rno) {
log.info("get: " + rno);
return new ResponseEntity<>(replyService.get(rno), HttpStatus.OK);
}
@DeleteMapping(value = "/{rno}", produces = { MediaType.TEXT_PLAIN_VALUE })
public ResponseEntity<String> remove(@PathVariable("rno") Long rno) {
log.info("remove: " + rno);
return replyService.remove(rno) == 1
? new ResponseEntity<>("success", HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
@RequestMapping(method = { RequestMethod.PUT,
RequestMethod.PATCH
}, value = "/{rno}",
consumes = "application/json",
produces = {
MediaType.TEXT_PLAIN_VALUE
})
public ResponseEntity<String> modify(
@RequestBody ReplyVO reply,
@PathVariable("rno") Long rno) {
reply.setRno(rno);
log.info("rno: " + rno);
log.info("modify: " + reply);
return replyService.modify(reply) == 1
? new ResponseEntity<>("success", HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
댓글 수정은 JSON 형태로 전달되는 데이터와 파라미터로 전달되는 댓글 번호를 처리하고,
PUT 방식이나 PATCH 방식을 이용하도록 처리하고, 실제 수정되는 데이터는 JSON 포멧이므로 @RequestBody를 이용해서 처리한다.
@RequestBody로 처리되는 데이터는 일반 파라미터나 @PathVariable 파라미터를 처리할 수 없기때문에 직접 처리해주는 부분을 주의해야한다.
댓글 수정 테스트 : Restlet Clinet
반응형
'Project B (SPMS) > Project B 파트5' 카테고리의 다른 글
[B -2-39] Ajax 댓글 처리 12 (0) | 2019.10.09 |
---|---|
[B -2-38] Ajax 댓글 처리 11 (0) | 2019.10.09 |
[B -2-36] Ajax 댓글 처리 9 (0) | 2019.10.08 |
[B -2-35] Ajax 댓글 처리 8 (0) | 2019.10.06 |
[B -2-34] Ajax 댓글 처리 7 (0) | 2019.10.06 |