Coverage Report - com.ctrip.apollo.adminservice.controller.ItemController
 
Classes in this File Line Coverage Branch Coverage Complexity
ItemController
32%
10/31
20%
2/10
2.6
 
 1  
 package com.ctrip.apollo.adminservice.controller;
 2  
 
 3  
 import java.util.LinkedList;
 4  
 import java.util.List;
 5  
 
 6  
 import org.springframework.beans.factory.annotation.Autowired;
 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.RestController;
 13  
 
 14  
 import com.ctrip.apollo.biz.entity.Item;
 15  
 import com.ctrip.apollo.biz.service.ItemService;
 16  
 import com.ctrip.apollo.common.auth.ActiveUser;
 17  
 import com.ctrip.apollo.common.utils.BeanUtils;
 18  
 import com.ctrip.apollo.core.dto.ItemDTO;
 19  
 import com.ctrip.apollo.core.exception.NotFoundException;
 20  
 
 21  
 @RestController
 22  1
 public class ItemController {
 23  
 
 24  
   @Autowired
 25  
   private ItemService itemService;
 26  
 
 27  
   @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
 28  
   public ItemDTO createOrUpdate(@PathVariable("appId") String appId,
 29  
       @PathVariable("clusterName") String clusterName,
 30  
       @PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto,
 31  
       @ActiveUser UserDetails user) {
 32  0
     Item entity = BeanUtils.transfrom(Item.class, dto);
 33  0
     Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
 34  0
     if (managedEntity != null) {
 35  0
       managedEntity.setDataChangeLastModifiedBy(user.getUsername());
 36  0
       BeanUtils.copyEntityProperties(entity, managedEntity);
 37  0
       entity = itemService.update(managedEntity);
 38  
     } else {
 39  0
       entity.setDataChangeCreatedBy(user.getUsername());
 40  0
       entity = itemService.save(entity);
 41  
     }
 42  
 
 43  0
     dto = BeanUtils.transfrom(ItemDTO.class, entity);
 44  0
     return dto;
 45  
   }
 46  
 
 47  
   @RequestMapping(path = "/items/{itemId}", method = RequestMethod.DELETE)
 48  
   public void delete(@PathVariable("itemId") long itemId, @ActiveUser UserDetails user) {
 49  0
     Item entity = itemService.findOne(itemId);
 50  0
     if (entity == null) throw new NotFoundException("item not found for itemId " + itemId);
 51  0
     itemService.delete(entity.getId(), user.getUsername());
 52  0
   }
 53  
 
 54  
   @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
 55  
   public List<ItemDTO> findItems(@PathVariable("appId") String appId,
 56  
       @PathVariable("clusterName") String clusterName,
 57  
       @PathVariable("namespaceName") String namespaceName) {
 58  3
     List<Item> items = itemService.findItems(appId, clusterName, namespaceName);
 59  3
     List<ItemDTO> itemDTOs = new LinkedList<>();
 60  
 
 61  3
     for (Item item: items){
 62  9
       ItemDTO itemDTO = BeanUtils.transfrom(ItemDTO.class, item);
 63  9
       itemDTO.setLastModifiedBy(item.getDataChangeLastModifiedBy());
 64  9
       itemDTO.setLastModifiedTime(item.getDataChangeLastModifiedTime());
 65  9
       itemDTOs.add(itemDTO);
 66  9
     }
 67  3
     return itemDTOs;
 68  
   }
 69  
 
 70  
   @RequestMapping("/items/{itemId}")
 71  
   public ItemDTO get(@PathVariable("itemId") long itemId) {
 72  0
     Item item = itemService.findOne(itemId);
 73  0
     if (item == null) throw new NotFoundException("item not found for itemId " + itemId);
 74  0
     return BeanUtils.transfrom(ItemDTO.class, item);
 75  
   }
 76  
 
 77  
   @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key}")
 78  
   public ItemDTO get(@PathVariable("appId") String appId,
 79  
       @PathVariable("clusterName") String clusterName,
 80  
       @PathVariable("namespaceName") String namespaceName, @PathVariable("key") String key) {
 81  0
     Item item = itemService.findOne(appId, clusterName, namespaceName, key);
 82  0
     if (item == null) throw new NotFoundException(
 83  0
         String.format("item not found for %s %s %s %s", appId, clusterName, namespaceName, key));
 84  0
     return BeanUtils.transfrom(ItemDTO.class, item);
 85  
   }
 86  
 }