ReplyController
페이지 정보
![profile_image](https://dancepkt.cafe24.com/data/member_image/ad/admin.gif?1630310007)
본문
package web.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
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.extern.slf4j.Slf4j;
import web.dto.Criteria;
import web.dto.PageMaker;
import web.dto.ReplyDto;
import web.service.ReplyService;
@RestController
@RequestMapping("/replies/")
public class ReplyController {
@Autowired
private ReplyService service;
//글 등록
@RequestMapping(value="", method=RequestMethod.POST)
public ResponseEntity<String> register(@RequestBody ReplyDto vo) {
ResponseEntity<String> entity = null;
try {
service.replyInsert(vo);
entity = new ResponseEntity<String>("success", HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
entity = new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST); //400에러
}
return entity;
}
//글 목록
@RequestMapping(value="/all/{bno}", method=RequestMethod.GET)
public ResponseEntity<List<ReplyDto>> list(@PathVariable("bno") Integer bno) {
// @PathVariable - URI 경로에서 원하는 데이터를 추출하는 용도로 사용
ResponseEntity<List<ReplyDto>> entity = null;
try {
entity = new ResponseEntity<>(service.replyList(bno), HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
entity = new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return entity;
}
//글 수정
@RequestMapping(value="/{rno}", method={ RequestMethod.PUT, RequestMethod.PATCH })
public ResponseEntity<String> update(@PathVariable("rno") Integer rno, @RequestBody ReplyDto vo) {
ResponseEntity<String> entity = null;
try {
vo.setRno(rno);
service.replyUpdate(vo);
entity = new ResponseEntity<String>("success", HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
entity = new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
return entity;
}
//글 삭제
@RequestMapping(value="/{rno}", method=RequestMethod.DELETE)
public ResponseEntity<String> remove(@PathVariable("rno") Integer rno) {
ResponseEntity<String> entity = null;
try {
service.replyDelete(rno);
entity = new ResponseEntity<String>("success", HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
entity = new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
return entity;
}
//페이징 처리
@RequestMapping(value="/{bno}/{page}", method=RequestMethod.GET)
public ResponseEntity<Map<String, Object>> listPage(@PathVariable("bno") Integer bno,@PathVariable("page") Integer page) {
ResponseEntity<Map<String, Object>> entity = null;
try {
Criteria cri = new Criteria();
cri.setPage(page);
PageMaker pageMaker = new PageMaker();
pageMaker.setCri(cri);
Map<String, Object> map = new HashMap<String, Object>();
List<ReplyDto> list = service.listPage(bno, cri);
map.put("list", list);
int replyCount = service.count(bno);
pageMaker.setTotalCount(replyCount);
map.put("pageMaker", pageMaker);
entity = new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
entity = new ResponseEntity<Map<String, Object>>(HttpStatus.BAD_REQUEST);
}
return entity;
}
}
- 이전글ReplyServiceImpl 24.06.13
- 다음글● DTO 만들기 ( ReplyDto.java ) 24.06.13
댓글목록
등록된 댓글이 없습니다.