Java's new project learning online notes - day3(9)

Posted by chadowch on Sun, 01 Dec 2019 21:19:38 +0100

4 delete page
User operation process:
1. The user enters the user list and clicks "delete"
2. Execute the deletion operation, and prompt "deletion succeeded" or "deletion failed"
4.1 delete page interface definition

@ApiOperation("delete page by ID")
public ResponseResult delete(String id);

4.2 delete page server development
4.2.1Dao
Use the deleteById method provided by Spring Data to complete the deletion.
4.2.2 Service

//Delete page   
  public ResponseResult delete(String id){   
     CmsPage one = this.getById(id);     
    if(one!=null){     
       //Delete page     
        cmsPageRepository.deleteById(id);        
     return new ResponseResult(CommonCode.SUCCESS);      
   }     
   return new ResponseResult(CommonCode.FAIL);     
}

4.2.3Controller

@DeleteMapping("/del/{id}") //Using the delete method of http to complete the post operation
 public ResponseResult delete(@PathVariable("id") String id) { 
   return pageService.delete(id); 
}

4.3 delete page front end development
4.3.1 Api method

/*Page deletion*/ export const page_del = id => {
   return http.requestDelete(apiUrl+'/cms/page/del/'+id)
 }

4.3.2 preparation page
1. Add the delete button on the page

<el‐table‐column label="operation" width="120">  
     <template slot‐scope="page"> 
        <el‐button   
        size="small"type="text"     
      @click="edit(page.row.pageId)">edit      
    </el‐button>     
    <el‐button       
    size="small"type="text"   
        @click="del(page.row.pageId)">delete    
      </el‐button>   
    </template>  
   </el‐table‐column>

2. Delete event

//delete  
      del:function (pageId) {      
   this.$confirm('Are you sure to delete this page?', 'Tips', {}).then(() => {
           cmsApi.page_del(pageId).then((res)=>{    
         if(res.success){    
           this.$message({       
          type: 'success',        
        message: 'Delete successful!'     
           });      
          //Query page  
              this.query()   
          }else{       
        this.$message({   
              type: 'error',  
               message: 'Delete failed!'  
              });      
       }       
    })     
      }) 
      }

5 exception handling 5.1 analysis of exception handling
To find a problem from the service method of the add page:

//Add page  
   public CmsPageResult add(CmsPage cmsPage){ //Verify that the page exists,
//Query according to page name, site Id, page web path         
        CmsPage cmsPage1 =

cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),  cmsPage.getSiteId(), cmsPage.getPageWebPath());   
       if(cmsPage1==null){   
         cmsPage.setPageId(null);
//Add page primary key automatically generated by spring data     
        cmsPageRepository.save(cmsPage);  
           //Return result         
    CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,cmsPage);       
      return cmsPageResult;  
       }    
    return new CmsPageResult(CommonCode.FAIL,null);  
   }

Question:
1. As long as the operation is not successful, the code above only returns "error code: 11111, failure information: operation failed" to the user, and it is unable to distinguish the specific error information.
2. Where is the exception caught during the execution of the service method? try/catch needs to be added to all services. If try/catch needs to be added to the controller, the code redundancy is serious and difficult to maintain.
Solution:
1. The coding sequence in the Service method is to check and judge first, throw specific exception information if there is a problem, and finally execute specific business operations to return success information.
2. To catch exceptions in the unified exception handling class, there is no need for the controller to catch exceptions and return the unified response information to the user.
The code template is as follows:

//Add page public CmsPageResult add(CmsPage cmsPage){ 
   //Verify if cmsPage is empty   
  if(cmsPage == null){    
    //Exception thrown, illegal request    
     //...  
   }   
 //Query based on the page name (the page name has created a unique index in mongodb)   
 CmsPage cmsPage1 =  cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),  cmsPage.getSiteId(), cmsPage.getPageWebPath());  
   //Check whether the page exists, throw an exception if it already exists  
   if(cmsPage1 !=null){    
    //Exception thrown, same page name already exists    
     //...  
   }    
cmsPage.setPageId(null);//Add page primary key automatically generated by spring data   
  CmsPage save = cmsPageRepository.save(cmsPage); 
    //Return result  
   CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,save);  
   return cmsPageResult; }

Topics: Java Spring MongoDB