Coverage Report - com.ctrip.apollo.adminservice.controller.AppController
 
Classes in this File Line Coverage Branch Coverage Complexity
AppController
100%
25/25
100%
8/8
2.2
 
 1  
 package com.ctrip.apollo.adminservice.controller;
 2  
 
 3  
 import java.util.List;
 4  
 
 5  
 import org.springframework.beans.factory.annotation.Autowired;
 6  
 import org.springframework.data.domain.Pageable;
 7  
 import org.springframework.security.core.userdetails.UserDetails;
 8  
 import org.springframework.web.bind.annotation.PathVariable;
 9  
 import org.springframework.web.bind.annotation.RequestBody;
 10  
 import org.springframework.web.bind.annotation.RequestMapping;
 11  
 import org.springframework.web.bind.annotation.RequestMethod;
 12  
 import org.springframework.web.bind.annotation.RequestParam;
 13  
 import org.springframework.web.bind.annotation.RestController;
 14  
 
 15  
 import com.ctrip.apollo.biz.entity.App;
 16  
 import com.ctrip.apollo.biz.service.AdminService;
 17  
 import com.ctrip.apollo.biz.service.AppService;
 18  
 import com.ctrip.apollo.common.auth.ActiveUser;
 19  
 import com.ctrip.apollo.common.utils.BeanUtils;
 20  
 import com.ctrip.apollo.core.dto.AppDTO;
 21  
 import com.ctrip.apollo.core.exception.NotFoundException;
 22  
 import com.ctrip.apollo.core.utils.StringUtils;
 23  
 
 24  
 @RestController
 25  6
 public class AppController {
 26  
 
 27  
   @Autowired
 28  
   private AppService appService;
 29  
 
 30  
   @Autowired
 31  
   private AdminService adminService;
 32  
 
 33  
   @RequestMapping(path = "/apps", method = RequestMethod.POST)
 34  
   public AppDTO createOrUpdate(@RequestBody AppDTO dto, @ActiveUser UserDetails user) {
 35  7
     App entity = BeanUtils.transfrom(App.class, dto);
 36  7
     App managedEntity = appService.findOne(entity.getAppId());
 37  7
     if (managedEntity != null) {
 38  3
       managedEntity.setDataChangeLastModifiedBy(user.getUsername());
 39  3
       BeanUtils.copyEntityProperties(entity, managedEntity);
 40  3
       entity = appService.update(managedEntity);
 41  
     } else {
 42  4
       entity.setDataChangeCreatedBy(user.getUsername());
 43  4
       entity.setDataChangeLastModifiedBy(user.getUsername());
 44  4
       entity = adminService.createNewApp(entity);
 45  
     }
 46  
 
 47  5
     dto = BeanUtils.transfrom(AppDTO.class, entity);
 48  5
     return dto;
 49  
   }
 50  
 
 51  
   @RequestMapping(path = "/apps/{appId}", method = RequestMethod.DELETE)
 52  
   public void delete(@PathVariable("appId") String appId, @ActiveUser UserDetails user) {
 53  2
     App entity = appService.findOne(appId);
 54  2
     if (entity == null) throw new NotFoundException("app not found for appId " + appId);
 55  1
     appService.delete(entity.getId(), user.getUsername());
 56  1
   }
 57  
 
 58  
   @RequestMapping("/apps")
 59  
   public List<AppDTO> find(@RequestParam(value = "name", required = false) String name,
 60  
       Pageable pageable) {
 61  3
     List<App> app = null;
 62  3
     if (StringUtils.isBlank(name)) {
 63  2
       app = appService.findAll(pageable);
 64  
     } else {
 65  1
       app = appService.findByName(name);
 66  
     }
 67  3
     return BeanUtils.batchTransform(AppDTO.class, app);
 68  
   }
 69  
 
 70  
   @RequestMapping("/apps/{appId}")
 71  
   public AppDTO get(@PathVariable("appId") String appId) {
 72  7
     App app = appService.findOne(appId);
 73  7
     if (app == null) throw new NotFoundException("app not found for appId " + appId);
 74  5
     return BeanUtils.transfrom(AppDTO.class, app);
 75  
   }
 76  
 
 77  
   @RequestMapping("/apps/{appId}/unique")
 78  
   public boolean isAppIdUnique(@PathVariable("appId") String appId) {
 79  2
     return appService.isAppIdUnique(appId);
 80  
   }
 81  
 
 82  
 }