ReplyController > SPRINGBOOT

본문 바로가기
사이트 내 전체검색

SPRINGBOOT

ReplyController

페이지 정보

profile_image
작성자 관리자
댓글 0건 조회 79회 작성일 24-06-13 09:38

본문

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;

}


}

댓글목록

등록된 댓글이 없습니다.

회원로그인

회원가입

사이트 정보

공지사항
자유게시판
질문답변
1:1문의

 

별명 : 터푸가위
주소 : 부산시 동래구 명장로20번길 90
대표 : 박규태
메일 : dancepkt@******.com

접속자집계

오늘
169
어제
285
최대
10,760
전체
277,775
Copyright © dancePKT . All rights reserved.