2016년 7월 26일 화요일

(방학특강)#9.스프링부트,JAP게시판(글수정)-AngulerJS,스프링RestController - 스프링실무교육학원

(방학특강)#9.스프링부트,JAP게시판(글수정)-AngulerJS,스프링RestController - 스프링실무교육학원

게시판 글수정

[board_controller.js]
'usestrict';

App.controller('BoardController',['$scope', 'BoardService',
      function($scope, BoardService) {
          var self = this;
          self.board={id:null,name:'',passwd:'',title:'',content:''};
          self.page=[];
              
          //리스트 보기
          self.list = function(curPage){
              BoardService.list(curPage)
              .then(
                 function(data) {
                     self.page = data;
                    console.log("[controller:list]", self.page);
                     //alert("목록보기 성공!");
                 },
                 function(errResponse){
                     console.error('Error whilefetching page...');
                 }
              );
          };  
         
          //글 입력
          self.create = function(board) {
              BoardService.create(board)
              .then(
                  function() {
                    alert("SaveOK!");
                    self.list(0);
                  },
                  function(errResponse){
                     console.error('Error whilecreating Article.');
                  }
               );   
           };
          
           //글 수정
           self.update= function(board, id){
              BoardService.update(board, id)
                      .then(
                                         function() {
                                      alert("Update OK!");
                                      self.list(self.page.number);  //현재 페이지 리로드
                              },
                              function(errResponse){
                                   console.error('Error while updating User.');
                              }
                   );
           };
          
           //글 삭제
           self.delete = function(id){
               BoardService.delete(id)
                       .then(
                               function() {
                                        alert("Delete OK!");
                                        self.list(self.page.number);  //현재 페이지 리로드
                               },
                              function(errResponse){
                                   console.error('Error while deleting Article.');
                               }
                   );
           };
           
          self.list(0);
         
          // ADD or UPDATE 버튼 클릭
          self.submit = function() {
              if(self.board.id===null){                     
                  self.create(self.board);         
                  console.log("[controller:create]",self.board);
              }else{
                  self.update(self.board,self.board.id);
                 console.log('Article updated with id ', self.board.id);
              }
              self.reset();             
          };  
         
          //글읽기
          self.edit = function(id){
              console.log('[controller:edit]',id);
              console.log("3333",self.page);
              for(var i = 0; i <self.page.content.length; i++){
                  if(self.page.content[i].id === id) {
                     self.board =angular.copy(self.page.content[i]);
                     console.log("[readarticle]", self.board);
                     break;
                  }
              }
          }; 
          
          //글 삭제
          self.remove = function(id){
          if (confirm('Are you sure you want to deletethis article?')) {
                      console.log('[controller:remove]', id);
                  //글입력(수정)화면 CLEAR
                  if(self.board.id === id){ 
                     self.reset();
                  }
                  self.delete(id);
                    }else {
                        return;
                    }             
          };
         
          self.reset = function(){
          self.board={id:null,name:'',passwd:'',title:'',content:''};
              $scope.myForm.$setPristine();//reset Form
          };
         
}]);

[board_service.js]
'usestrict';

App.factory('BoardService',['$http', '$q', function($http, $q){

    return {
        
            list: function(curPage) {
                    return$http.get('http://localhost:8080/board/' + curPage)
                            .then(
                                   function(response){
                                              console.log("[service:list]servercall  suceeded.");
                                        returnresponse.data;
                                    },
                                   function(errResponse){
                                       console.error('Error while fetching contents');
                                        return$q.reject(errResponse);
                                    }
                            );
            } ,
            create: function(board){
                return$http.post('http://localhost:8080/board/', board)
                        .then(
                               function(response){
                                    console.log("[service:create]servercall  suceeded.");
                                    returnresponse.data;
                                },
                                function(errResponse){
                                   console.error('Error while creating article');
                                    return$q.reject(errResponse);
                                }
                        );
            },
            delete: function(id){
                return$http.delete('http://localhost:8080/board/'+id)
                        .then(
                               function(response){
                                    returnresponse.data;
                                },
                               function(errResponse){
                                   console.error('Error while deleting article');
                                    return$q.reject(errResponse);
                                }
                        );
            },
            update:function(board, id){
                return$http.put('http://localhost:8080/board/'+id, board)
                       .then(
                               function(response){
                                    returnresponse.data;
                                },
                               function(errResponse){
                                   console.error('Error while updating board');
                                   return$q.reject(errResponse);
                                }
                       );
            }        
    };
 }]);

[BoardService.java 추가]
           //글수정
           public voidupdate(Board board, Integer id);

[BoardServiceImpl.java 추가]

           @Override
           //-----------------------------------------
           // 글 수정
           //-----------------------------------------
           public voidupdate(Board board, Integer id) {
                     board.setId(id);
                     boardRepository.save(board);
           }

[BoardController.java 추가]
          
           //---------------------------------------------
           // 게시판 글 수정
           //---------------------------------------------
           @RequestMapping(value="/{id}",method = RequestMethod.PUT)
           publicResponseEntity<Board> delete(@RequestBody Board board,
                                                           @PathVariableInteger id) {                  
                     boardService.update(board,id);
                     returnnew ResponseEntity<Board>(board, HttpStatus.OK);
           }          

댓글 없음:

댓글 쓰기