提交 b1c4b7d8 编写于 作者: J Jason Song

Merge pull request #78 from yiming187/item_update

Add update API for item/release
......@@ -3,6 +3,8 @@ package com.ctrip.apollo.adminservice.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -13,8 +15,8 @@ import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.service.ItemService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.exception.NotFoundException;
@RestController
public class ItemController {
......@@ -25,18 +27,40 @@ public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping(path = "/items/", method = RequestMethod.POST)
public ResponseEntity<ItemDTO> create(@RequestBody ItemDTO dto) {
Item entity = BeanUtils.transfrom(Item.class, dto);
entity = itemService.save(entity);
dto = BeanUtils.transfrom(ItemDTO.class, entity);
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
}
@RequestMapping(path = "/items/{itemId}", method = RequestMethod.DELETE)
public void delete(@PathVariable("itemId") long itemId) {
Item entity = itemService.findOne(itemId);
if (entity == null) throw new NotFoundException("item not found for itemId " + itemId);
itemService.delete(entity.getId());
}
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
public List<ItemDTO> findItems(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Item> items = viewService.findItems(appId, clusterName, namespaceName);
return BeanUtils.batchTransform(ItemDTO.class, items);
}
@RequestMapping("/items/{itemId}")
public ItemDTO findOne(@PathVariable("itemId") long itemId) {
public ItemDTO get(@PathVariable("itemId") long itemId) {
Item item = itemService.findOne(itemId);
return BeanUtils.transfrom(ItemDTO.class, item);
}
@RequestMapping(path = "/item/{itemId}", method = RequestMethod.PUT)
public ItemDTO update(@PathVariable("itemId") long itemId, @RequestBody ItemDTO dto) {
Item entity = itemService.findOne(itemId);
if (entity == null) throw new NotFoundException("item not found for itemId " + itemId);
entity = itemService.update(BeanUtils.transfrom(Item.class, dto));
return BeanUtils.transfrom(ItemDTO.class, entity);
}
}
package com.ctrip.apollo.adminservice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.service.ItemSetService;
import com.ctrip.apollo.core.dto.ItemChangeSets;
@RestController
public class ItemSetController {
@Autowired
private ItemSetService itemSetService;
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/itemset", method = RequestMethod.POST)
public ResponseEntity<Void> create(@RequestBody ItemChangeSets changeSet) {
itemSetService.updateSet(changeSet);
return ResponseEntity.status(HttpStatus.OK).build();
}
}
......@@ -30,6 +30,14 @@ public class NamespaceController {
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.POST)
public ResponseEntity<NamespaceDTO> create(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName, @RequestBody NamespaceDTO dto) {
if (!appId.equals(dto.getAppId())) {
throw new IllegalArgumentException(String
.format("Path variable %s is not equals to object field %s", appId, dto.getAppId()));
}
if (!clusterName.equals(dto.getClusterName())) {
throw new IllegalArgumentException(String.format(
"Path variable %s is not equals to object field %s", clusterName, dto.getClusterName()));
}
Namespace entity = BeanUtils.transfrom(Namespace.class, dto);
entity = namespaceService.save(entity);
dto = BeanUtils.transfrom(NamespaceDTO.class, entity);
......@@ -41,8 +49,8 @@ public class NamespaceController {
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
if (entity == null)
throw new NotFoundException("namespace not found for namespaceName " + namespaceName);
if (entity == null) throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
namespaceService.delete(entity.getId());
}
......@@ -56,6 +64,8 @@ public class NamespaceController {
@RequestMapping("/namespaces/{namespaceId}")
public NamespaceDTO get(@PathVariable("namespaceId") Long namespaceId) {
Namespace namespace = namespaceService.findOne(namespaceId);
if (namespace == null)
throw new NotFoundException(String.format("namespace not found for %s", namespaceId));
return BeanUtils.transfrom(NamespaceDTO.class, namespace);
}
......@@ -64,6 +74,8 @@ public class NamespaceController {
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
return BeanUtils.transfrom(NamespaceDTO.class, namespace);
}
......@@ -71,14 +83,22 @@ public class NamespaceController {
public NamespaceDTO update(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @RequestBody NamespaceDTO dto) {
if (!appId.equals(dto.getAppId())) {
throw new IllegalArgumentException(String
.format("Path variable %s is not equals to object field %s", appId, dto.getAppId()));
}
if (!clusterName.equals(dto.getClusterName())) {
throw new IllegalArgumentException(String.format(
"Path variable %s is not equals to object field %s", clusterName, dto.getClusterName()));
}
if (!namespaceName.equals(dto.getNamespaceName())) {
throw new IllegalArgumentException(
String.format("Path variable %s is not equals to object field %s", namespaceName,
dto.getNamespaceName()));
}
Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
if (entity == null)
throw new NotFoundException("namespace not found for name " + namespaceName);
if (entity == null) throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
entity = namespaceService.update(BeanUtils.transfrom(Namespace.class, dto));
return BeanUtils.transfrom(NamespaceDTO.class, entity);
}
......
......@@ -5,6 +5,8 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Release;
......@@ -13,7 +15,7 @@ import com.ctrip.apollo.biz.service.ReleaseService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.core.exception.NotFoundException;
@RestController
public class ReleaseController {
......@@ -28,13 +30,15 @@ public class ReleaseController {
private ConfigService configService;
@RequestMapping("/release/{releaseId}")
public ReleaseDTO findOne(@PathVariable("releaseId") long releaseId) {
public ReleaseDTO get(@PathVariable("releaseId") long releaseId) {
Release release = releaseService.findOne(releaseId);
if (release == null)
throw new NotFoundException(String.format("release not found for %s", releaseId));
return BeanUtils.transfrom(ReleaseDTO.class, release);
}
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases")
public List<ReleaseDTO> findReleases(@PathVariable("appId") String appId,
public List<ReleaseDTO> find(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Release> releases = viewSerivce.findReleases(appId, clusterName, namespaceName);
......@@ -42,14 +46,21 @@ public class ReleaseController {
}
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases/latest")
public ReleaseDTO findLatestRelease(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName){
if (StringUtils.isContainEmpty(appId, clusterName, namespaceName)){
return null;
}
public ReleaseDTO getLatest(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
Release release = configService.findRelease(appId, clusterName, namespaceName);
if (release == null) throw new NotFoundException(
String.format("latest release not found for %s %s %s", appId, clusterName, namespaceName));
return BeanUtils.transfrom(ReleaseDTO.class, release);
}
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases", method = RequestMethod.POST)
public ReleaseDTO buildRelease(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @RequestParam("name") String name,
@RequestParam(name = "comment", required = false) String comment) {
Release release = releaseService.buildRelease(name, comment, appId, clusterName, namespaceName);
return BeanUtils.transfrom(ReleaseDTO.class, release);
}
}
......@@ -3,7 +3,6 @@ package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.utils.BeanUtils;
......
......@@ -5,6 +5,7 @@ import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.utils.BeanUtils;
@Service
public class ItemService {
......@@ -12,9 +13,23 @@ public class ItemService {
@Autowired
private ItemRepository itemRepository;
public void delete(long id) {
itemRepository.delete(id);
}
public Item findOne(long itemId) {
Item item = itemRepository.findOne(itemId);
return item;
}
public Item save(Item item) {
return itemRepository.save(item);
}
public Item update(Item item) {
Item managedItem = itemRepository.findOne(item.getId());
BeanUtils.copyEntityProperties(item, managedItem);
return itemRepository.save(managedItem);
}
}
package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO;
@Service
public class ItemSetService {
@Autowired
private ItemRepository itemRepository;
public void updateSet(ItemChangeSets changeSet) {
for (ItemDTO item : changeSet.getCreateItems()) {
Item entity = BeanUtils.transfrom(Item.class, item);
itemRepository.save(entity);
}
for (ItemDTO item : changeSet.getUpdateItems()) {
Item entity = BeanUtils.transfrom(Item.class, item);
Item managedItem = itemRepository.findOne(entity.getId());
BeanUtils.copyEntityProperties(entity, managedItem);
itemRepository.save(managedItem);
}
for (ItemDTO item : changeSet.getDeletedItems()) {
Item entity = BeanUtils.transfrom(Item.class, item);
itemRepository.delete(entity.getId());
}
}
}
package com.ctrip.apollo.biz.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.repository.NamespaceRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.core.exception.NotFoundException;
import com.google.gson.Gson;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service
public class ReleaseService {
@Autowired
private ReleaseRepository releaseRepository;
@Autowired
private NamespaceRepository namespaceRepository;
@Autowired
private ItemRepository itemRepository;
private Gson gson = new Gson();
public Release findOne(long releaseId) {
Release release = releaseRepository.findOne(releaseId);
return release;
}
public Release buildRelease(String name, String comment, String appId, String clusterName,
String namespaceName) {
Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
clusterName, namespaceName);
if (namespace == null) {
throw new NotFoundException(String.format("Could not find namespace for %s %s %s", appId,
clusterName, namespaceName));
}
List<Item> items = itemRepository.findByNamespaceId(namespace.getId());
Map<String, String> configurations = new HashMap<String, String>();
for (Item item : items) {
configurations.put(item.getKey(), item.getValue());
}
Release release = new Release();
release.setName(name);
release.setComment(comment);
release.setAppId(appId);
release.setClusterName(clusterName);
release.setNamespaceName(namespaceName);
release.setConfigurations(gson.toJson(configurations));
return releaseRepository.save(release);
}
}
......@@ -14,7 +14,7 @@ public class NamespaceDTO {
return appId;
}
public String getClusterId() {
public String getClusterName() {
return clusterName;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册