Notice
Recent Posts
Recent Comments
Link
«   2024/06   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

ReplyController.java 본문

SPRING/chapter04_MVC

ReplyController.java

GAWON 2023. 7. 18. 09:14
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);
		
		
	}
	
	
	
}

'SPRING > chapter04_MVC' 카테고리의 다른 글

UploadController.java  (0) 2023.07.18
SampleController.java  (0) 2023.07.18
HomeController.java  (0) 2023.07.18
CommonController.java  (0) 2023.07.14
BoardController.java  (0) 2023.07.14