| 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.http.HttpStatus; |
| 7 | |
import org.springframework.http.ResponseEntity; |
| 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.biz.service.ViewService; |
| 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 ViewService viewService; |
| 26 | |
|
| 27 | |
@Autowired |
| 28 | |
private ItemService itemService; |
| 29 | |
|
| 30 | |
@RequestMapping(path = "/items/", method = RequestMethod.POST) |
| 31 | |
public ResponseEntity<ItemDTO> create(@RequestBody ItemDTO dto) { |
| 32 | 0 | Item entity = BeanUtils.transfrom(Item.class, dto); |
| 33 | 0 | entity = itemService.save(entity); |
| 34 | 0 | dto = BeanUtils.transfrom(ItemDTO.class, entity); |
| 35 | 0 | return ResponseEntity.status(HttpStatus.CREATED).body(dto); |
| 36 | |
} |
| 37 | |
|
| 38 | |
@RequestMapping(path = "/items/{itemId}", method = RequestMethod.DELETE) |
| 39 | |
public void delete(@PathVariable("itemId") long itemId) { |
| 40 | 0 | Item entity = itemService.findOne(itemId); |
| 41 | 0 | if (entity == null) throw new NotFoundException("item not found for itemId " + itemId); |
| 42 | 0 | itemService.delete(entity.getId()); |
| 43 | 0 | } |
| 44 | |
|
| 45 | |
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items") |
| 46 | |
public List<ItemDTO> findItems(@PathVariable("appId") String appId, |
| 47 | |
@PathVariable("clusterName") String clusterName, |
| 48 | |
@PathVariable("namespaceName") String namespaceName) { |
| 49 | 3 | List<Item> items = viewService.findItems(appId, clusterName, namespaceName); |
| 50 | 3 | return BeanUtils.batchTransform(ItemDTO.class, items); |
| 51 | |
} |
| 52 | |
|
| 53 | |
@RequestMapping("/items/{itemId}") |
| 54 | |
public ItemDTO get(@PathVariable("itemId") long itemId) { |
| 55 | 0 | Item item = itemService.findOne(itemId); |
| 56 | 0 | return BeanUtils.transfrom(ItemDTO.class, item); |
| 57 | |
} |
| 58 | |
|
| 59 | |
@RequestMapping(path = "/item/{itemId}", method = RequestMethod.PUT) |
| 60 | |
public ItemDTO update(@PathVariable("itemId") long itemId, @RequestBody ItemDTO dto) { |
| 61 | 0 | Item entity = itemService.findOne(itemId); |
| 62 | 0 | if (entity == null) throw new NotFoundException("item not found for itemId " + itemId); |
| 63 | 0 | entity = itemService.update(BeanUtils.transfrom(Item.class, dto)); |
| 64 | 0 | return BeanUtils.transfrom(ItemDTO.class, entity); |
| 65 | |
} |
| 66 | |
} |