Commit 450548f0 by Jason Song Committed by GitHub

Merge pull request #375 from lepdou/open

open api item 用key当主键
parents 03609414 e45c7bc3
...@@ -4,22 +4,12 @@ import com.ctrip.framework.apollo.common.dto.BaseDTO; ...@@ -4,22 +4,12 @@ import com.ctrip.framework.apollo.common.dto.BaseDTO;
public class OpenItemDTO extends BaseDTO { public class OpenItemDTO extends BaseDTO {
private long id;
private String key; private String key;
private String value; private String value;
private String comment; private String comment;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getKey() { public String getKey() {
return key; return key;
} }
......
...@@ -35,8 +35,7 @@ public class ItemController { ...@@ -35,8 +35,7 @@ public class ItemController {
@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)") @PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST) @RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
public OpenItemDTO createItem(@PathVariable String appId, @PathVariable String env, public OpenItemDTO createItem(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String @PathVariable String clusterName, @PathVariable String namespaceName,
namespaceName,
@RequestBody OpenItemDTO item, HttpServletRequest request) { @RequestBody OpenItemDTO item, HttpServletRequest request) {
RequestPrecondition.checkArguments( RequestPrecondition.checkArguments(
...@@ -60,48 +59,53 @@ public class ItemController { ...@@ -60,48 +59,53 @@ public class ItemController {
} }
@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)") @PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}", method = RequestMethod.PUT) @RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key:.+}", method = RequestMethod.PUT)
public void updateItem(@PathVariable String appId, @PathVariable String env, public void updateItem(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String namespaceName, @PathVariable String clusterName, @PathVariable String namespaceName,
@PathVariable long itemId, @RequestBody OpenItemDTO item, @PathVariable String key, @RequestBody OpenItemDTO item, HttpServletRequest request) {
HttpServletRequest request) {
RequestPrecondition.checkArguments(item != null, "item payload can not be empty");
RequestPrecondition.checkArguments(item != null && item.getId() > 0 && itemId == item.getId(),
"item data error");
RequestPrecondition.checkArguments( RequestPrecondition.checkArguments(
!StringUtils.isContainEmpty(item.getKey(), item.getValue(), item !StringUtils.isContainEmpty(item.getKey(), item.getValue(), item.getDataChangeLastModifiedBy()),
.getDataChangeLastModifiedBy()),
"key,value,dataChangeLastModifiedBy 字段不能为空"); "key,value,dataChangeLastModifiedBy 字段不能为空");
RequestPrecondition.checkArguments(item.getKey().equals(key), "path中的key和payload中的key不一致");
if (userService.findByUserId(item.getDataChangeLastModifiedBy()) == null) { if (userService.findByUserId(item.getDataChangeLastModifiedBy()) == null) {
throw new BadRequestException("用户不存在."); throw new BadRequestException("用户不存在");
} }
ItemDTO toUpdateItem = itemService.loadItem(Env.fromString(env), itemId); ItemDTO toUpdateItem = itemService.loadItem(Env.fromString(env), appId, clusterName, namespaceName, item.getKey());
if (toUpdateItem == null) { if (toUpdateItem == null) {
throw new BadRequestException("item not exist"); throw new BadRequestException("item不存在");
} }
//protect. only value,comment,lastModifiedBy can be modified //protect. only value,comment,lastModifiedBy can be modified
toUpdateItem.setComment(item.getComment()); toUpdateItem.setComment(item.getComment());
toUpdateItem.setValue(item.getValue()); toUpdateItem.setValue(item.getValue());
toUpdateItem.setDataChangeLastModifiedBy(item.getDataChangeLastModifiedBy()); toUpdateItem.setDataChangeLastModifiedBy(item.getDataChangeLastModifiedBy());
itemService.updateItem(appId, Env.fromString(env), clusterName, namespaceName, itemService.updateItem(appId, Env.fromString(env), clusterName, namespaceName, toUpdateItem);
toUpdateItem);
} }
@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)") @PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}", method = RequestMethod.DELETE) @RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key:.+}", method = RequestMethod.DELETE)
public void deleteItem(@PathVariable String appId, @PathVariable String env, public void deleteItem(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String namespaceName, @PathVariable String clusterName, @PathVariable String namespaceName,
@PathVariable long itemId, @RequestParam String operator, @PathVariable String key, @RequestParam String operator,
HttpServletRequest request) { HttpServletRequest request) {
if (userService.findByUserId(operator) == null) { if (userService.findByUserId(operator) == null) {
throw new BadRequestException("用户不存在."); throw new BadRequestException("用户不存在");
}
ItemDTO toDeleteItem = itemService.loadItem(Env.valueOf(env), appId, clusterName, namespaceName, key);
if (toDeleteItem == null){
throw new BadRequestException("item不存在");
} }
itemService.deleteItem(Env.fromString(env), itemId, operator);
itemService.deleteItem(Env.fromString(env), toDeleteItem.getId(), operator);
} }
} }
...@@ -90,8 +90,9 @@ public class AdminServiceAPI { ...@@ -90,8 +90,9 @@ public class AdminServiceAPI {
return Arrays.asList(itemDTOs); return Arrays.asList(itemDTOs);
} }
public ItemDTO loadItem(Env env, long itemId) { public ItemDTO loadItem(Env env, String appId, String clusterName, String namespaceName, String key) {
return restTemplate.get(env, "/items/{itemId}", ItemDTO.class, itemId); return restTemplate.get(env, "apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key}",
ItemDTO.class, appId, clusterName, namespaceName, key);
} }
public void updateItemsByChangeSet(String appId, Env env, String clusterName, String namespace, public void updateItemsByChangeSet(String appId, Env env, String clusterName, String namespace,
......
...@@ -114,8 +114,8 @@ public class ItemService { ...@@ -114,8 +114,8 @@ public class ItemService {
return itemAPI.findItems(appId, env, clusterName, namespaceName); return itemAPI.findItems(appId, env, clusterName, namespaceName);
} }
public ItemDTO loadItem(Env env, long itemId) { public ItemDTO loadItem(Env env, String appId, String clusterName, String namespaceName, String key) {
return itemAPI.loadItem(env, itemId); return itemAPI.loadItem(env, appId, clusterName, namespaceName, key);
} }
public void syncItems(List<NamespaceIdentifier> comparedNamespaces, List<ItemDTO> sourceItems) { public void syncItems(List<NamespaceIdentifier> comparedNamespaces, List<ItemDTO> sourceItems) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment